Class: TypedParams::Transforms::Collapse

Inherits:
Transform
  • Object
show all
Defined in:
lib/typed_params/transforms/collapse.rb

Constant Summary collapse

DEFAULT_FORMAT =
:parent_child

Instance Method Summary collapse

Methods inherited from Transform

wrap

Constructor Details

#initialize(options) ⇒ Collapse

Returns a new instance of Collapse.



10
11
12
13
14
15
16
17
18
19
# File 'lib/typed_params/transforms/collapse.rb', line 10

def initialize(options)
  @format = case options
            in format: :parent_child | :child_parent | :child => format
              format
            in Proc => transformer
              transformer
            in true
              DEFAULT_FORMAT
            end
end

Instance Method Details

#call(param) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/typed_params/transforms/collapse.rb', line 21

def call(param)
  return unless
    param.hash? && param.parent?

  parent = param.parent

  param.values.each do |child|
    key, value = transform(param.key, param.value, child.key, child.value)

    child.key   = key
    child.value = value

    parent[key] = child # move up
  end

  param.delete
end