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

.retrieve(model) ⇒ Object

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



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/db/entity.rb', line 56

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 = Entity.new(h)
  end
  entity
end

Instance Method Details

#after_initializeObject



46
47
48
49
50
51
52
# File 'lib/db/entity.rb', line 46

def after_initialize
  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