Class: Chef::Node::ImmutableMash

Inherits:
Mash
  • Object
show all
Includes:
CommonAPI, Immutablize, Mixin::ImmutablizeHash, Mixin::StateTracking
Defined in:
lib/chef/node/immutable_collections.rb

Overview

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 overriden 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.

Constant Summary

Constants included from Mixin::ImmutablizeHash

Mixin::ImmutablizeHash::DISALLOWED_MUTATOR_METHODS

Instance Attribute Summary collapse

Attributes included from Mixin::StateTracking

#__node__, #__path__, #__precedence__, #__root__

Instance Method Summary collapse

Methods included from CommonAPI

#exist?, #read, #read!, #unlink, #unlink!, #write, #write!

Methods included from Immutablize

#convert_value, #safe_dup

Methods included from Mixin::StateTracking

#[]=

Methods inherited from Mash

#[]=, #default, #delete, #except, #fetch, from_hash, #initialize_copy, #key?, #merge, #regular_update, #regular_writer, #stringify_keys!, #symbolize_keys, #update, #values_at

Constructor Details

#initialize(mash_data = {}) ⇒ ImmutableMash

Returns a new instance of ImmutableMash.



247
248
249
# File 'lib/chef/node/immutable_collections.rb', line 247

def initialize(mash_data = {})
  # Immutable collections no longer have initialized state
end

Instance Attribute Details

#short_circuit_attr_levelsObject

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.



304
305
306
# File 'lib/chef/node/immutable_collections.rb', line 304

def short_circuit_attr_levels
  @short_circuit_attr_levels
end

Instance Method Details

#[](key) ⇒ Object



284
285
286
287
# File 'lib/chef/node/immutable_collections.rb', line 284

def [](key)
  ensure_generated_cache!
  super
end

#dupObject

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



258
259
260
261
262
263
264
# File 'lib/chef/node/immutable_collections.rb', line 258

def dup
  h = Mash.new
  each_pair do |k, v|
    h[k] = safe_dup(v)
  end
  h
end

#ensure_generated_cache!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.



298
299
300
301
# File 'lib/chef/node/immutable_collections.rb', line 298

def ensure_generated_cache!
  generate_cache unless @generated_cache
  @generated_cache = true
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.

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



243
244
245
# File 'lib/chef/node/immutable_collections.rb', line 243

def internal_set(key, value)
  regular_writer(key, convert_value(value, __path__ + [ key ]))
end

#resetObject



291
292
293
294
295
# File 'lib/chef/node/immutable_collections.rb', line 291

def reset
  @generated_cache = false
  @short_circuit_attr_level = nil
  internal_clear # redundant?
end

#to_hObject Also known as: to_hash



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/chef/node/immutable_collections.rb', line 266

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