diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2017-03-13 19:06:37 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2017-03-13 19:06:37 -0700 |
commit | 8cd2899fa40e21089d832b0a786b8fc4d335d738 (patch) | |
tree | 06b6345247d4c4d8cd2a07b7622c6ed23cc73cdd /lib/chef/node | |
parent | 987bcb5dd740637693dbb6723e8730f32da43696 (diff) | |
download | chef-8cd2899fa40e21089d832b0a786b8fc4d335d738.tar.gz |
Chef-13: properly deep dup Node#to_hash
previously to_hash allowed mutating non-container elements, now
they get properly dup'd.
fixes a 2.5 year old pending spec test.
also fills out the API so that there is to_h/to_hash/to_a/to_array
instead of the weird mix-and-match we had before.
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
Diffstat (limited to 'lib/chef/node')
-rw-r--r-- | lib/chef/node/immutable_collections.rb | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/lib/chef/node/immutable_collections.rb b/lib/chef/node/immutable_collections.rb index 12ee2e5dd8..08bd9c9c77 100644 --- a/lib/chef/node/immutable_collections.rb +++ b/lib/chef/node/immutable_collections.rb @@ -77,14 +77,16 @@ class Chef when ImmutableArray v.to_a when ImmutableMash - v.to_hash + v.to_h else - v + safe_dup(v) end end a end + alias_method :to_array, :to_a + # for consistency's sake -- integers 'converted' to integers def convert_key(key) key @@ -140,22 +142,31 @@ class Chef Mash.new(self) end - def to_hash + def to_h h = Hash.new each_pair do |k, v| h[k] = case v when ImmutableMash - v.to_hash + v.to_h when ImmutableArray v.to_a else - v + safe_dup(v) end end h end + alias_method :to_hash, :to_h + + # For elements like Fixnums, true, nil... + def safe_dup(e) + e.dup + rescue TypeError + e + end + prepend Chef::Node::Mixin::StateTracking prepend Chef::Node::Mixin::ImmutablizeHash end |