Module: Mixture::Extensions::Attributable::InstanceMethods
- Defined in:
- lib/mixture/extensions/attributable.rb
Overview
The instance methods for attribution.
Instance Method Summary collapse
- #attribute(key, value = Undefined) ⇒ Object
-
#attributes ⇒ Hash{Symbol => Object}
The attributes defined on this instance.
-
#attributes=(attrs) ⇒ void
Sets the attributes on the instance.
-
#unknown_attribute(attr) ⇒ Object
Called when an unknown attribute is accessed using #attribute.
Instance Method Details
#attribute(key) ⇒ Object #attribute(key, value) ⇒ void
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/mixture/extensions/attributable.rb', line 91 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 |
#attributes ⇒ Hash{Symbol => Object}
The attributes defined on this instance. It returns a hash containing the key-value pairs for each attribute.
57 58 59 60 61 |
# File 'lib/mixture/extensions/attributable.rb', line 57 def attributes Hash[self.class.attributes.map do |name, attr| [name, instance_variable_get(attr.ivar)] end] end |
#attributes=(attrs) ⇒ void
This method returns an undefined value.
Sets the attributes on the instance. It iterates through the given hash and uses #attribute to set the key, value pair.
49 50 51 |
# File 'lib/mixture/extensions/attributable.rb', line 49 def attributes=(attrs) attrs.each { |key, value| attribute(key, value) } end |
#unknown_attribute(attr) ⇒ Object
Called when an unknown attribute is accessed using
#attribute. By default, it just raises an ArgumentError
.
68 69 70 |
# File 'lib/mixture/extensions/attributable.rb', line 68 def unknown_attribute(attr) fail ArgumentError, "Unknown attribute #{attr} passed" end |