diff options
author | brianbianco <brian.bianco@gmail.com> | 2013-03-05 10:38:47 -0500 |
---|---|---|
committer | Bryan McLellan <btm@getchef.com> | 2014-03-28 16:38:44 -0700 |
commit | 4cad176f2912475c7e36f3e86eea42fea4d51a35 (patch) | |
tree | 1f70ebf580e6f9a06787e4338422055139903c76 | |
parent | 433d8588d100dd7e99cb7a9eba3d869fe8a4d25b (diff) | |
download | chef-4cad176f2912475c7e36f3e86eea42fea4d51a35.tar.gz |
[CHEF-3953] ImmutableMash and ImmutableArray should implement to_hash and to_a respectively
- ImmutableMash now implements a to_hash method. This recurses through the ImmutableMash and returns a fully mutable Hash
- ImmutableArray now implements a to_a method. This recurses through the ImmutableArray and returns a fully mutable Array
- Unit tests added for both of the above methods
-rw-r--r-- | lib/chef/node/immutable_collections.rb | 24 | ||||
-rw-r--r-- | spec/unit/node/immutable_collections_spec.rb | 23 |
2 files changed, 47 insertions, 0 deletions
diff --git a/lib/chef/node/immutable_collections.rb b/lib/chef/node/immutable_collections.rb index 63ac2b4c6b..6dc40ce949 100644 --- a/lib/chef/node/immutable_collections.rb +++ b/lib/chef/node/immutable_collections.rb @@ -96,6 +96,18 @@ class Chef Array.new(map {|e| safe_dup(e)}) end + def to_a + a = Array.new + each do |v| + if v.kind_of?(Chef::Node::ImmutableArray) + a.push v.to_a + else + a.push v + end + end + a + end + end # == ImmutableMash @@ -187,6 +199,18 @@ class Chef Mash.new(self) end + def to_hash + h = Hash.new + each_pair do |k,v| + if v.kind_of?(Chef::Node::ImmutableMash) + h[k] = v.to_hash + else + h[k] = v + end + end + h + end + end end diff --git a/spec/unit/node/immutable_collections_spec.rb b/spec/unit/node/immutable_collections_spec.rb index f30c1970f7..b690beb4c5 100644 --- a/spec/unit/node/immutable_collections_spec.rb +++ b/spec/unit/node/immutable_collections_spec.rb @@ -54,6 +54,17 @@ describe Chef::Node::ImmutableMash do @immutable_mash[:top_level_4][:level2].should be_a(Chef::Node::ImmutableMash) end + it "converts an immutable nested mash to a new mutable hash" do + orig = Mash.new(@data_in) + copy = orig.dup + copy = copy.to_hash + copy.should be_a(Hash) + copy['top_level_4']['level2'].should be_a(Hash) + copy.should == orig + copy['top_level_4']['level2'] = "A different value." + copy.should_not == orig + end + [ :[]=, @@ -87,6 +98,7 @@ describe Chef::Node::ImmutableArray do before do @immutable_array = Chef::Node::ImmutableArray.new(%w[foo bar baz] + Array(1..3) + [nil, true, false, [ "el", 0, nil ] ]) + @immutable_nested_array = Chef::Node::ImmutableArray.new(["level1",@immutable_array]) end ## @@ -139,5 +151,16 @@ describe Chef::Node::ImmutableArray do mutable[0] = :value mutable[0].should == :value end + + it "converts an immutable nested array to a new mutable array" do + copy = @immutable_nested_array.dup + copy = copy.to_a + copy.should be_a(Array) + copy[1].should be_a(Array) + copy.should == @immutable_nested_array + copy[0] = "A different value." + copy.should_not == @immutable_nested_array + end + end |