Class | Mash |
In: |
lib/extlib/mash.rb
|
Parent: | Hash |
This class has dubious semantics and we only have it so that people can write params[:key] instead of params[‘key’].
[]= | -> | regular_writer |
@param constructor<Object>
The default value for the mash. Defaults to an empty hash.
@details [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/extlib/mash.rb, line 11 11: def initialize(constructor = {}) 12: if constructor.is_a?(Hash) 13: super() 14: update(constructor) 15: else 16: super(constructor) 17: end 18: end
@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/extlib/mash.rb, line 42 42: def []=(key, value) 43: regular_writer(convert_key(key), convert_value(value)) 44: end
@param key<Object> The default value for the mash. Defaults to nil.
@details [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/extlib/mash.rb, line 25 25: def default(key = nil) 26: if key.is_a?(Symbol) && include?(key = key.to_s) 27: self[key] 28: else 29: super 30: end 31: end
@param *rejected<Array[(String, Symbol)] The mash keys to exclude.
@return <Mash> A new mash without the selected keys.
@example
{ :one => 1, :two => 2, :three => 3 }.except(:one) #=> { "two" => 2, "three" => 3 }
# File lib/extlib/mash.rb, line 106 106: def except(*keys) 107: super(*keys.map {|k| convert_key(k)}) 108: end
@param key<Object> The key to check for. This will be run through convert_key.
@return <TrueClass, FalseClass> True if the key exists in the mash.
# File lib/extlib/mash.rb, line 61 61: def key?(key) 62: super(convert_key(key)) 63: 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/extlib/mash.rb, line 51 51: def update(other_hash) 52: other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) } 53: self 54: 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/extlib/mash.rb, line 82 82: def values_at(*indices) 83: indices.collect {|key| self[convert_key(key)]} 84: 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/extlib/mash.rb, line 139 139: def convert_value(value) 140: if value.class == Hash 141: value.to_mash 142: elsif value.is_a?(Array) 143: value.collect { |e| convert_value(e) } 144: else 145: value 146: end 147: end