class Iugu::Object

Attributes

errors[RW]

Public Class Methods

new(attributes = {}) click to toggle source
# File lib/iugu/object.rb, line 10
def initialize(attributes = {})
  @unsaved_attributes = Set.new
  set_attributes attributes
end

Public Instance Methods

add_accessor(name) click to toggle source
# File lib/iugu/object.rb, line 15
def add_accessor(name)
  singleton_class.class_eval do
    define_method(name.to_s) { self.attributes[name.to_s] }
    define_method(name.to_s + "=") do |value|
      self.attributes[name.to_s] = value
      self.unsaved_attributes.add name.to_s
    end unless name.to_s == 'id'
  end
end
attributes() click to toggle source
# File lib/iugu/object.rb, line 36
def attributes
  @attributes
end
copy(object) click to toggle source
# File lib/iugu/object.rb, line 44
def copy(object)
  @unsaved_attributes = Set.new
  @attributes = {}
  set_attributes object.attributes
end
method_missing(name, *args) click to toggle source
Calls superclass method
# File lib/iugu/object.rb, line 25
def method_missing(name, *args)
  return super unless name.to_s.end_with? '='
  return super if name.to_s.start_with? 'id'
  add_accessor(name.to_s[0...-1])
  return send(name, args[0])
end
modified_attributes() click to toggle source
# File lib/iugu/object.rb, line 40
def modified_attributes
  Iugu::Utils.intersect @unsaved_attributes, @attributes
end
set_attributes(attributes, unsaved = false) click to toggle source
# File lib/iugu/object.rb, line 50
def set_attributes(attributes, unsaved = false)
  @attributes = Iugu::Utils.stringify_keys(attributes)
  @attributes.each do |k, v|
    add_accessor(k)
  end
  @unsaved_attributes = @attributes.keys.to_set if unsaved
end
unsaved_attributes() click to toggle source
# File lib/iugu/object.rb, line 32
def unsaved_attributes
  @unsaved_attributes
end

Protected Instance Methods

metaclass() click to toggle source
# File lib/iugu/object.rb, line 60
def metaclass
  class << self; self; end
end