# File lib/db/entity.rb, line 70 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
Load the entity for the CIMI::Model
model
, or create a new one if none exists yet
# File lib/db/entity.rb, line 57 def self.retrieve(model) unless model.id raise "Can not retrieve entity for #{model.class.name} without an id" end h = model_hash(model) entity = Provider::lookup.entities_dataset.first(h) unless entity h[:provider_id] = Provider::lookup.id entity = @@model_entity_map[model.class].new(h) end entity end
# File lib/db/entity.rb, line 46 def after_initialize super if ent_properties self.properties = JSON::parse(ent_properties) else self.properties = {} end end
# File lib/db/entity.rb, line 41 def before_save self.ent_properties = properties.to_json super end
# File lib/db/entity.rb, line 36 def properties=(v) # Make sure @properties is always a Hash @properties = v || {} end
# File lib/db/entity.rb, line 28 def to_hash retval = {} retval.merge!(:name => self.name) if !self.name.nil? retval.merge!(:description => self.description) if !self.description.nil? retval.merge!(:property => JSON::parse(self.ent_properties)) if !self.ent_properties.nil? retval end