In Files

Parent

Class/Module 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">

Constants

VERSION

Public Class Methods

new(source_hash = nil) 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 49
def initialize(source_hash = nil)
  deep_update(source_hash) if source_hash
  super(nil)
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
def [](key)
  key = convert_key(key)
  regular_reader(key)
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
def deep_merge(other_hash)
  dup.deep_merge!(other_hash)
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
def deep_update(other_hash)
  stringified_hash = other_hash.stringify_keys
  stringified_hash.each_pair do |k,v|
    k = convert_key(k)
    self[k] = self[k].to_mash if self[k].is_a?(Hash) unless self[k].is_a?(Mash)
    if self[k].is_a?(Hash) && stringified_hash[k].is_a?(Hash)
      self[k].deep_merge!(stringified_hash[k])
    else
      self.send(k + "=", convert_value(stringified_hash[k]))
    end
  end
end
Also aliased as: deep_merge!
default(key = nil) click to toggle source

Borrowed from Merb's Mash object.

Parameters

key<Object>

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
def default(key = nil) 
  if key.is_a?(Symbol) && key?(key) 
    self[key] 
  else 
    super 
  end 
end
dup() click to toggle source

Duplicates the current mash as a new mash.

# File lib/mash.rb, line 99
def dup
  Mash.new(self)
end
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
def initializing_reader(key)
  return self[key] if key?(key)
  self[key] = Mash.new
end
inspect() click to toggle source

Prints out a pretty object-like string of the defined attributes.

# File lib/mash.rb, line 112
def inspect
  ret = "<#{self.class.to_s}"
  keys.sort.each do |key|
    ret << " #{key}=#{self[key].inspect}"
  end
  ret << ">"
  ret
end
Also aliased as: regular_inspect, to_s
key?(key) click to toggle source
# File lib/mash.rb, line 106
def key?(key)
  picky_key?(convert_key(key))
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_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 164
def to_hash
  Hash.new(default).merge(self)
end
to_s() click to toggle source
Alias for: inspect
update(other_hash) click to toggle source

Parameters

other_hash<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 151
def update(other_hash)
  other_hash.each_pair do |key, value|
    if respond_to?(convert_key(key) + "=")
      self.send(convert_key(key) + "=", convert_value(value))
    else
      regular_writer(convert_key(key), convert_value(value))
    end
  end
  self
end
Also aliased as: merge!

[Validate]

Generated with the Darkfish Rdoc Generator 2.