Class: Chef::Node::ImmutableArray

Inherits:
Array
  • Object
show all
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::DISALLOWED_MUTATOR_METHODS

Instance Attribute Summary

Attributes included from Mixin::StateTracking

#__node__, #__path__, #__precedence__, #__root__

Instance Method Summary collapse

Methods included from Immutablize

#immutablize

Methods included from Mixin::StateTracking

#[], #[]=

Constructor Details

#initialize(array_data = []) ⇒ ImmutableArray

Returns a new instance of ImmutableArray.



55
56
57
58
59
# File 'lib/chef/node/immutable_collections.rb', line 55

def initialize(array_data = [])
  array_data.each do |value|
    internal_push(immutablize(value))
  end
end

Instance Method Details

#convert_key(key) ⇒ Object

for consistency’s sake – integers ‘converted’ to integers



89
90
91
# File 'lib/chef/node/immutable_collections.rb', line 89

def convert_key(key)
  key
end

#dupObject



68
69
70
# File 'lib/chef/node/immutable_collections.rb', line 68

def dup
  Array.new(map { |e| safe_dup(e) })
end

#safe_dup(e) ⇒ Object

For elements like Fixnums, true, nil…



62
63
64
65
66
# File 'lib/chef/node/immutable_collections.rb', line 62

def safe_dup(e)
  e.dup
rescue TypeError
  e
end

#to_aObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/chef/node/immutable_collections.rb', line 72

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