Module: Trailblazer::Finder::Utils::Hash

Defined in:
lib/trailblazer/finder/utils/hash.rb

Class Method Summary collapse

Class Method Details

.deep_locate(comparator, object, result = []) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/trailblazer/finder/utils/hash.rb', line 10

def deep_locate(comparator, object, result = [])
  if object.is_a?(::Enumerable) && comparator.is_a?(::Proc)
    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) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
# File 'lib/trailblazer/finder/utils/hash.rb', line 21

def match_comparator?(value, comparator, object)
  key = nil
  key, value = value if object.is_a?(::Hash)
  comparator.call(key, value, object)
end

.remove_keys_from_hash(hash, keys) ⇒ Object



27
28
29
30
31
32
# File 'lib/trailblazer/finder/utils/hash.rb', line 27

def remove_keys_from_hash(hash, keys)
  hash.each do |key, _value|
    hash.delete(key) if keys.include?(key)
  end
  hash
end