You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltacloud.apache.org by lu...@apache.org on 2013/03/14 19:19:03 UTC

[7/15] git commit: DB Entity: make sure we instantiate the correct subclass

DB Entity: make sure we instantiate the correct subclass

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.


Project: http://git-wip-us.apache.org/repos/asf/deltacloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltacloud/commit/d86e0667
Tree: http://git-wip-us.apache.org/repos/asf/deltacloud/tree/d86e0667
Diff: http://git-wip-us.apache.org/repos/asf/deltacloud/diff/d86e0667

Branch: refs/heads/master
Commit: d86e0667c2881186750dbcc0d5116d3e8e965f56
Parents: 7f53cad
Author: David Lutterkort <lu...@redhat.com>
Authored: Mon Mar 11 15:39:50 2013 -0700
Committer: David Lutterkort <lu...@redhat.com>
Committed: Wed Mar 13 17:28:13 2013 -0700

----------------------------------------------------------------------
 server/lib/db/entity.rb |   17 ++++++++++++++++-
 1 files changed, 16 insertions(+), 1 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/d86e0667/server/lib/db/entity.rb
----------------------------------------------------------------------
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,