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:
-
Methods that mutate state are overridden to raise an error instead.
-
Methods that read from the collection are overridden so that they check if the
Chef::Node::Attribute
has been modified since an instance of this class was generated. An error is raised if the object detects that it is stale. -
Values can be accessed in attr_reader-like fashion via method_missing.
Public Class Methods
Source
# File lib/chef/node/immutable_collections.rb, line 156 def initialize(mash_data = {}) mash_data.each do |key, value| internal_set(key, value) end end
Public Instance Methods
Source
# File lib/chef/node/immutable_collections.rb, line 208 def [](*args) value = super value = value.call while value.is_a?(::Chef::DelayedEvaluator) value end
Source
# File lib/chef/node/immutable_collections.rb, line 169 def dup h = Mash.new each_pair do |k, v| h[k] = safe_dup(v) end h end
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
Source
# File lib/chef/node/immutable_collections.rb, line 152 def internal_set(key, value) regular_writer(key, convert_value(value)) end
this is for deep_merge usage, chef users must never touch this API @api private
Source
# File lib/chef/node/immutable_collections.rb, line 202 def safe_dup(e) e.dup rescue TypeError e end
For elements like Fixnums, true, nil…
Source
# File lib/chef/node/immutable_collections.rb, line 177 def to_h h = {} 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
Source
# File lib/chef/node/immutable_collections.rb, line 197 def to_yaml(*opts) to_h.to_yaml(*opts) end
As Psych module, not respecting ImmutableMash
object So first convert it to Hash/Array then parse it to ‘.to_yaml`