Module: Toolchain::Attributes::InstanceMethods

Defined in:
lib/toolchain/attributes.rb

Instance Method Summary collapse

Instance Method Details

#attributesHash

Returns keys and values for each defined attribute.

Returns:

  • (Hash)

    keys and values for each defined attribute.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/toolchain/attributes.rb', line 109

def attributes
  include_nil = Configuration.include_nil_in_attributes

  attributes = Hash.new.tap do |attrs|
    Helpers.each_key(self.class) do |key|
      value = send(key)

      if !value.nil? || (value.nil? && include_nil)
        attrs[key] = value
      end
    end
  end

  transformation = Configuration.hash_transformation
  Helpers.send(transformation, attributes)
end

#attributes=(value) ⇒ Object

Mass-assignment for the defined attributes by passing in a Hash. Non-existing attributes are ignored.

Parameters:

  • value (Hash)


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/toolchain/attributes.rb', line 131

def attributes=(value)
  if !value.kind_of?(Hash)
    raise Errors::InvalidMassAssignment,
      "Can't mass-assign #{value.class} (#{value}) type " +
      "to #{self.class}#attributes."
  end

  value = Helpers.symbolize_keys(value)
  keys = Array.new.tap do |keys|
    Helpers.each_key(self.class) { |key| keys << key }
    keys.uniq!
  end

  value.each do |key, value|
    send("#{key}=", value) if keys.include?(key)
  end
end

#initialize(attributes = {}) ⇒ Object

Parameters:

  • attributes (Hash) (defaults to: {})


103
104
105
# File 'lib/toolchain/attributes.rb', line 103

def initialize(attributes = {})
  self.attributes = attributes
end