summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Doubrovkine (dB.) @dblockdotorg <dblock@dblock.org>2014-07-09 15:32:52 -0400
committerDaniel Doubrovkine (dB.) @dblockdotorg <dblock@dblock.org>2014-07-09 15:32:52 -0400
commitf4eabadc7cfb57d323f7cc7c92f5a2c53c312da3 (patch)
treead837dcbc6efb458e92c881d36033bd585d15f3e
parent85cf9f283b9dd97219a1910a553c072e39211188 (diff)
parent5fadec46475aa012fe78146ad817f1c6234b8fd1 (diff)
downloadhashie-f4eabadc7cfb57d323f7cc7c92f5a2c53c312da3.tar.gz
Merge pull request #192 from dblock/stringify-keys-recursively
Fixed StringifyKeys#stringify_keys! to recursively stringify keys of embedded ::Hash types.
-rw-r--r--CHANGELOG.md2
-rw-r--r--lib/hashie/extensions/stringify_keys.rb6
-rw-r--r--spec/hashie/hash_spec.rb4
3 files changed, 10 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b6d0458..c03727e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,8 @@
* [#184](https://github.com/intridea/hashie/pull/184): Allow ranges on Rash to match all Numeric types - [@medcat](https://github.com/medcat).
* [#187](https://github.com/intridea/hashie/pull/187): Automatically require version - [@medcat](https://github.com/medcat).
* [#190](https://github.com/intridea/hashie/issues/190): Fixed `coerce_key` with `from` Trash feature and Coercion extension - [@gregory](https://github.com/gregory).
+* [#192](https://github.com/intridea/hashie/pull/192): Fixed StringifyKeys#stringify_keys! to recursively stringify keys of embedded ::Hash types - [@dblock](https://github.com/dblock).
+
* Your contribution here.
## 3.1 (6/25/2014)
diff --git a/lib/hashie/extensions/stringify_keys.rb b/lib/hashie/extensions/stringify_keys.rb
index fe00951..ba3e6a8 100644
--- a/lib/hashie/extensions/stringify_keys.rb
+++ b/lib/hashie/extensions/stringify_keys.rb
@@ -35,6 +35,12 @@ module Hashie
object
elsif object.respond_to?(:stringify_keys!)
object.stringify_keys!
+ elsif ::Hash === object
+ object.keys.each do |k|
+ stringify_keys_recursively!(object[k])
+ object[k.to_s] = object.delete(k)
+ end
+ object
else
object
end
diff --git a/spec/hashie/hash_spec.rb b/spec/hashie/hash_spec.rb
index 7d1f796..fa1c878 100644
--- a/spec/hashie/hash_spec.rb
+++ b/spec/hashie/hash_spec.rb
@@ -13,10 +13,10 @@ describe Hash do
expect(hash).to eq Hashie::Hash['a' => 'hey', '123' => 'bob']
end
- it '#stringify_keys! turns all keys into strings non-recursively' do
+ it '#stringify_keys! turns all keys into strings recursively' do
hash = Hashie::Hash[:a => 'hey', 123 => { 345 => 'hey' }]
hash.stringify_keys!
- expect(hash).to eq Hashie::Hash['a' => 'hey', '123' => { 345 => 'hey' }]
+ expect(hash).to eq Hashie::Hash['a' => 'hey', '123' => { '345' => 'hey' }]
end
it '#stringify_keys returns a hash with stringified keys' do