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.
28 29 30 |
# File 'lib/mixture/extensions/hashable.rb', line 28 def [](key) attribute(key.to_s.intern) end |
#[]=(key, value) ⇒ Object Also known as: store
Alias for Attributable::InstanceMethods#attribute.
36 37 38 |
# File 'lib/mixture/extensions/hashable.rb', line 36 def []=(key, value) attribute(key.to_s.intern, value) end |
#fetch(key) ⇒ Object #fetch(key, default) ⇒ Object #fetch(key) {|key| ... } ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/mixture/extensions/hashable.rb', line 85 def fetch(key, default = Undefined) if key?(key.to_s.intern) then attribute(key.to_s.intern) elsif block_given? then yield(key.to_s.intern) elsif 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.
51 52 53 |
# File 'lib/mixture/extensions/hashable.rb', line 51 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.
42 43 44 |
# File 'lib/mixture/extensions/hashable.rb', line 42 def to_hash attributes end |