diff options
author | Patrick Willett <patrick@willett.com> | 2013-11-25 00:15:13 -0800 |
---|---|---|
committer | Patrick Willett <patrick@willett.com> | 2013-11-25 00:15:13 -0800 |
commit | a798eba1458ff8b48760e190e92d94ad8e87de34 (patch) | |
tree | 2880c26c18352925e2103061e34561da1c7e905f | |
parent | 3321005d0bd77e0811a2cea6dbbd302d8e86851f (diff) | |
download | hashie-a798eba1458ff8b48760e190e92d94ad8e87de34.tar.gz |
pass Hashie::Hash#to_hash's options to recursive calls
-rw-r--r-- | lib/hashie/hash.rb | 4 | ||||
-rw-r--r-- | spec/hashie/mash_spec.rb | 29 |
2 files changed, 30 insertions, 3 deletions
diff --git a/lib/hashie/hash.rb b/lib/hashie/hash.rb index c36bb91..43ff6c9 100644 --- a/lib/hashie/hash.rb +++ b/lib/hashie/hash.rb @@ -15,11 +15,11 @@ module Hashie k = options[:symbolize_keys] ? k.to_sym : k.to_s out[k] ||= [] self[k].each do |array_object| - out[k] << (Hash === array_object ? array_object.to_hash : array_object) + out[k] << (Hash === array_object ? array_object.to_hash(options) : array_object) end else k = options[:symbolize_keys] ? k.to_sym : k.to_s - out[k] = Hash === self[k] ? self[k].to_hash : self[k] + out[k] = Hash === self[k] ? self[k].to_hash(options) : self[k] end end out diff --git a/spec/hashie/mash_spec.rb b/spec/hashie/mash_spec.rb index 7842716..b094c81 100644 --- a/spec/hashie/mash_spec.rb +++ b/spec/hashie/mash_spec.rb @@ -360,7 +360,6 @@ describe Hashie::Mash do converted.to_hash["a"].first.is_a?(Hashie::Mash).should be_false converted.to_hash["a"].first.is_a?(Hash).should be_true converted.to_hash["a"].first["c"].first.is_a?(Hashie::Mash).should be_false - converted.to_hash({:symbolize_keys => true}).keys[0].should == :a end end @@ -409,4 +408,32 @@ describe Hashie::Mash do end end end + + describe "#to_hash" do + let(:hash) { { "outer" => { "inner" => 42 }, "testing" => [1, 2, 3] } } + let(:mash) { Hashie::Mash.new(hash) } + + it "returns a standard Hash" do + mash.to_hash.should be_a(::Hash) + end + + it "includes all keys" do + mash.to_hash.keys.should eql(%w(outer testing)) + end + + it "converts keys to symbols when symbolize_keys option is true" do + mash.to_hash(:symbolize_keys => true).keys.should include(:outer) + mash.to_hash(:symbolize_keys => true).keys.should_not include("outer") + end + + it "leaves keys as strings when symbolize_keys option is false" do + mash.to_hash(:symbolize_keys => false).keys.should include("outer") + mash.to_hash(:symbolize_keys => false).keys.should_not include(:outer) + end + + it "symbolizes keys recursively" do + mash.to_hash(:symbolize_keys => true)[:outer].keys.should include(:inner) + mash.to_hash(:symbolize_keys => true)[:outer].keys.should_not include("inner") + end + end end |