From: David Lutterkort <lut...@redhat.com> We only ever instantiated Database::Entity, but never any of its subclasses. We now keep a map of CIMI::Model => Database::Entity subclass and call new on the appropriate subclass of Entity. --- server/lib/db/entity.rb | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
diff --git a/server/lib/db/entity.rb b/server/lib/db/entity.rb index 45a7f30..7f32161 100644 --- a/server/lib/db/entity.rb +++ b/server/lib/db/entity.rb @@ -44,6 +44,7 @@ module Deltacloud end def after_initialize + super if ent_properties self.properties = JSON::parse(ent_properties) else @@ -61,11 +62,25 @@ module Deltacloud entity = Provider::lookup.entities_dataset.first(h) unless entity h[:provider_id] = Provider::lookup.id - entity = Entity.new(h) + entity = @@model_entity_map[model.class].new(h) end entity end + def self.inherited(subclass) + super + # Build a map from CIMI::Model class to Entity subclass. This only + # works if the two classes have the same name in their respective + # modules. + # The map is used to determine what Entity subclass to instantiate + # for a given model in +retrieve+ + @@model_entity_map ||= Hash.new(Entity) + n = subclass.name.split('::').last + if k = CIMI::Model::const_get(n) + @@model_entity_map[k] = subclass + end + end + private def self.model_hash(model) { :be_kind => model.class.name, -- 1.8.1.4