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/mash.rb, line 47
47: def initialize(source_hash = nil, &blk)
48: deep_update(source_hash) if source_hash
49: super(&blk)
50: end
Retrieves an attribute set in the Mash. Will convert any key passed in to a string before retrieving.
# File lib/mash.rb, line 79
79: def [](key)
80: key = convert_key(key)
81: regular_reader(key)
82: end
Performs a deep_update on a duplicate of the current mash.
# File lib/mash.rb, line 124
124: def deep_merge(other_hash)
125: dup.deep_merge!(other_hash)
126: end
Recursively merges this mash with the passed in hash, merging each hash in the hierarchy.
# File lib/mash.rb, line 130
130: def deep_update(other_hash)
131: other_hash = other_hash.to_hash if other_hash.is_a?(Mash)
132: other_hash = other_hash.stringify_keys
133: other_hash.each_pair do |k,v|
134: k = convert_key(k)
135: self[k] = self[k].to_mash if self[k].is_a?(Hash) unless self[k].is_a?(Mash)
136: if self[k].is_a?(Hash) && other_hash[k].is_a?(Hash)
137: self[k] = self[k].deep_merge(other_hash[k]).dup
138: else
139: self.send(k + "=", convert_value(other_hash[k],true))
140: end
141: end
142: end
Borrowed from Merb’s Mash object.
| key | The default value for the mash. Defaults to nil. |
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/mash.rb, line 66
66: def default(key = nil)
67: if key.is_a?(Symbol) && key?(key)
68: self[key]
69: else
70: key ? super : super()
71: end
72: end
Duplicates the current mash as a new mash.
# File lib/mash.rb, line 100
100: def dup
101: Mash.new(self)
102: 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/mash.rb, line 93
93: def initializing_reader(key)
94: return self[key] if key?(key)
95: self[key] = Mash.new
96: end
Prints out a pretty object-like string of the defined attributes.
# File lib/mash.rb, line 112
112: def inspect
113: ret = "<#{self.class.to_s}"
114: keys.sort.each do |key|
115: ret << " #{key}=#{self[key].inspect}"
116: end
117: ret << ">"
118: ret
119: end
# File lib/mash.rb, line 105
105: def key?(key)
106: picky_key?(convert_key(key))
107: end
Converts a mash back to a hash (with stringified keys)
# File lib/mash.rb, line 165
165: def to_hash
166: Hash.new(default).merge(self)
167: end
| other_hash |
A hash to update values in the mash with. Keys will be stringified and Hashes will be converted to Mashes.
| Mash | The updated mash. |
# File lib/mash.rb, line 152
152: def update(other_hash)
153: other_hash.each_pair do |key, value|
154: if respond_to?(convert_key(key) + "=")
155: self.send(convert_key(key) + "=", convert_value(value))
156: else
157: regular_writer(convert_key(key), convert_value(value))
158: end
159: end
160: self
161: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.