Class: BaseModel

Inherits:
Object
  • Object
show all
Defined in:
lib/deltacloud/models/base_model.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(init = nil) ⇒ BaseModel

Returns a new instance of BaseModel.



22
23
24
25
26
27
28
29
# File 'lib/deltacloud/models/base_model.rb', line 22

def initialize(init=nil)
  if ( init )
    @id=init[:id]
    init.each{|k,v|
      self.send( "#{k}=", v ) if ( self.respond_to?( "#{k}=" ) )
    }
  end
end

Class Method Details

.attr_accessor(*vars) ⇒ Object



31
32
33
34
35
# File 'lib/deltacloud/models/base_model.rb', line 31

def self.attr_accessor(*vars)
  @attributes ||= [:id]
  @attributes.concat vars
  super
end

.attributesObject



37
38
39
# File 'lib/deltacloud/models/base_model.rb', line 37

def self.attributes
  @attributes
end

Instance Method Details

#attributesObject



41
42
43
# File 'lib/deltacloud/models/base_model.rb', line 41

def attributes
  self.class.attributes
end

#idObject



45
46
47
# File 'lib/deltacloud/models/base_model.rb', line 45

def id
  @id
end

#to_hashObject



49
50
51
52
53
# File 'lib/deltacloud/models/base_model.rb', line 49

def to_hash
  out = {}
  self.attributes.each { |attribute| out.merge!({ attribute => self.send(:"#{attribute}") } ) }
  out
end

#to_json(*a) ⇒ Object



55
56
57
# File 'lib/deltacloud/models/base_model.rb', line 55

def to_json(*a)
  self.to_hash.to_json(*a)
end