Module: Grape::Extensions::DeepSymbolizeHash

Defined in:
lib/grape/extensions/deep_symbolize_hash.rb

Class Method Summary collapse

Class Method Details

.deep_symbolize_keys_in(object) ⇒ Object


6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/grape/extensions/deep_symbolize_hash.rb', line 6

def self.deep_symbolize_keys_in(object)
  case object
  when ::Hash
    object.each_with_object({}) do |(key, value), new_hash|
      new_hash[symbolize_key(key)] = deep_symbolize_keys_in(value)
    end
  when ::Array
    object.map { |element| deep_symbolize_keys_in(element) }
  else
    object
  end
end

.symbolize_key(key) ⇒ Object


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/grape/extensions/deep_symbolize_hash.rb', line 19

def self.symbolize_key(key)
  if key.is_a?(Symbol)
    key
  elsif key.is_a?(String)
    key.to_sym
  elsif key.respond_to?(:to_sym)
    key.to_sym
  else
    key
  end
end