Class: Chef::Node::ImmutableMash
- 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 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.
Constant Summary
Constants included from Mixin::ImmutablizeHash
Mixin::ImmutablizeHash::ALLOWED_METHODS, Mixin::ImmutablizeHash::DISALLOWED_MUTATOR_METHODS
Instance Attribute Summary
Attributes included from Mixin::StateTracking
#__node__, #__path__, #__precedence__, #__root__
Instance Method Summary collapse
- #[](*args) ⇒ Object
-
#dup ⇒ Object
NOTE: #default and #default= are likely to be pretty confusing.
-
#initialize(mash_data = {}) ⇒ ImmutableMash
constructor
A new instance of ImmutableMash.
-
#internal_set(key, value) ⇒ Object
private
this is for deep_merge usage, chef users must never touch this API.
-
#safe_dup(e) ⇒ Object
For elements like Fixnums, true, nil…
- #to_h ⇒ Object (also: #to_hash)
-
#to_yaml(*opts) ⇒ Object
As Psych module, not respecting ImmutableMash object So first convert it to Hash/Array then parse it to ‘.to_yaml`.
Methods included from CommonAPI
#exist?, #read, #read!, #unlink, #unlink!, #write, #write!
Methods included from Immutablize
Methods included from Mixin::StateTracking
Constructor Details
#initialize(mash_data = {}) ⇒ ImmutableMash
Returns a new instance of ImmutableMash.
156 157 158 159 160 |
# 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 |
Instance Method Details
#[](*args) ⇒ Object
208 209 210 211 212 |
# File 'lib/chef/node/immutable_collections.rb', line 208 def [](*args) value = super value = value.call while value.is_a?(::Chef::DelayedEvaluator) value end |
#dup ⇒ Object
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
169 170 171 172 173 174 175 |
# 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 |
#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
152 153 154 |
# File 'lib/chef/node/immutable_collections.rb', line 152 def internal_set(key, value) regular_writer(key, convert_value(value)) end |
#safe_dup(e) ⇒ Object
For elements like Fixnums, true, nil…
202 203 204 205 206 |
# File 'lib/chef/node/immutable_collections.rb', line 202 def safe_dup(e) e.dup rescue TypeError e end |
#to_h ⇒ Object Also known as: to_hash
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# 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 |
#to_yaml(*opts) ⇒ Object
As Psych module, not respecting ImmutableMash object So first convert it to Hash/Array then parse it to ‘.to_yaml`
197 198 199 |
# File 'lib/chef/node/immutable_collections.rb', line 197 def to_yaml(*opts) to_h.to_yaml(*opts) end |