class Profitbricks::Model

Public Class Methods

belongs_to(model, options = {}) click to toggle source
# 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
has_many(model, options = {}) click to toggle source
# 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
new(hash = {}, parent=nil) click to toggle source
# 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

Public Instance Methods

attributes() click to toggle source
# 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
reload() click to toggle source
# File lib/profitbricks/model.rb, line 17
def reload
  updated = self.class.find(:id => self.id)
  update_attributes(updated)
end

Private Instance Methods

initialize_association(name, association, value) click to toggle source
# File lib/profitbricks/model.rb, line 67
def initialize_association name, association, value
  if association[:type] == :collection
    initialize_collection_association name, association, value
  elsif association[:type] == :belongs_to
    initialize_belongs_to_association name, association, value
  end
end
initialize_belongs_to_association(name, association, value) click to toggle source
# File lib/profitbricks/model.rb, line 82
def initialize_belongs_to_association name, association, value
  initialize_getter name, association[:class].send(:new, value, self)
end
initialize_collection_association(name, association, value) click to toggle source
# File lib/profitbricks/model.rb, line 75
def initialize_collection_association name, association, value
  self.instance_variable_set("@#{name}", [])
  [value].flatten.compact.each do |object|
    instance_variable_get("@#{name}").send(:push, association[:class].send(:new, object, self))
  end
end
initialize_getter(name, value=nil) click to toggle source
# File lib/profitbricks/model.rb, line 60
def initialize_getter name, value=nil
  self.class.send :define_method, name do 
    instance_variable_get("@#{name}")
  end
  self.instance_variable_set("@#{name}", value) if value != nil
end
type_cast(value) click to toggle source
# File lib/profitbricks/model.rb, line 55
def type_cast(value)
  return value.to_i if value =~ /^\d+$/
  value
end
update_attributes(updated) click to toggle source
# File lib/profitbricks/model.rb, line 48
def update_attributes(updated)
  self.instance_variables.each do |var|
    self.instance_variable_set(var, updated.instance_variable_get(var))
  end
  true
end
update_attributes_from_hash(updated) click to toggle source
# File lib/profitbricks/model.rb, line 42
def update_attributes_from_hash(updated)
  updated.keys.each do |a|
    initialize_getter(a, updated[a])
    self.instance_variable_set("@#{a}", updated[a])
  end
end