class Mash
This class has dubious semantics and we only have it so that people can write params instead of params.
Public Class Methods
@param constructor [Object]
The default value for the mash. Defaults to an empty hash.
@overload [Alternatives]
If constructor is a Hash, a new mash will be created based on the keys of the hash and no default value will be set.
# File lib/gorillib/hash/mash.rb, line 25 def initialize(constructor = {}) if constructor.is_a?(Hash) super() update(constructor) else super(constructor) end end
Public Instance Methods
@param key<Object> The key to set. @param value<Object>
The value to set the key to.
@see Mash#convert_key
@see Mash#convert_value
# File lib/gorillib/hash/mash.rb, line 56 def []=(key, value) regular_writer(convert_key(key), convert_value(value)) end
@param key<Object> The default value for the mash. Defaults to nil.
@overload [Alternatives]
If key is a Symbol and it is a key in the mash, then the default value will be set to the value matching the key.
# File lib/gorillib/hash/mash.rb, line 39 def default(key = nil) if key.is_a?(Symbol) && include?(key = key.to_s) self[key] else super end end
@param key<Object>
The key to delete from the mash.\
# File lib/gorillib/hash/mash.rb, line 109 def delete(key) super(convert_key(key)) end
@param <Object> key The key to fetch. This will be run through convert_key. @param <Array> args Default value. @yield Default block
@return [Object] The value at key or the default value.
# File lib/gorillib/hash/mash.rb, line 88 def fetch(key, *args, &block) super(convert_key(key), *args, &block) end
@param key<Object> The key to check for. This will be run through convert_key.
@return [Boolean] True if the key exists in the mash.
# File lib/gorillib/hash/mash.rb, line 75 def key?(key) super(convert_key(key)) end
@param hash<Hash> The hash to merge with the mash.
@return [Mash] A new mash with the hash values merged in.
# File lib/gorillib/hash/mash.rb, line 103 def merge(hash) self.dup.update(hash) end
Used to provide the same interface as Hash
.
@return [Mash] This mash unchanged.
# File lib/gorillib/hash/mash.rb, line 116 def stringify_keys!; self end
@return [Hash] The mash as a Hash
with symbolized keys.
# File lib/gorillib/hash/mash.rb, line 119 def symbolize_keys hsh = Hash.new(default) each{|key, val| hsh[key.to_sym] = val } hsh end
@return [Hash] The mash as a Hash
with string keys.
# File lib/gorillib/hash/mash.rb, line 126 def to_hash Hash.new(default).merge(self) end
@param other_hash<Hash>
A hash to update values in the mash with. The keys and the values will be converted to Mash format.
@return [Mash] The updated mash.
# File lib/gorillib/hash/mash.rb, line 65 def update(other_hash) other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) } self end
@param indices [Array]
The keys to retrieve values for. These will be run through +convert_key+.
@return [Array] The values at each of the provided keys
# File lib/gorillib/hash/mash.rb, line 96 def values_at(*indices) indices.map{|key| self[convert_key(key)] } end
Protected Instance Methods
@param key [Object] The key to convert.
@return [Object]
The converted key. If the key was a symbol, it will be converted to a string.
@api private
# File lib/gorillib/hash/mash.rb, line 139 def convert_key(key) key.kind_of?(Symbol) ? key.to_s : key end
@param value [Object] The value to convert.
@return [Object]
The converted value. A Hash or an Array of hashes, will be converted to their Mash equivalents.
@api private
# File lib/gorillib/hash/mash.rb, line 150 def convert_value(value) if value.class == Hash value.to_mash elsif value.is_a?(Array) value.map{|e| convert_value(e) } else value end end