In Files

Parent

Files

Class Index [+]

Quicksearch

Mash

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:

Basic Example

   
  mash = Mash.new
  mash.name? # => false
  mash.name = "Bob"
  mash.name # => "Bob"
  mash.name? # => true

Hash Conversion Example

  
  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

Bang Example

  mash = Mash.new
  mash.author # => nil
  mash.author! # => <Mash>
  
  mash = Mash.new
  mash.author!.name = "Michael Bleigh"
  mash.author # => <Mash name="Michael Bleigh">

Public Class Methods

new(source_hash = nil, &blk) click to toggle source

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

Public Instance Methods

[](key) click to toggle source

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
Also aliased as: regular_reader
deep_merge(other_hash) click to toggle source

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
deep_merge!(other_hash) click to toggle source
Alias for: deep_update
deep_update(other_hash) click to toggle source

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
Also aliased as: deep_merge!
default(key = nil) click to toggle source

Borrowed from Merb’s Mash object.

Parameters

key

The default value for the mash. Defaults to nil.

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/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
dup() click to toggle source

Duplicates the current mash as a new mash.

     # File lib/mash.rb, line 100
100:   def dup
101:     Mash.new(self)
102:   end
Also aliased as: regular_dup
initializing_reader(key) click to toggle source

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
inspect() click to toggle source

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
Also aliased as: regular_inspect, to_s
key?(key) click to toggle source
     # File lib/mash.rb, line 105
105:   def key?(key)
106:     picky_key?(convert_key(key))
107:   end
Also aliased as: picky_key?
merge!(other_hash) click to toggle source
Alias for: update
picky_key?(key) click to toggle source
Alias for: key?
regular_dup() click to toggle source
Alias for: dup
regular_inspect() click to toggle source
Alias for: inspect
regular_reader(key) click to toggle source
Alias for: []
to_hash() click to toggle source

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
to_s() click to toggle source
Alias for: inspect
update(other_hash) click to toggle source

Parameters

other_hash

A hash to update values in the mash with. Keys will be stringified and Hashes will be converted to Mashes.

Returns

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
Also aliased as: merge!

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.