Module: Kangaroo::Attributes

Included in:
Model::Base, Model::Field
Defined in:
lib/kangaroo/model/attributes.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#attributesHash

Read all attributes. Attributes are read via getters.

Returns:

  • (Hash)

    attributes



45
46
47
48
49
50
51
# File 'lib/kangaroo/model/attributes.rb', line 45

def attributes
  {}.tap do |attributes|
    self.class.attribute_names.each do |key|
      attributes[key] = send(key)
    end
  end
end

#attributes=(attributes) ⇒ Hash

Mass set attributes. Attribute values are set via setters, not directly stored in the @attributes Hash.

Parameters:

  • attributes (Hash)

    Hash of attribute names and values

Returns:

  • (Hash)

    attributes



34
35
36
37
38
39
40
# File 'lib/kangaroo/model/attributes.rb', line 34

def attributes= attributes
  attributes.except('id', :id).map do |key_value|
    __send__ "#{key_value.first}=", key_value.last
  end

  self.attributes
end

#freezeObject

Freeze this object



54
55
56
57
58
# File 'lib/kangaroo/model/attributes.rb', line 54

def freeze
  @changed_attributes.freeze
  @attributes.freeze
  super
end

#read_attribute(name) ⇒ Object

Read an attribute value by name

Parameters:

  • name (String, Symbol)

    attribute name

Returns:

  • attribute value



15
16
17
# File 'lib/kangaroo/model/attributes.rb', line 15

def read_attribute name
  @attributes[name.to_s]
end

#write_attribute(name, value) ⇒ Object

Write an attribute by name

Parameters:

  • name (String, Symbol)

    attribute name

  • value

    attribute value to set

Returns:

  • attribute value



24
25
26
27
# File 'lib/kangaroo/model/attributes.rb', line 24

def write_attribute name, value
  attribute_will_change! name.to_s
  @attributes[name.to_s] = value
end