Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/soaspec/core_ext/hash.rb

Overview

Override Hash class with convenience methods

Direct Known Subclasses

IndifferentHash

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.transform_keys_to_symbols(value) ⇒ Object

Transform each key in Hash to a symbol. Privately used by non-self method

Parameters:

  • value (Object)

    Value inside hash to transform keys under



5
6
7
8
9
10
# File 'lib/soaspec/core_ext/hash.rb', line 5

def self.transform_keys_to_symbols(value)
  return value unless value.is_a?(Hash)

  hash = value.each_with_object({}) { |(k, v), memo| memo[k.to_sym] = Hash.transform_keys_to_symbols(v); }
  hash
end

Instance Method Details

#include_value?(value) ⇒ Boolean

Value present in nested Hash.

Returns:

  • (Boolean)

    Whether value is included in nested Hash



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/soaspec/core_ext/hash.rb', line 19

def include_value?(value)
  each_value do |v|
    return true if v == value
    next unless v.is_a? Hash

    v.each_value do |v|
      return true if v == value
      next unless v.is_a? Hash

      v.each_value do |v|
        return true if v == value
      end
    end
  end
  false
end

#transform_keys_to_symbolsObject

Take keys of hash and transform those to a symbols



13
14
15
# File 'lib/soaspec/core_ext/hash.rb', line 13

def transform_keys_to_symbols
  each_with_object({}) { |(k, v), memo| memo[k.to_sym] = Hash.transform_keys_to_symbols(v); }
end