Sha256: 9f872da6572840e37c3d7f34c532209d5ba880304b4084c68906cb46ccda733f

Contents?: true

Size: 1.29 KB

Versions: 12

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

# Override Hash class with convenience methods
class Hash
  # Transform each key in Hash to a symbol. Privately used by non-self method
  # @param [Object] value Value inside hash to transform keys under
  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

  # Take keys of hash and transform those to a symbols
  # @example
  #   hash = { 'a' => 1, 'b' => { c: 4 } }
  #   hash.transform_keys_to_symbols # => { a: 1, b: { c: 4 } }
  # @return [Hash] Hash will all keys converted to symbols
  def transform_keys_to_symbols
    each_with_object({}) { |(k, v), memo| memo[k.to_sym] = Hash.transform_keys_to_symbols(v); }
  end

  # Value present in nested Hash.
  # @example
  #   hash = { a: { b: 25 }, c: 3 }
  #   hash.include_value?(25) #=> true
  # @return [Boolean] Whether value is included in nested Hash
  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
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
soaspec-0.3.11 lib/soaspec/core_ext/hash.rb
soaspec-0.3.10 lib/soaspec/core_ext/hash.rb
soaspec-0.3.9 lib/soaspec/core_ext/hash.rb
soaspec-0.3.8 lib/soaspec/core_ext/hash.rb
soaspec-0.3.7 lib/soaspec/core_ext/hash.rb
soaspec-0.3.6 lib/soaspec/core_ext/hash.rb
soaspec-0.3.3 lib/soaspec/core_ext/hash.rb
soaspec-0.3.2 lib/soaspec/core_ext/hash.rb
soaspec-0.3.1 lib/soaspec/core_ext/hash.rb
soaspec-0.2.32 lib/soaspec/core_ext/hash.rb
soaspec-0.2.31 lib/soaspec/core_ext/hash.rb
soaspec-0.2.30 lib/soaspec/core_ext/hash.rb