Class: Profitbricks::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/profitbricks/model.rb

Direct Known Subclasses

DataCenter, Firewall, FirewallRule, Image, Ip, IpBlock, LoadBalancer, Nic, Server, Storage

Constant Summary

@@associations =
{}

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Model) initialize(hash = {}, parent = nil)



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/profitbricks/model.rb', line 5

def initialize(hash = {}, parent=nil)
  klass = self.class.to_s.underscore
  hash.keys.each do |k|
    attribute = k.to_s.sub("#{klass}_", '').to_sym
    if @@associations[attribute]
      initialize_association(attribute, @@associations[attribute], hash[k])
    else
      initialize_getter(attribute, type_cast(hash[k]))
    end
  end
end

Class Method Details

+ (Object) belongs_to(model, options = {})



28
29
30
31
# File 'lib/profitbricks/model.rb', line 28

def self.belongs_to(model, options = {})
  klass = Profitbricks.get_class model.to_s, options
  @@associations[model] = {:type => :belongs_to, :class => klass}
end

+ (Object) has_many(model, options = {})



22
23
24
25
26
# File 'lib/profitbricks/model.rb', line 22

def self.has_many(model, options = {})
  klass = Profitbricks.get_class model.to_s[0..-2], options
  @@associations[model] = {:type => :collection, :class => klass}
  define_method(model) { instance_variable_get("@#{model}") }
end

Instance Method Details

- (Object) attributes



33
34
35
36
37
38
39
# File 'lib/profitbricks/model.rb', line 33

def attributes
  a = {}
  self.instance_variables.each do |variable|
    a[variable.to_s[1..-1].to_sym] = self.instance_variable_get(variable)
  end
  a
end

- (Object) reload



17
18
19
20
# File 'lib/profitbricks/model.rb', line 17

def reload
  updated = self.class.find(:id => self.id)
  update_attributes(updated)
end