Class: Mash

Inherits:
Hash
  • Object
show all
Defined in:
lib/chef/mash.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constructor = {}) ⇒ Mash


59
60
61
62
63
64
65
66
# File 'lib/chef/mash.rb', line 59

def initialize(constructor = {})
  if constructor.is_a?(Hash)
    super()
    update(constructor)
  else
    super(constructor)
  end
end

Class Method Details

.from_hash(hash) ⇒ Mash

The input Hash's default value is maintained


191
192
193
194
195
# File 'lib/chef/mash.rb', line 191

def self.from_hash(hash)
  mash = Mash.new(hash)
  mash.default = hash.default
  mash
end

Instance Method Details

#[]=(key, value) ⇒ Object

See Also:

  • #convert_key
  • #convert_value

104
105
106
# File 'lib/chef/mash.rb', line 104

def []=(key, value)
  regular_writer(convert_key(key), convert_value(value))
end

#default(key = nil) ⇒ Object


87
88
89
90
91
92
93
# File 'lib/chef/mash.rb', line 87

def default(key = nil)
  if key.is_a?(Symbol) && include?(key = key.to_s)
    self[key]
  else
    super
  end
end

#delete(key) ⇒ Object


157
158
159
# File 'lib/chef/mash.rb', line 157

def delete(key)
  super(convert_key(key))
end

#except(*keys) ⇒ Mash

Returns A new mash without the selected keys.

Examples:

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

168
169
170
# File 'lib/chef/mash.rb', line 168

def except(*keys)
  super(*keys.map { |k| convert_key(k) })
end

#fetch(key, *extras) ⇒ Object


136
137
138
# File 'lib/chef/mash.rb', line 136

def fetch(key, *extras)
  super(convert_key(key), *extras)
end

#initialize_copy(orig) ⇒ Object


71
72
73
74
75
76
77
78
79
80
# File 'lib/chef/mash.rb', line 71

def initialize_copy(orig)
  super  # Handle nested values

  each do |k, v|
    if v.kind_of?(Mash) || v.is_a?(Array)
      self[k] = v.dup
    end
  end
  self
end

#key?(key) ⇒ Boolean Also known as: include?, has_key?, member?


123
124
125
# File 'lib/chef/mash.rb', line 123

def key?(key)
  super(convert_key(key))
end

#merge(hash) ⇒ Mash


151
152
153
# File 'lib/chef/mash.rb', line 151

def merge(hash)
  dup.update(hash)
end

#regular_updateObject


96
# File 'lib/chef/mash.rb', line 96

alias_method :regular_update, :update

#regular_writerObject


95
# File 'lib/chef/mash.rb', line 95

alias_method :regular_writer, :[]=

#stringify_keys!Mash

Used to provide the same interface as Hash.


175
# File 'lib/chef/mash.rb', line 175

def stringify_keys!; self end

#symbolize_keysHash


178
179
180
181
182
# File 'lib/chef/mash.rb', line 178

def symbolize_keys
  h = Hash.new(default)
  each { |key, val| h[key.to_sym] = val }
  h
end

#to_hashHash


185
186
187
# File 'lib/chef/mash.rb', line 185

def to_hash
  Hash.new(default).merge(self)
end

#update(other_hash) ⇒ Mash Also known as: merge!


113
114
115
116
# File 'lib/chef/mash.rb', line 113

def update(other_hash)
  other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
  self
end

#values_at(*indices) ⇒ Array


144
145
146
# File 'lib/chef/mash.rb', line 144

def values_at(*indices)
  indices.collect { |key| self[convert_key(key)] }
end