Class Mash
In: lib/extlib/mash.rb
Parent: Hash
Mash Hash dot/f_15.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

@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/extlib/mash.rb, line 11
11:   def initialize(constructor = {})
12:     if constructor.is_a?(Hash)
13:       super()
14:       update(constructor)
15:     else
16:       super(constructor)
17:     end
18:   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/extlib/mash.rb, line 42
42:   def []=(key, value)
43:     regular_writer(convert_key(key), convert_value(value))
44:   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/extlib/mash.rb, line 25
25:   def default(key = nil)
26:     if key.is_a?(Symbol) && include?(key = key.to_s)
27:       self[key]
28:     else
29:       super
30:     end
31:   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/extlib/mash.rb, line 106
106:   def except(*keys)
107:     super(*keys.map {|k| convert_key(k)})
108:   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/extlib/mash.rb, line 74
74:   def fetch(key, *extras)
75:     super(convert_key(key), *extras)
76:   end

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

@return <TrueClass, FalseClass> True if the key exists in the mash.

[Source]

    # File lib/extlib/mash.rb, line 61
61:   def key?(key)
62:     super(convert_key(key))
63:   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/extlib/mash.rb, line 89
89:   def merge(hash)
90:     self.dup.update(hash)
91:   end

Used to provide the same interface as Hash.

@return <Mash> This mash unchanged.

[Source]

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

@return <Hash> The mash as a Hash with symbolized keys.

[Source]

     # File lib/extlib/mash.rb, line 116
116:   def symbolize_keys
117:     h = Hash.new(default)
118:     each { |key, val| h[key.to_sym] = val }
119:     h
120:   end

@return <Hash> The mash as a Hash with string keys.

[Source]

     # File lib/extlib/mash.rb, line 123
123:   def to_hash
124:     Hash.new(default).merge(self)
125:   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/extlib/mash.rb, line 51
51:   def update(other_hash)
52:     other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
53:     self
54:   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/extlib/mash.rb, line 82
82:   def values_at(*indices)
83:     indices.collect {|key| self[convert_key(key)]}
84:   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/extlib/mash.rb, line 135
135:   def convert_key(key)
136:     key.kind_of?(Symbol) ? key.to_s : key
137:   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/extlib/mash.rb, line 146
146:   def convert_value(value)
147:     if value.class == Hash
148:       value.to_mash
149:     elsif value.is_a?(Array)
150:       value.collect { |e| convert_value(e) }
151:     else
152:       value
153:     end
154:   end

[Validate]