Class: Deltacloud::Database::Entity

Inherits:
Sequel::Model
  • Object
show all
Defined in:
lib/db/entity.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#propertiesObject

Returns the value of attribute properties.



21
22
23
# File 'lib/db/entity.rb', line 21

def properties
  @properties
end

Class Method Details

.inherited(subclass) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# 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

.retrieve(model) ⇒ Object

Load the entity for the CIMI::Model model, or create a new one if none exists yet



57
58
59
60
61
62
63
64
65
66
67
68
# 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

Instance Method Details

#after_initializeObject



46
47
48
49
50
51
52
53
# 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

#before_saveObject



41
42
43
44
# File 'lib/db/entity.rb', line 41

def before_save
  self.ent_properties = properties.to_json
  super
end

#to_hashObject



28
29
30
31
32
33
34
# 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