Class: TypedParams::Transformer

Inherits:
Mapper
  • Object
show all
Defined in:
lib/typed_params/transformer.rb

Instance Method Summary collapse

Methods inherited from Mapper

call, #initialize

Constructor Details

This class inherits a constructor from TypedParams::Mapper

Instance Method Details

#call(params) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/typed_params/transformer.rb', line 7

def call(params)
  depth_first_map(params) do |param|
    schema = param.schema
    parent = param.parent

    # Ignore nil optionals when config is enabled
    unless schema.allow_nil?
      if param.value.nil? && schema.optional? && TypedParams.config.ignore_nil_optionals
        param.delete

        break
      end
    end

    schema.transforms.map do |transform|
      key, value = transform.call(param.key, param.value)
      if key.nil?
        param.delete

        break
      end

      # Check for nils again after transform
      unless schema.allow_nil?
        if value.nil? && schema.optional? && TypedParams.config.ignore_nil_optionals
          param.delete

          break
        end
      end

      # If param's key has changed, we want to rename the key
      # for its parent too.
      if param.parent? && param.key != key
        parent[key] = param.delete
      end

      param.key, param.value = key, value
    end
  end
end