Class: Chef::Node::ImmutableArray
- Inherits:
-
Array
- Object
- Array
- Chef::Node::ImmutableArray
- Includes:
- Immutablize
- 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 collapse
- DISALLOWED_MUTATOR_METHODS =
A list of methods that mutate Array. Each of these is overridden to raise an error, making this instances of this class more or less immutable.
[ :<<, :[]=, :clear, :collect!, :compact!, :default=, :default_proc=, :delete, :delete_at, :delete_if, :fill, :flatten!, :insert, :keep_if, :map!, :merge!, :pop, :push, :update, :reject!, :reverse!, :replace, :select!, :shift, :slice!, :sort!, :sort_by!, :uniq!, :unshift, ]
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
Methods included from Immutablize
Constructor Details
#initialize(array_data) ⇒ ImmutableArray
Returns a new instance of ImmutableArray.
69 70 71 72 73 |
# File 'lib/chef/node/immutable_collections.rb', line 69 def initialize(array_data) array_data.each do |value| internal_push(immutablize(value)) end end |
Instance Method Details
#dup ⇒ Object
90 91 92 |
# File 'lib/chef/node/immutable_collections.rb', line 90 def dup Array.new(map { |e| safe_dup(e) }) end |
#safe_dup(e) ⇒ Object
For elements like Fixnums, true, nil…
84 85 86 87 88 |
# File 'lib/chef/node/immutable_collections.rb', line 84 def safe_dup(e) e.dup rescue TypeError e end |
#to_a ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/chef/node/immutable_collections.rb', line 94 def to_a a = Array.new each do |v| a << case v when ImmutableArray v.to_a when ImmutableMash v.to_hash else v end end a end |