Mash allows you to create pseudo-objects that have method-like accessors for hash keys. This is useful for such implementations as an API-accessing library that wants to fake robust objects without the overhead of actually doing so. Think of it as OpenStruct with some additional goodies.
A Mash will look at the methods you pass it and perform operations based on the following rules:
No punctuation: Returns the value of the hash for that key, or nil if none exists.
Assignment (=): Sets the attribute of the given method name.
Existence (?): Returns true or false depending on whether that key has been set.
Bang (!): Forces the existence of this key, used for deep Mashes. Think of it as “touch” for mashes.
mash = Mash.new mash.name? # => false mash.name = "Bob" mash.name # => "Bob" mash.name? # => true
hash = {:a => {:b => 23, :d => {:e => "abc"}}, :f => [{:g => 44, :h => 29}, 12]} mash = Mash.new(hash) mash.a.b # => 23 mash.a.d.e # => "abc" mash.f.first.g # => 44 mash.f.last # => 12
mash = Mash.new mash.author # => nil mash.author! # => <Mash> mash = Mash.new mash.author!.name = "Michael Bleigh" mash.author # => <Mash name="Michael Bleigh">
If you pass in an existing hash, it will convert it to a Mash including recursively descending into arrays and hashes, converting them as well.
# File lib/hashie/mash.rb, line 51 51: def initialize(source_hash = nil, default = nil, &blk) 52: deep_update(source_hash) if source_hash 53: default ? super(default) : super(&blk) 54: end
Retrieves an attribute set in the Mash. Will convert any key passed in to a string before retrieving.
# File lib/hashie/mash.rb, line 67 67: def [](key) 68: regular_reader(convert_key(key)) 69: end
Performs a deep_update on a duplicate of the current mash.
# File lib/hashie/mash.rb, line 98 98: def deep_merge(other_hash) 99: dup.deep_merge!(other_hash) 100: end
Recursively merges this mash with the passed in hash, merging each hash in the hierarchy.
# File lib/hashie/mash.rb, line 104 104: def deep_update(other_hash) 105: other_hash.each_pair do |k,v| 106: regular_writer(convert_key(k), convert_value(other_hash[k], true)) 107: end 108: self 109: end
Duplicates the current mash as a new mash.
# File lib/hashie/mash.rb, line 88 88: def dup 89: Mash.new(self, self.default) 90: end
This is the bang method reader, it will return a new Mash if there isn’t a value already assigned to the key requested.
# File lib/hashie/mash.rb, line 80 80: def initializing_reader(key) 81: ck = convert_key(key) 82: regular_writer(ck, Hashie::Mash.new) unless key?(ck) 83: regular_reader(ck) 84: end
# File lib/hashie/mash.rb, line 92 92: def key?(key) 93: super(convert_key(key)) 94: end
# File lib/hashie/mash.rb, line 121 121: def method_missing(method_name, *args, &blk) 122: return self[method_name] if key?(method_name) 123: match = method_name.to_s.match(/(.*?)([?=!]?)$/) 124: case match[2] 125: when "=" 126: self[match[1]] = args.first 127: when "?" 128: key?(match[1]) 129: when "!" 130: initializing_reader(match[1]) 131: else 132: default(method_name, *args, &blk) 133: end 134: end
Will return true if the Mash has had a key set in addition to normal respond_to? functionality.
# File lib/hashie/mash.rb, line 116 116: def respond_to?(method_name) 117: return true if key?(method_name) 118: super 119: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.