Class: Chef::Node::ImmutableArray
- Inherits:
-
Array
- Object
- Array
- Chef::Node::ImmutableArray
- Includes:
- Immutablize, Mixin::ImmutablizeArray, Mixin::StateTracking
- Defined in:
- lib/chef/node/immutable_collections.rb
Overview
ImmutableArray
ImmutableArray is used to implement Array collections when reading node attributes.
ImmutableArray acts like an ordinary Array, except:
-
Methods that mutate the array are overridden to raise an error, making the collection more or less immutable.
-
Since this class stores values computed from a parent Chef::Node::Attribute’s values, it overrides all reader methods to detect staleness and raise an error if accessed when stale.
Constant Summary
Constants included from Mixin::ImmutablizeArray
Mixin::ImmutablizeArray::ALLOWED_METHODS, Mixin::ImmutablizeArray::DISALLOWED_MUTATOR_METHODS
Instance Attribute Summary
Attributes included from Mixin::StateTracking
#__node__, #__path__, #__precedence__, #__root__
Instance Method Summary collapse
- #dup ⇒ Object
-
#initialize(array_data = []) ⇒ ImmutableArray
constructor
A new instance of ImmutableArray.
-
#safe_dup(e) ⇒ Object
For elements like Fixnums, true, nil…
- #to_a ⇒ Object (also: #to_array)
-
#to_yaml(*opts) ⇒ Object
As Psych module, not respecting ImmutableArray object So first convert it to Hash/Array then parse it to ‘.to_yaml`.
Methods included from Immutablize
Methods included from Mixin::StateTracking
Constructor Details
#initialize(array_data = []) ⇒ ImmutableArray
Returns a new instance of ImmutableArray.
67 68 69 70 71 |
# File 'lib/chef/node/immutable_collections.rb', line 67 def initialize(array_data = []) array_data.each do |value| internal_push(immutablize(value)) end end |
Instance Method Details
#dup ⇒ Object
80 81 82 |
# File 'lib/chef/node/immutable_collections.rb', line 80 def dup Array.new(map { |e| safe_dup(e) }) end |
#safe_dup(e) ⇒ Object
For elements like Fixnums, true, nil…
74 75 76 77 78 |
# File 'lib/chef/node/immutable_collections.rb', line 74 def safe_dup(e) e.dup rescue TypeError e end |
#to_a ⇒ Object Also known as: to_array
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/chef/node/immutable_collections.rb', line 84 def to_a Array.new(map do |v| case v when ImmutableArray v.to_a when ImmutableMash v.to_h else safe_dup(v) end end) end |
#to_yaml(*opts) ⇒ Object
As Psych module, not respecting ImmutableArray object So first convert it to Hash/Array then parse it to ‘.to_yaml`
101 102 103 |
# File 'lib/chef/node/immutable_collections.rb', line 101 def to_yaml(*opts) to_a.to_yaml(*opts) end |