Class: Chef::Node::VividMash
- 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.
[ :clear, :delete_if, :keep_if, :merge!, :update, :reject!, :replace, :select!, :shift, ]
Instance Attribute Summary
Attributes included from Mixin::StateTracking
#__node__, #__path__, #__precedence__, #__root__
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #convert_key(key) ⇒ Object
-
#convert_value(value) ⇒ Object
Mash uses #convert_value to mashify values on input.
-
#delete(key, &block) ⇒ Object
For all of the mutating methods on Mash, override them so that they also invalidate the cached
merged_attributeson the root Attribute object. - #dup ⇒ Object
-
#initialize(data = {}) ⇒ VividMash
constructor
A new instance of VividMash.
- #method_missing(symbol, *args) ⇒ Object
Methods included from CommonAPI
#exist?, #read, #read!, #unlink, #unlink!, #write, #write!
Methods inherited from Mash
#default, #except, #fetch, from_hash, #initialize_copy, #key?, #merge, #regular_update, #regular_writer, #stringify_keys!, #symbolize_keys, #to_hash, #update, #values_at
Constructor Details
#initialize(data = {}) ⇒ VividMash
Returns a new instance of VividMash.
161 162 163 |
# File 'lib/chef/node/attribute_collections.rb', line 161 def initialize(data = {}) super(data) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/chef/node/attribute_collections.rb', line 183 def method_missing(symbol, *args) # Calling `puts arg` implicitly calls #to_ary on `arg`. If `arg` does # not implement #to_ary, ruby recognizes it as a single argument, and # if it returns an Array, then ruby prints each element. If we don't # account for that here, we'll auto-vivify a VividMash for the key # :to_ary which creates an unwanted key and raises a TypeError. if symbol == :to_ary super elsif args.empty? self[symbol] elsif symbol.to_s =~ /=$/ key_to_set = symbol.to_s[/^(.+)=$/, 1] self[key_to_set] = (args.length == 1 ? args[0] : args) else raise NoMethodError, "Undefined node attribute or method `#{symbol}' on `node'. To set an attribute, use `#{symbol}=value' instead." end end |
Instance Method Details
#[](key) ⇒ Object
165 166 167 168 169 170 171 172 173 |
# File 'lib/chef/node/attribute_collections.rb', line 165 def [](key) value = super if !key?(key) value = self.class.new({}, __root__) self[key] = value else value end end |
#[]=(key, value) ⇒ Object
175 176 177 178 179 |
# File 'lib/chef/node/attribute_collections.rb', line 175 def []=(key, value) ret = super send_reset_cache(__path__, key) ret end |
#convert_key(key) ⇒ Object
201 202 203 |
# File 'lib/chef/node/attribute_collections.rb', line 201 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.
209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/chef/node/attribute_collections.rb', line 209 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
For all of the mutating methods on Mash, override them so that they also invalidate the cached merged_attributes on the root Attribute object.
149 150 151 152 |
# File 'lib/chef/node/attribute_collections.rb', line 149 def delete(key, &block) send_reset_cache(__path__, key) super end |
#dup ⇒ Object
224 225 226 |
# File 'lib/chef/node/attribute_collections.rb', line 224 def dup Mash.new(self) end |