Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/core/mod/hash.rb

Overview

Hash class additions

Over time these may get depreciated in favor of other solutions.

Instance Method Summary collapse

Instance Method Details

#search(search_key, options = {}) ⇒ Object

Search for a key (potentially recursively) in a given hash.

This method uses strict matching between string and symbol key types. They must be the same type or they will not match.

  • Parameters

    • String, Symbol

      search_key Key to search for in hash

    • Hash<String, Symbol|ANY>

      options Search options

      • Boolean

        :recurse Whether to recurse through sub hashes to find key value

      • Integer

        :recurse_level Maximum level to recurse into nested hashes or -1 for all

  • Returns

    • ANY

      Return value for search key if value found, nil otherwise

  • Errors

See also:

  • Nucleon::Util::Data::interpolate



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/core/mod/hash.rb', line 27

def search(search_key, options = {})
  config = Nucleon::Config.ensure(options)
  value  = nil

  recurse       = config.get(:recurse, false)
  recurse_level = config.get(:recurse_level, -1)

  self.each do |key, data|
    if key == search_key
      value = data

    elsif data.is_a?(Hash) &&
      recurse && (recurse_level == -1 || recurse_level > 0)

      recurse_level -= 1 unless recurse_level == -1
      value = value.search(search_key,
        Nucleon::Config.new(config).set(:recurse_level, recurse_level)
      )
    end
    break unless value.nil?
  end
  return value
end