Module: Mixture::Extensions::Attributable::InstanceMethods

Defined in:
lib/mixture/extensions/attributable.rb

Overview

The instance methods for attribution.

Instance Method Summary collapse

Instance Method Details

#attribute(key, value = Undefined) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/mixture/extensions/attributable.rb', line 39

def attribute(key, value = Undefined)
  attr = self.class.attributes.fetch(key) do
    return unknown_attribute(key)
  end

  return instance_variable_get(attr.ivar) if value == Undefined

  value = attr.update(value)
  instance_variable_set(attr.ivar, value)
end

#attributesObject



29
30
31
32
33
# File 'lib/mixture/extensions/attributable.rb', line 29

def attributes
  Hash[self.class.attributes.map do |name, attr|
    [name, instance_variable_get(attr.ivar)]
  end]
end

#attributes=(attrs) ⇒ Object

Sets the attributes on the instance.



25
26
27
# File 'lib/mixture/extensions/attributable.rb', line 25

def attributes=(attrs)
  attrs.each { |key, value| attribute(key, value) }
end

#unknown_attribute(attr) ⇒ Object



35
36
37
# File 'lib/mixture/extensions/attributable.rb', line 35

def unknown_attribute(attr)
  fail ArgumentError, "Unknown attribute #{attr} passed"
end