diff options
author | Daniel Doubrovkine (dB.) @dblockdotorg <dblock@dblock.org> | 2016-10-19 09:05:28 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-19 09:05:28 -0400 |
commit | 9c38812a3e54e1313e826d945e03a311367ddb1e (patch) | |
tree | 9fe2be18786db448422078fb008d76d1bed71ecf | |
parent | 0cc606f4c6c67a82ba7762b44ecc24672da51838 (diff) | |
parent | 4c4870425169a70b3edcf1cba88165aadc0fac63 (diff) | |
download | hashie-9c38812a3e54e1313e826d945e03a311367ddb1e.tar.gz |
Merge pull request #378 from sazor/deep-find-all-fix
Deep find all goes inside all nested hashes
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | lib/hashie/extensions/deep_locate.rb | 7 | ||||
-rw-r--r-- | spec/hashie/extensions/deep_find_spec.rb | 25 |
3 files changed, 29 insertions, 4 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 31531ef..60cd69a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ scheme are considered to be bugs. * [#369](https://github.com/intridea/hashie/pull/369): If a translation for a property exists when using IndifferentAccess and IgnoreUndeclared, use the translation to find the property - [@whitethunder](https://github.com/whitethunder). * [#376](https://github.com/intridea/hashie/pull/376): Leave string index unchanged if it can't be converted to integer for Array#dig - [@sazor](https://github.com/sazor). * [#377](https://github.com/intridea/hashie/pull/377): Dont use Rubygems to check ruby version - [@sazor](https://github.com/sazor). +* [#378](https://github.com/intridea/hashie/pull/378): Deep find all searches inside all nested hashes - [@sazor](https://github.com/sazor). ### Security diff --git a/lib/hashie/extensions/deep_locate.rb b/lib/hashie/extensions/deep_locate.rb index 8416c3f..930513a 100644 --- a/lib/hashie/extensions/deep_locate.rb +++ b/lib/hashie/extensions/deep_locate.rb @@ -76,10 +76,9 @@ module Hashie if object.is_a?(::Enumerable) if object.any? { |value| _match_comparator?(value, comparator, object) } result.push object - else - (object.respond_to?(:values) ? object.values : object.entries).each do |value| - _deep_locate(comparator, value, result) - end + end + (object.respond_to?(:values) ? object.values : object.entries).each do |value| + _deep_locate(comparator, value, result) end end diff --git a/spec/hashie/extensions/deep_find_spec.rb b/spec/hashie/extensions/deep_find_spec.rb index 0b1401c..b3cc57e 100644 --- a/spec/hashie/extensions/deep_find_spec.rb +++ b/spec/hashie/extensions/deep_find_spec.rb @@ -42,6 +42,31 @@ describe Hashie::Extensions::DeepFind do it 'returns nil if it does not find any matches' do expect(instance.deep_find_all(:wahoo)).to be_nil end + + context 'when match value is hash itself' do + let(:hash) do + { + title: { + type: :string + }, + library: { + books: [ + { title: 'Call of the Wild' }, + { title: 'Moby Dick' } + ], + shelves: nil, + location: { + address: '123 Library St.', + title: 'Main Library' + } + } + } + end + + it 'detects all values from a nested hash' do + expect(instance.deep_find_all(:title)).to eq([{ type: :string }, 'Call of the Wild', 'Moby Dick', 'Main Library']) + end + end end context 'on an ActiveSupport::HashWithIndifferentAccess' do |