Class: Chef::Node::VividMash

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

Overview

VividMash

VividMash is identical to a Mash, with a few exceptions:

  • It has a reference to the root Chef::Node::Attribute to which it belongs, and will trigger cache invalidation on that object when mutated.

  • It auto-vivifies, that is a reference to a missing element will result in the creation of a new VividMash for that key. (This only works when using the element reference method, ‘[]` – other methods, such as #fetch, work as normal).

  • attr_accessor style element set and get are supported via method_missing

Constant Summary collapse

MUTATOR_METHODS =

Methods that mutate a VividMash. Each of them is overridden so that it also invalidates the cached merged_attributes on the root Attribute object.

Chef::Node::Mixin::ImmutablizeHash::DISALLOWED_MUTATOR_METHODS - %i{write write! unlink unlink!}

Instance Attribute Summary

Attributes included from Mixin::StateTracking

#__node__, #__path__, #__precedence__, #__root__

Instance Method Summary collapse

Methods included from CommonAPI

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

Constructor Details

#initialize(data = {}) ⇒ VividMash

Returns a new instance of VividMash.



130
131
132
# File 'lib/chef/node/attribute_collections.rb', line 130

def initialize(data = {})
  super(data)
end

Instance Method Details

#[](key) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'lib/chef/node/attribute_collections.rb', line 134

def [](key)
  value = super
  if !key?(key)
    value = self.class.new({}, __root__)
    self[key] = value
  else
    value
  end
end

#[]=(key, value) ⇒ Object



144
145
146
147
148
# File 'lib/chef/node/attribute_collections.rb', line 144

def []=(key, value)
  ret = super
  send_reset_cache(__path__, key)
  ret # rubocop:disable Lint/Void
end

#convert_key(key) ⇒ Object



152
153
154
# File 'lib/chef/node/attribute_collections.rb', line 152

def convert_key(key)
  super
end

#convert_value(value) ⇒ Object

Mash uses #convert_value to mashify values on input. We override it here to convert hash or array values to VividMash or AttrArray for consistency and to ensure that the added parts of the attribute tree will have the correct cache invalidation behavior.



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/chef/node/attribute_collections.rb', line 160

def convert_value(value)
  case value
  when VividMash
    value
  when AttrArray
    value
  when Hash
    VividMash.new(value, __root__, __node__, __precedence__)
  when Array
    AttrArray.new(value, __root__, __node__, __precedence__)
  else
    value
  end
end

#delete(key, &block) ⇒ Object



125
126
127
128
# File 'lib/chef/node/attribute_collections.rb', line 125

def delete(key, &block)
  send_reset_cache(__path__, key)
  super
end

#dupObject



175
176
177
# File 'lib/chef/node/attribute_collections.rb', line 175

def dup
  Mash.new(self)
end

#to_yaml(*opts) ⇒ Object



179
180
181
# File 'lib/chef/node/attribute_collections.rb', line 179

def to_yaml(*opts)
  to_h.to_yaml(*opts)
end