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
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/mixture/extensions/attributable.rb', line 96 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.
62 63 64 65 66 |
# File 'lib/mixture/extensions/attributable.rb', line 62 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.
54 55 56 |
# File 'lib/mixture/extensions/attributable.rb', line 54 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
.
73 74 75 |
# File 'lib/mixture/extensions/attributable.rb', line 73 def unknown_attribute(attr) fail ArgumentError, "Unknown attribute #{attr.inspect} passed" end |