Hash
This class has dubious semantics and we only have it so that people can write params[:key] instead of params[‘key’].
Initializes a new mash.
@param [Hash, Object] constructor
The default value for the mash. 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/dm-core/support/mash.rb, line 12 12: def initialize(constructor = {}) 13: if constructor.is_a?(Hash) 14: super() 15: update(constructor) 16: else 17: super(constructor) 18: end 19: end
Sets the value associated with the specified key.
@param [Object] key The key to set. @param [Object] value The value to set the key to.
# File lib/dm-core/support/mash.rb, line 42 42: def []=(key, value) 43: regular_writer(convert_key(key), convert_value(value)) 44: end
Gets the default value for the mash.
@param [Object] key
The default value for the mash. 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/dm-core/support/mash.rb, line 27 27: def default(key = nil) 28: if key.is_a?(Symbol) && include?(key = key.to_s) 29: self[key] 30: else 31: super 32: end 33: end
@param [Object] key The key to delete from the mash.
# File lib/dm-core/support/mash.rb, line 96 96: def delete(key) 97: super(convert_key(key)) 98: end
Returns a mash that includes everything but the given keys.
@param [Array
@return [Mash] A new mash without the selected keys.
@example
{ :one => 1, :two => 2, :three => 3 }.except(:one) #=> { "two" => 2, "three" => 3 }
# File lib/dm-core/support/mash.rb, line 109 109: def except(*keys) 110: self.dup.except!(*keys.map {|k| convert_key(k)}) 111: end
Removes the specified keys from the mash.
@param [Array] *keys The mash keys to exclude.
@return [Hash] hash
@example
mash = { :one => 1, :two => 2, :three => 3 } mash.except!(:one, :two) mash # => { :three => 3 }
# File lib/dm-core/support/mash.rb, line 123 123: def except!(*keys) 124: keys.each { |key| delete(key) } 125: self 126: end
@param [Object] key The key to fetch. @param [Array] *extras Default value.
@return [Object] The value at key or the default value.
# File lib/dm-core/support/mash.rb, line 76 76: def fetch(key, *extras) 77: super(convert_key(key), *extras) 78: end
Determines whether the mash contains the specified key.
@param [Object] key The key to check for. @return [Boolean] True if the key exists in the mash.
# File lib/dm-core/support/mash.rb, line 64 64: def key?(key) 65: super(convert_key(key)) 66: end
@param [Hash] hash The hash to merge with the mash.
@return [Mash] A new mash with the hash values merged in.
# File lib/dm-core/support/mash.rb, line 91 91: def merge(hash) 92: self.dup.update(hash) 93: end
Used to provide the same interface as Hash.
@return [Mash] This mash unchanged.
# File lib/dm-core/support/mash.rb, line 131 131: def stringify_keys!; self end
@return [Hash] The mash as a Hash with symbolized keys.
# File lib/dm-core/support/mash.rb, line 134 134: def symbolize_keys 135: h = Hash.new(default) 136: each { |key, val| h[key.to_sym] = val } 137: h 138: end
@return [Hash] The mash as a Hash with string keys.
# File lib/dm-core/support/mash.rb, line 141 141: def to_hash 142: Hash.new(default).merge(self) 143: end
Updates the mash with the key/value pairs from the specified hash.
@param [Hash] other_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/dm-core/support/mash.rb, line 53 53: def update(other_hash) 54: other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) } 55: self 56: end
@param [Object] key The key to convert.
@param [Object]
The converted key. If the key was a symbol, it will be converted to a string.
@api private
# File lib/dm-core/support/mash.rb, line 153 153: def convert_key(key) 154: key.kind_of?(Symbol) ? key.to_s : key 155: end
@param [Object] value 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/dm-core/support/mash.rb, line 164 164: def convert_value(value) 165: if value.class == Hash 166: mash = Mash.new(value) 167: mash.default = value.default 168: mash 169: elsif value.is_a?(Array) 170: value.collect { |e| convert_value(e) } 171: else 172: value 173: end 174: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.