class Chef::Node::ImmutableMash

ImmutableMash

ImmutableMash implements Hash/Dict behavior for reading values from node attributes.

ImmutableMash acts like a Mash (Hash that is indifferent to String or Symbol keys), with some important exceptions:

Public Class Methods

new(mash_data = {}) click to toggle source
# File lib/chef/node/immutable_collections.rb, line 132
def initialize(mash_data = {})
  mash_data.each do |key, value|
    internal_set(key, value)
  end
end

Public Instance Methods

dup() click to toggle source

NOTE: default and default= are likely to be pretty confusing. For a regular ruby Hash, they control what value is returned for, e.g.,

hash[:no_such_key] #=> hash.default

Of course, 'default' has a specific meaning in Chef-land

# File lib/chef/node/immutable_collections.rb, line 145
def dup
  h = Mash.new
  each_pair do |k, v|
    h[k] = safe_dup(v)
  end
  h
end
internal_set(key, value) click to toggle source

this is for deep_merge usage, chef users must never touch this API @api private

# File lib/chef/node/immutable_collections.rb, line 128
def internal_set(key, value)
  regular_writer(key, convert_value(value))
end
safe_dup(e) click to toggle source

For elements like Fixnums, true, nil…

# File lib/chef/node/immutable_collections.rb, line 172
def safe_dup(e)
  e.dup
rescue TypeError
  e
end
to_h() click to toggle source
# File lib/chef/node/immutable_collections.rb, line 153
def to_h
  h = Hash.new
  each_pair do |k, v|
    h[k] =
      case v
      when ImmutableMash
        v.to_h
      when ImmutableArray
        v.to_a
      else
        safe_dup(v)
      end
  end
  h
end
Also aliased as: to_hash
to_hash()
Alias for: to_h