Module: Mixture::Extensions::Hashable
- Extended by:
- Forwardable
- Includes:
- Comparable, Enumerable
- Defined in:
- lib/mixture/extensions/hashable.rb
Overview
Has the mixture respond to #[] and #[]=.
Constant Summary collapse
- MAPPED_METHODS =
The methods that are mapped directly to the #to_hash method.
%w( each <=> keys values each_key each_value has_value? value? size length empty? each_pair ).map(&:intern)
Instance Method Summary collapse
-
#[](key) ⇒ Object
Alias for Attributable::InstanceMethods#attribute.
-
#[]=(key, value) ⇒ Object
(also: #store)
Alias for Attributable::InstanceMethods#attribute.
- #fetch(key, default = Undefined) ⇒ Object
-
#key?(key) ⇒ Boolean
(also: #has_key?)
Checks for an attribute with the given name.
-
#to_hash ⇒ Hash{Symbol => Object}
(also: #to_h)
The attributes defined on this instance.
Instance Method Details
#[](key) ⇒ Object
Alias for Attributable::InstanceMethods#attribute.
27 28 29 |
# File 'lib/mixture/extensions/hashable.rb', line 27 def [](key) attribute(key.to_s.intern) end |
#[]=(key, value) ⇒ Object Also known as: store
Alias for Attributable::InstanceMethods#attribute.
35 36 37 |
# File 'lib/mixture/extensions/hashable.rb', line 35 def []=(key, value) attribute(key.to_s.intern, value) end |
#fetch(key) ⇒ Object #fetch(key, default) ⇒ Object #fetch(key) {|key| ... } ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/mixture/extensions/hashable.rb', line 84 def fetch(key, default = Undefined) case when key?(key.to_s.intern) then attribute(key.to_s.intern) when block_given? then yield(key.to_s.intern) when default != Undefined then default else fail KeyError, "Undefined attribute #{key.to_s.intern}" end end |
#key?(key) ⇒ Boolean Also known as: has_key?
Checks for an attribute with the given name.
50 51 52 |
# File 'lib/mixture/extensions/hashable.rb', line 50 def key?(key) self.class.attributes.key?(key.to_s.intern) end |
#to_hash ⇒ Hash{Symbol => Object} Also known as: to_h
The attributes defined on this instance. It returns a hash containing the key-value pairs for each attribute.
41 42 43 |
# File 'lib/mixture/extensions/hashable.rb', line 41 def to_hash attributes end |