Module: Micro::Case::Utils::Hashes

Defined in:
lib/micro/case/utils.rb

Class Method Summary collapse

Class Method Details

.hash_respond_to?(hash, method) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/micro/case/utils.rb', line 6

def self.hash_respond_to?(hash, method)
  Kind::Hash[hash].respond_to?(method)
end

.slice(hash, keys) ⇒ Object



25
26
27
28
29
# File 'lib/micro/case/utils.rb', line 25

def self.slice(hash, keys)
  return hash.slice(*keys) if hash_respond_to?(hash, :slice)

  hash.select { |key, _value| keys.include?(key) }
end

.stringify_keys(hash) ⇒ Object



19
20
21
22
23
# File 'lib/micro/case/utils.rb', line 19

def self.stringify_keys(hash)
  return hash.transform_keys(&:to_s) if hash_respond_to?(hash, :transform_keys)

  hash.each_with_object({}) { |(k, v), memo| memo[k.to_s] = v }
end

.symbolize_keys(hash) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/micro/case/utils.rb', line 10

def self.symbolize_keys(hash)
  return hash.transform_keys { |key| key.to_sym rescue key } if hash_respond_to?(hash, :transform_keys)

  hash.each_with_object({}) do |(k, v), memo|
    key = k.to_sym rescue k
    memo[key] = v
  end
end