diff options
author | dblock <dblock@dblock.org> | 2014-03-30 20:25:03 -0400 |
---|---|---|
committer | dblock <dblock@dblock.org> | 2014-03-30 20:25:03 -0400 |
commit | 20733c83cb489b33ded89f535786601930067a1d (patch) | |
tree | 9cd9d8caf0d3915bc3583612173269980c66b06c | |
parent | e1256f9a687f2e411d832514b8d12673ba79277b (diff) | |
parent | 4302d2de94df61e5bb12e8076e1108f2dd641e17 (diff) | |
download | hashie-20733c83cb489b33ded89f535786601930067a1d.tar.gz |
Merged.
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | lib/hashie/hash.rb | 12 | ||||
-rw-r--r-- | spec/hashie/hash_spec.rb | 12 |
3 files changed, 19 insertions, 6 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index a5c13bd..224996d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ * [#120](https://github.com/intridea/hashie/pull/120): Pass options to recursive to_hash calls - [@pwillett](https://github.com/pwillett). * [#113](https://github.com/intridea/hashie/issues/113): Fixed Hash#merge with Hashie::Dash - [@spencer1248](https://github.com/spencer1248). * [#99](https://github.com/intridea/hashie/issues/99): Hash#deep_merge raises errors when it encounters integers - [@defsprite](https://github.com/defsprite). +* [#133](https://github.com/intridea/hashie/pull/133): Fixed Hash##to_hash with symbolize_keys - [@mhuggins](https://github.com/mhuggins). ## 2.0.5 diff --git a/lib/hashie/hash.rb b/lib/hashie/hash.rb index 43ff6c9..164b0ae 100644 --- a/lib/hashie/hash.rb +++ b/lib/hashie/hash.rb @@ -7,19 +7,19 @@ module Hashie class Hash < ::Hash include HashExtensions - # Converts a mash back to a hash (with stringified keys) + # Converts a mash back to a hash (with stringified or symbolized keys) def to_hash(options={}) out = {} keys.each do |k| + assignment_key = k.to_s + assignment_key = assignment_key.to_sym if options[:symbolize_keys] if self[k].is_a?(Array) - k = options[:symbolize_keys] ? k.to_sym : k.to_s - out[k] ||= [] + out[assignment_key] ||= [] self[k].each do |array_object| - out[k] << (Hash === array_object ? array_object.to_hash(options) : array_object) + out[assignment_key] << (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(options) : self[k] + out[assignment_key] = Hash === self[k] ? self[k].to_hash(options) : self[k] end end out diff --git a/spec/hashie/hash_spec.rb b/spec/hashie/hash_spec.rb index 30571d2..dd66e03 100644 --- a/spec/hashie/hash_spec.rb +++ b/spec/hashie/hash_spec.rb @@ -19,4 +19,16 @@ describe Hash do hash.should == Hashie::Hash[:a => "hey", 123 => "bob"] stringified_hash.should == Hashie::Hash["a" => "hey", "123" => "bob"] end + + it "#to_hash should return a hash with stringified keys" do + hash = Hashie::Hash["a" => "hey", 123 => "bob", "array" => [1, 2, 3]] + stringified_hash = hash.to_hash + stringified_hash.should == {"a" => "hey", "123" => "bob", "array" => [1, 2, 3]} + end + + it "#to_hash with symbolize_keys set to true should return a hash with symbolized keys" do + hash = Hashie::Hash["a" => "hey", 123 => "bob", "array" => [1, 2, 3]] + symbolized_hash = hash.to_hash(:symbolize_keys => true) + symbolized_hash.should == {:a => "hey", :"123" => "bob", :array => [1, 2, 3]} + end end
\ No newline at end of file |