Perhaps you want something like this?
In my STI, I have entities, all of which have an address as well as a
contact person.
class Entity < ActiveRecord::Base
composed_of :address,
:class_name => Address,
:mapping =>
[ # database # ruby
[ :primary_address, :primary ],
[ :secondary_address, :secondary ],
[ :city, :city ],
[ :region, :region ],
[ :postal_code, :postcode ],
]
composed_of :contact,
:class_name => Contact,
:mapping =>
[ # database # ruby
[ :phone_number, :phone ],
[ :mobile_number, :mobile ],
[ :fax_number, :fax ],
[ :email_address, :email ]
]
end
Then I use aggregations to ‘wrap’ some of the columns into something
that makes more sense within the application domain …
class Address
attr_reader :primary, :secondary, :city, :region, :postcode
def initialize(primary, secondary, city, region, postcode)
@primary = primary
@secondary = secondary
@city = city
@region = region
@postcode = postcode
end
def to_s
“#{@primary}, @ #{@city}”
end
end
class Contact
attr_reader :phone, :mobile, :fax, :email
def initialize(phone, mobile, fax, email)
@phone = phone
@mobile = mobile
@fax = fax
@email = email
end
def to_s
“M: #{@mobile} T: #{@phone} F: #{@fax} E: #{@email}”
end
end
And the human class, gets everything defined in Entity for free …
class Human < Entity
composed_of :name,
:class_name => Name,
:mapping =>
[ # database # ruby
[ :first_name, :first ],
[ :middle_name, :middle ],
[ :last_name, :last ],
[ :description, :descr ]
]
end
As does each Organization
class Organization < Entity
def location
“#{self.city}, #{self.region}”
end
end
Hope this helps …
– G.