Class: Cased::Context::Expander

Inherits:
Object
  • Object
show all
Defined in:
lib/cased/context/expander.rb

Class Method Summary collapse

Class Method Details

.expand(payload) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cased/context/expander.rb', line 6

def self.expand(payload)
  return {} if payload.nil?
  return payload unless payload.respond_to?(:each)

  cased_payload = payload.dup
  payload.each do |key, value|
    if value.respond_to?(:cased_context)
      cased_payload.delete(key)
      cased_payload.update(value.cased_context(category: key))
    elsif value.is_a?(Hash)
      cased_payload[key] = expand(value)
    elsif value.is_a?(Array)
      values = value.collect do |val|
        if val.respond_to?(:cased_context)
          val.cased_context
        else
          val
        end
      end
      cased_payload.update(key => values)
    end
  end

  cased_payload
end