module Toolchain::Attributes::InstanceMethods

Public Class Methods

new(attributes = {}) click to toggle source

@param attributes [Hash]

# File lib/toolchain/attributes.rb, line 103
def initialize(attributes = {})
  self.attributes = attributes
end

Public Instance Methods

attributes() click to toggle source

@return [Hash] keys and values for each defined attribute.

# 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) click to toggle source

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

@param value [Hash]

# 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