Module: Chef::Node::Immutablize

Included in:
Attribute, ImmutableArray, ImmutableMash
Defined in:
lib/chef/node/immutable_collections.rb

Instance Method Summary collapse

Instance Method Details

#convert_value(value) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/chef/node/immutable_collections.rb', line 34

def convert_value(value)
  # The order in this case statement is *important*.
  # ImmutableMash and ImmutableArray should be tested first,
  # as this saves unnecessary creation of intermediate objects
  case value
  when ImmutableMash, ImmutableArray
    value
  when Hash
    ImmutableMash.new(value, __root__, __node__, __precedence__)
  when Array
    ImmutableArray.new(value, __root__, __node__, __precedence__)
  else
    safe_dup(value).freeze
  end
end

#immutablize(value) ⇒ Object



50
51
52
# File 'lib/chef/node/immutable_collections.rb', line 50

def immutablize(value)
  convert_value(value)
end

#safe_dup(e) ⇒ Object

For elements like Fixnums, true, nil...



28
29
30
31
32
# File 'lib/chef/node/immutable_collections.rb', line 28

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