Parent

Included Modules

Hashie::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, default = 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/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

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/hashie/mash.rb, line 67
67:     def [](key)
68:       regular_reader(convert_key(key))
69:     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/hashie/mash.rb, line 98
 98:     def deep_merge(other_hash)
 99:       dup.deep_merge!(other_hash)
100:     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/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
Also aliased as: deep_merge!, update
dup() click to toggle source

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
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/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
key?(key) click to toggle source
    # File lib/hashie/mash.rb, line 92
92:     def key?(key)
93:       super(convert_key(key))
94:     end
merge!(other_hash) click to toggle source
Alias for: update
method_missing(method_name, *args, &blk) click to toggle source
     # 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
regular_dup() click to toggle source
Alias for: dup
regular_reader(key) click to toggle source
Alias for: []
respond_to?(method_name) click to toggle source

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
update(other_hash) click to toggle source
Also aliased as: merge!
Alias for: deep_update

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.