Class Mash
In: lib/ohai/mash.rb
Parent: Hash
Mash Hash dot/f_5.png

This class has dubious semantics and we only have it so that people can write params[:key] instead of params[‘key’].

Methods

External Aliases

[]= -> regular_writer

Public Class methods

@return [Mash] Convert a Hash into a Mash The input Hash‘s default value is maintained

[Source]

     # File lib/ohai/mash.rb, line 177
177:   def self.from_hash(hash)
178:     mash = Mash.new(hash)
179:     mash.default = hash.default
180:     mash
181:   end

@param constructor<Object>

  The default value for the mash. Defaults to an empty hash.

@details [Alternatives]

  If constructor is a Hash, a new mash will be created based on the keys of
  the hash and no default value will be set.

[Source]

    # File lib/ohai/mash.rb, line 59
59:   def initialize(constructor = {})
60:     if constructor.is_a?(Hash)
61:       super()
62:       update(constructor)
63:     else
64:       super(constructor)
65:     end
66:   end

Public Instance methods

@param key<Object> The key to set. @param value<Object>

  The value to set the key to.

@see Mash#convert_key @see Mash#convert_value

[Source]

    # File lib/ohai/mash.rb, line 90
90:   def []=(key, value)
91:     regular_writer(convert_key(key), convert_value(value))
92:   end

@param key<Object> The default value for the mash. Defaults to nil.

@details [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.

[Source]

    # File lib/ohai/mash.rb, line 73
73:   def default(key = nil)
74:     if key.is_a?(Symbol) && include?(key = key.to_s)
75:       self[key]
76:     else
77:       super
78:     end
79:   end

@param *rejected<Array[(String, Symbol)] The mash keys to exclude.

@return [Mash] A new mash without the selected keys.

@example

  { :one => 1, :two => 2, :three => 3 }.except(:one)
    #=> { "two" => 2, "three" => 3 }

[Source]

     # File lib/ohai/mash.rb, line 154
154:   def except(*keys)
155:     super(*keys.map {|k| convert_key(k)})
156:   end

@param key<Object> The key to fetch. This will be run through convert_key. @param *extras<Array> Default value.

@return [Object] The value at key or the default value.

[Source]

     # File lib/ohai/mash.rb, line 122
122:   def fetch(key, *extras)
123:     super(convert_key(key), *extras)
124:   end

@param key<Object> The key to check for. This will be run through convert_key.

@return [Boolean] True if the key exists in the mash.

[Source]

     # File lib/ohai/mash.rb, line 109
109:   def key?(key)
110:     super(convert_key(key))
111:   end

@param hash<Hash> The hash to merge with the mash.

@return [Mash] A new mash with the hash values merged in.

[Source]

     # File lib/ohai/mash.rb, line 137
137:   def merge(hash)
138:     self.dup.update(hash)
139:   end

Used to provide the same interface as Hash.

@return [Mash] This mash unchanged.

[Source]

     # File lib/ohai/mash.rb, line 161
161:   def stringify_keys!; self end

@return [Hash] The mash as a Hash with symbolized keys.

[Source]

     # File lib/ohai/mash.rb, line 164
164:   def symbolize_keys
165:     h = Hash.new(default)
166:     each { |key, val| h[key.to_sym] = val }
167:     h
168:   end

@return [Hash] The mash as a Hash with string keys.

[Source]

     # File lib/ohai/mash.rb, line 171
171:   def to_hash
172:     Hash.new(default).merge(self)
173:   end

@param other_hash<Hash>

  A hash to update values in the mash with. The keys and the values will be
  converted to Mash format.

@return [Mash] The updated mash.

[Source]

     # File lib/ohai/mash.rb, line 99
 99:   def update(other_hash)
100:     other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
101:     self
102:   end

@param *indices<Array>

  The keys to retrieve values for. These will be run through +convert_key+.

@return [Array] The values at each of the provided keys

[Source]

     # File lib/ohai/mash.rb, line 130
130:   def values_at(*indices)
131:     indices.collect {|key| self[convert_key(key)]}
132:   end

Protected Instance methods

@param key<Object> The key to convert.

@param [Object]

  The converted key. If the key was a symbol, it will be converted to a
  string.

@api private

[Source]

     # File lib/ohai/mash.rb, line 191
191:   def convert_key(key)
192:     key.kind_of?(Symbol) ? key.to_s : key
193:   end

@param value<Object> The value to convert.

@return [Object]

  The converted value. A Hash or an Array of hashes, will be converted to
  their Mash equivalents.

@api private

[Source]

     # File lib/ohai/mash.rb, line 202
202:   def convert_value(value)
203:     if value.class == Hash
204:       Mash.from_hash(value)
205:     elsif value.is_a?(Array)
206:       value.collect { |e| convert_value(e) }
207:     else
208:       value
209:     end
210:   end

[Validate]