diff options
-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 |