Skip to content

Instantly share code, notes, and snippets.

@lancecarlson
Created January 6, 2010 09:34
Show Gist options
  • Save lancecarlson/270162 to your computer and use it in GitHub Desktop.
Save lancecarlson/270162 to your computer and use it in GitHub Desktop.
Single Table Inheritance
READ
"(id = 4b44589b7d209c282d000001)"
READ
"(id = 4b44589b7d209c282d000002)"
READ
"(id = 4b44589b7d209c282d000003)"
READ
"(id = 4b44589b7d209c282d000004)"
- should have a type property that reflects the class
- should parent should return an instance of the child when type is explicitly specified
READ
"(type = \"Person\" AND type IN [\"Person\", \"Male\", \"Father\", \"Son\"])"
[#<Person @id="4b44589b7d209c282d000001" @name=nil @job=nil @type=Person>, #<Person @id="4b44589b7d209c282d000005" @name=nil @job=nil @type=Person>]
(type = "Father" AND type IN ["Father"])
true
READ
"(type IN [\"Father\"])"
- should discriminate types during reads (FAILED - 1)
1)
'Single Table Inheritance should discriminate types during reads' FAILED
expected: [#<Father @id="4b44589c7d209c282d00000a" @name=nil @job=nil @type=Father>, #<Father @id="4b44589c7d209c282d00000b" @name=nil @job=nil @type=Father>],
got: [#<Father @id="4b44589b7d209c282d000003" @name=nil @job=nil @type=Father>, #<Father @id="4b44589c7d209c282d000007" @name=nil @job=nil @type=Father>, #<Father @id="4b44589c7d209c282d00000a" @name=nil @job=nil @type=Father>, #<Father @id="4b44589c7d209c282d00000b" @name=nil @job=nil @type=Father>] (using ==)
./spec/sti_spec.rb:48:
Finished in 0.277598 seconds
module DataMapper
module Types
class Discriminator < DataMapper::Type
primitive String
def self.load(value, property)
typecast(value, property)
end
def self.dump(value, property)
value.name
end
def self.typecast(value, property)
Object.const_get(Extlib::Inflection.classify(value)) if value
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment