Module: DevSuite::Utils::Data::SearchFilter

Included in:
DevSuite::Utils::Data
Defined in:
lib/dev_suite/utils/data/search_filter.rb

Instance Method Summary collapse

Instance Method Details

#deep_filter_by_key_value(data, filter_key, filter_value) ⇒ Object

Recursively filter a nested hash or array based on a key-value condition



17
18
19
20
21
22
23
24
25
26
# File 'lib/dev_suite/utils/data/search_filter.rb', line 17

def deep_filter_by_key_value(data, filter_key, filter_value)
  case data
  when Hash
    filter_hash_by_key_value(data, filter_key, filter_value)
  when Array
    filter_array_by_key_value(data, filter_key, filter_value)
  else
    raise ArgumentError, "Unsupported data type: #{data.class}"
  end
end

#deep_find_by_key(data, search_key) ⇒ Object

Deep search for all occurrences of a key



8
9
10
11
12
13
14
# File 'lib/dev_suite/utils/data/search_filter.rb', line 8

def deep_find_by_key(data, search_key)
  result = []
  traverse_and_collect(data) do |key, value|
    result << value if key == search_key
  end
  result
end