module Mixture::Extensions::Hashable
Has the mixture respond to `#[]` and `#[]=`.
Constants
- MAPPED_METHODS
The methods that are mapped directly to the {#to_hash} method.
@return [Array<Symbol>]
Public Instance Methods
[](key)
click to toggle source
Alias for {Attributable::InstanceMethods#attribute}.
@param (see Attributable::InstanceMethods#attribute
) @return (see Attributable::InstanceMethods#attribute
)
# File lib/mixture/extensions/hashable.rb, line 28 def [](key) attribute(key.to_s.intern) end
[]=(key, value)
click to toggle source
Alias for {Attributable::InstanceMethods#attribute}.
@param (see Attributable::InstanceMethods#attribute
) @return (see Attributable::InstanceMethods#attribute
)
# File lib/mixture/extensions/hashable.rb, line 36 def []=(key, value) attribute(key.to_s.intern, value) end
Also aliased as: store
fetch(key, default = Undefined) { |key.intern| ... }
click to toggle source
@overload fetch(key)
Performs a fetch. This acts just like Hash's `fetch`. @param key [Symbol] The key to check for an attribute. @raise [KeyError] If the key isn't present. @return [Object] The attribute's value.
@overload fetch(key, default)
Performs a fetch. This acts just like Hash's fetch. If the attribute doesn't exist, the default argument value is used as a value instead. @param key [Symbol] The key to check for an attribute. @param default [Object] The value to use instead of an error. @return [Object] The attribute's value, or the default value instead.
@overload fetch(key) { }
Performs a fetch. This acts just like Hash's fetch. If the attribute doesn't exist, the value of the block is used as a value instead. @param key [Symbol] The key to check for an attribute. @yield [key] The block is called if an attribute does not exist. @yieldparam key [Symbol] The key of the non-existant attribute. @yieldreturn [Object] Return value of the method. @return [Object] The attribute's value, or the block's value instead.
# 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)
click to toggle source
Checks for an attribute with the given name.
@param key [Symbol] The name of the attribute to check. @return [Boolean]
# File lib/mixture/extensions/hashable.rb, line 51 def key?(key) self.class.attributes.key?(key.to_s.intern) end
Also aliased as: has_key?
to_hash()
click to toggle source
(see Attributable::InstanceMethods#attributes
)
# File lib/mixture/extensions/hashable.rb, line 42 def to_hash attributes end
Also aliased as: to_h