module Hashie::Extensions::DeepLocate
@example Locate the hash containing a key
Hashie::Extensions::DeepLocate.deep_locate(:name, { user: { name: 'Ada' } }) # => [{ name: 'Ada' }]
Public Class Methods
deep_locate(comparator, object)
click to toggle source
The module level implementation of deep_locate, in case you do not want to include/extend the base data structure. For further examples please see deep_locate.
@example
books = [{ title: 'Ruby for beginners', pages: 120 }, { pages: 80 }] comparator = ->(key, _value, _object) { key == :title } Hashie::Extensions::DeepLocate.deep_locate(comparator, books) # => [{ title: 'Ruby for beginners', pages: 120 }]
# File lib/hashie/extensions/deep_locate.rb, line 16 def self.deep_locate(comparator, object) comparator = _construct_key_comparator(comparator, object) unless comparator.respond_to?(:call) _deep_locate(comparator, object) end
Private Class Methods
_construct_key_comparator(search_key, object)
click to toggle source
# File lib/hashie/extensions/deep_locate.rb, line 57 def self._construct_key_comparator(search_key, object) if (object.respond_to?(:indifferent_access?) && object.indifferent_access?) || activesupport_indifferent?(object) search_key = search_key.to_s end lambda do |non_callable_object| ->(key, _, _) { key == non_callable_object } end.call(search_key) end
_deep_locate(comparator, object, result = [])
click to toggle source
# File lib/hashie/extensions/deep_locate.rb, line 69 def self._deep_locate(comparator, object, result = []) if object.is_a?(::Enumerable) result.push object if object.any? { |value| _match_comparator?(value, comparator, object) } (object.respond_to?(:values) ? object.values : object.entries).each do |value| _deep_locate(comparator, value, result) end end result end
_match_comparator?(value, comparator, object)
click to toggle source
# File lib/hashie/extensions/deep_locate.rb, line 82 def self._match_comparator?(value, comparator, object) if object.is_a?(::Hash) key, value = value else key = nil end comparator.call(key, value, object) end
activesupport_indifferent?(object)
click to toggle source
# File lib/hashie/extensions/deep_locate.rb, line 93 def self.activesupport_indifferent?(object) defined?(::ActiveSupport::HashWithIndifferentAccess) && object.is_a?(::ActiveSupport::HashWithIndifferentAccess) end
Public Instance Methods
deep_locate(comparator)
click to toggle source
Performs a depth-first search on deeply nested data structures for a given comparator callable and returns each Enumerable, for which the callable returns true for at least one the its elements.
@example
books = [ { title: "Ruby for beginners", pages: 120 }, { title: "CSS for intermediates", pages: 80 }, { title: "Collection of ruby books", books: [ { title: "Ruby for the rest of us", pages: 576 } ] } ] books.extend(Hashie::Extensions::DeepLocate) ruby_titles = ->(key, value, _object) { key == :title && value.include?("Ruby") } books.deep_locate(ruby_titles) # => [{ title: 'Ruby for beginners', pages: 120 }, { title: 'Ruby for the rest of us', pages: 576 }] short_books = ->(key, value, _object) { key == :pages && value <= 120 } books.deep_locate(short_books) # => [{ title: 'Ruby for beginners', pages: 120 }, { title: 'CSS for intermediates', pages: 80 }]
# File lib/hashie/extensions/deep_locate.rb, line 53 def deep_locate(comparator) Hashie::Extensions::DeepLocate.deep_locate(comparator, self) end