diff options
author | gregory <greg2502@gmail.com> | 2014-06-12 10:36:59 -0700 |
---|---|---|
committer | gregory <greg2502@gmail.com> | 2014-06-12 11:38:51 -0700 |
commit | 6320564829563100db332c63f56d7e65644934a2 (patch) | |
tree | 4ce605bce33c7bfdb4d32685161ebbfc837acd58 | |
parent | 389e5404fd9adb3ba2e5c279c579d04c60a8a5f6 (diff) | |
download | hashie-6320564829563100db332c63f56d7e65644934a2.tar.gz |
call to_hash on object that respond to to_hash + specs
rename test class, wrap the tests on its own describe
update changelog
add spaces around hash
better changelog description
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | lib/hashie/hash.rb | 2 | ||||
-rw-r--r-- | spec/hashie/hash_spec.rb | 26 |
3 files changed, 28 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 15a7c9e..3b29de6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ **Note:** This version introduces several backward incompatible API changes. See [UPGRADING](UPGRADING.md) for details. +* [#169](https://github.com/intridea/hashie/pull/169): Hash#to_hash will also convert nested objects that implement to_hash - [@gregory](https://github.com/gregory). * [#150](https://github.com/intridea/hashie/pull/159): Handle nil intermediate object on deep fetch - [@stephenaument](https://github.com/stephenaument). * [#146](https://github.com/intridea/hashie/issues/146): Mash#respond_to? inconsistent with #method_missing and does not respond to #permitted? - [@dblock](https://github.com/dblock). * [#152](https://github.com/intridea/hashie/pull/152): Do not convert keys to String in Hashie::Dash and Hashie::Trash, use Hashie::Extensions::Dash::IndifferentAccess to achieve backward compatible behavior - [@dblock](https://github.com/dblock). diff --git a/lib/hashie/hash.rb b/lib/hashie/hash.rb index 956e2cb..87e1923 100644 --- a/lib/hashie/hash.rb +++ b/lib/hashie/hash.rb @@ -31,7 +31,7 @@ module Hashie out[assignment_key] << (Hash === array_object ? flexibly_convert_to_hash(array_object, options) : array_object) end else - out[assignment_key] = Hash === self[k] ? flexibly_convert_to_hash(self[k], options) : self[k] + out[assignment_key] = (Hash === self[k] || self[k].respond_to?(:to_hash)) ? flexibly_convert_to_hash(self[k], options) : self[k] end end out diff --git a/spec/hashie/hash_spec.rb b/spec/hashie/hash_spec.rb index 467731d..7d1f796 100644 --- a/spec/hashie/hash_spec.rb +++ b/spec/hashie/hash_spec.rb @@ -55,4 +55,30 @@ describe Hash do h[:key] = BareCustomMash.new expect { h.to_hash }.not_to raise_error end + + describe 'when the value is an object that respond_to to_hash' do + class ClassRespondsToHash + def to_hash(options = {}) + Hashie::Hash['a' => 'hey', b: 'bar', 123 => 'bob', 'array' => [1, 2, 3]].to_hash(options) + end + end + + it '#to_hash returns a hash with same keys' do + hash = Hashie::Hash['a' => 'hey', 123 => 'bob', 'array' => [1, 2, 3], subhash: ClassRespondsToHash.new] + stringified_hash = hash.to_hash + expect(stringified_hash).to eq('a' => 'hey', 123 => 'bob', 'array' => [1, 2, 3], subhash: { 'a' => 'hey', b: 'bar', 123 => 'bob', 'array' => [1, 2, 3] }) + end + + it '#to_hash with stringify_keys set to true returns a hash with stringified_keys' do + hash = Hashie::Hash['a' => 'hey', 123 => 'bob', 'array' => [1, 2, 3], subhash: ClassRespondsToHash.new] + symbolized_hash = hash.to_hash(stringify_keys: true) + expect(symbolized_hash).to eq('a' => 'hey', '123' => 'bob', 'array' => [1, 2, 3], 'subhash' => { 'a' => 'hey', 'b' => 'bar', '123' => 'bob', 'array' => [1, 2, 3] }) + end + + it '#to_hash with symbolize_keys set to true returns a hash with symbolized keys' do + hash = Hashie::Hash['a' => 'hey', 123 => 'bob', 'array' => [1, 2, 3], subhash: ClassRespondsToHash.new] + symbolized_hash = hash.to_hash(symbolize_keys: true) + expect(symbolized_hash).to eq(:a => 'hey', :"123" => 'bob', :array => [1, 2, 3], subhash: { :a => 'hey', :b => 'bar', :'123' => 'bob', :array => [1, 2, 3] }) + end + end end |