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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/chef/node/immutable_collections.rb', line 35

def convert_value(value)
  case value
  when Hash
    if ImmutableMash === value
      # Save an object creation
      value
    else
      ImmutableMash.new(value, __root__, __node__, __precedence__)
    end
  when Array
    if ImmutableArray === value
      # Save an object creation
      value
    else
      ImmutableArray.new(value, __root__, __node__, __precedence__)
    end
  else
    # We return any already frozen strings, since that's common over the course of a run.
    # Check `frozen?` first since that's faster than a Class comparison
    value.frozen? && String === value ? value : safe_dup(value).freeze
  end
end

#immutablize(value) ⇒ Object



58
59
60
# File 'lib/chef/node/immutable_collections.rb', line 58

def immutablize(value)
  convert_value(value)
end

#safe_dup(e) ⇒ Object

For elements like Fixnums, true, nil…



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

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