Class: Profitbricks::Model
- Inherits:
-
Object
- Object
- Profitbricks::Model
- 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)
-
- (Object) attributes
def get_xml_and_update_attributes(hash, attributes=nil).
-
- (Model) initialize(hash = {}, parent = nil)
constructor
A new instance of Model.
- - (Object) reload
Constructor Details
- (Model) initialize(hash = {}, parent = nil)
A new instance of Model
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 32 |
# File 'lib/profitbricks/model.rb', line 28 def self.belongs_to(model, = {}) klass = Profitbricks.get_class model.to_s, @@associations[model] = {:type => :belongs_to, :class => klass} define_method(model) { instance_variable_get("@#{model}") } end |
+ (Object) has_many(model, options = {})
22 23 24 25 26 |
# File 'lib/profitbricks/model.rb', line 22 def self.has_many(model, = {}) klass = Profitbricks.get_class model.to_s[0..-2], @@associations[model] = {:type => :collection, :class => klass} define_method(model) { instance_variable_get("@#{model}") } end |
Instance Method Details
- (Object) attributes
def get_xml_and_update_attributes(hash, attributes=nil)
attributes = hash.keys if attributes.nil?
attributes.each do |a|
initialize_getter(a, hash[a]) if hash[a]
end
hash, attributes = self.class.expand_attributes(hash, attributes, self.class)
xml = self.class.build_xml(hash, attributes)
end
def self.get_xml_and_update_attributes(hash, attributes=[])
hash, attributes = expand_attributes(hash, attributes, name())
self.build_xml(hash, attributes)
end
48 49 50 51 52 53 54 |
# File 'lib/profitbricks/model.rb', line 48 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 |