Module: Ricordami::HasAttributes::InstanceMethods

Defined in:
lib/ricordami/has_attributes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



36
37
38
# File 'lib/ricordami/has_attributes.rb', line 36

def attributes
  @attributes
end

Instance Method Details

#idObject

ActiveModel::AttributeMethods doesn’t seem to generate a reader for id that uses #attributes in Ruby 1.8.7, so hard-coding it now



50
51
52
# File 'lib/ricordami/has_attributes.rb', line 50

def id
  @attributes["id"]
end

#initialize(attrs = {}) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/ricordami/has_attributes.rb', line 38

def initialize(attrs = {})
  attrs = {} if attrs.nil?
  @attributes = {}.with_indifferent_access
  @reloading = false
  update_mem_attributes(attrs) unless attrs.empty?
  set_default_attribute_values
end

#update_mem_attributes(attrs) ⇒ Object

Replace attribute values with the hash attrs Note: attrs keys can be strings or symbols



56
57
58
59
60
# File 'lib/ricordami/has_attributes.rb', line 56

def update_mem_attributes(attrs)
  @reloading = true
  update_mem_attributes!(attrs)
  @reloading = false
end

#update_mem_attributes!(attrs) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/ricordami/has_attributes.rb', line 62

def update_mem_attributes!(attrs)
  valid_keys = self.class.attributes.keys
  attrs.symbolize_keys.slice(*valid_keys).each do |name, value|
    send(:"#{name}=", value)
  end
  true
end