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 instead of params.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constructor = {}) ⇒ Mash

Returns a new instance of Mash.

Parameters:

  • constructor (Object) (defaults to: {})

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



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

Returns:

  • (Mash)

    Convert a Hash into a Mash



197
198
199
200
201
# File 'lib/chef/mash.rb', line 197

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

Instance Method Details

#[]=(key, value) ⇒ Object

Parameters:

  • key (Object)

    The key to set.

  • value (Object)

    The value to set the key to.

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

Parameters:

  • key (Object) (defaults to: nil)

    The default value for the mash. Defaults to nil.



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

Parameters:

  • key (Object)

    The key to delete from the mash.\



163
164
165
# File 'lib/chef/mash.rb', line 163

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 }

Parameters:

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

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

Returns:

  • (Mash)

    A new mash without the selected keys.



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

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

#fetch(key, *extras) ⇒ Object

Returns The value at key or the default value.

Parameters:

  • key (Object)

    The key to fetch. This will be run through convert_key.

  • *extras (Array)

    Default value.

Returns:

  • (Object)

    The value at key or the default value.



142
143
144
# File 'lib/chef/mash.rb', line 142

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

#initialize_copy(orig) ⇒ Object

Returns A new copied Mash.

Parameters:

  • orig (Object)

    Mash being copied

Returns:

  • (Object)

    A new copied Mash



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

#internal_set(key, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

internal API for use by Chef’s deep merge cache



110
111
112
# File 'lib/chef/mash.rb', line 110

def internal_set(key, value)
  regular_writer(key, convert_value(value))
end

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

Returns True if the key exists in the mash.

Parameters:

  • key (Object)

    The key to check for. This will be run through convert_key.

Returns:

  • (Boolean)

    True if the key exists in the mash.



129
130
131
# File 'lib/chef/mash.rb', line 129

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

#merge(hash) ⇒ Mash

Returns A new mash with the hash values merged in.

Parameters:

  • hash (Hash)

    The hash to merge with the mash.

Returns:

  • (Mash)

    A new mash with the hash values merged in.



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

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.

Returns:

  • (Mash)

    This mash unchanged.



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

def stringify_keys!; self end

#symbolize_keysHash

Returns The mash as a Hash with symbolized keys.

Returns:

  • (Hash)

    The mash as a Hash with symbolized keys.



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

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

#to_hashHash

Returns The mash as a Hash with string keys.

Returns:

  • (Hash)

    The mash as a Hash with string keys.



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

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

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

Returns The updated mash.

Parameters:

  • other_hash (Hash)

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

Returns:

  • (Mash)

    The updated mash.



119
120
121
122
# File 'lib/chef/mash.rb', line 119

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

#values_at(*indices) ⇒ Array

Returns The values at each of the provided keys.

Parameters:

  • *indices (Array)

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

Returns:

  • (Array)

    The values at each of the provided keys



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

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