Class: NeverBounce::CLI::Feature::Envars::ExamplesMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/never_bounce/cli/feature/envars/examples_mapper.rb

Overview

A simple transformer/validator for examples argument.

See Also:

Instance Method Summary collapse

Instance Method Details

#process(input) ⇒ Hash Also known as: []

Process input, render output.

process([1, 2, 3])            # => {values: [1, 2, 3]}
process([1, 2, default: 3])   # => {values: [1, 2, 3], default: 3}

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/never_bounce/cli/feature/envars/examples_mapper.rb', line 16

def process(input)
  values, default = [], nil

  input.each do |elem|
    if elem.is_a? Hash
      # Validate, then use.
      raise ArgumentError, "Unknown element format: #{elem.inspect}" if elem.keys != [:default]
      values << (default = elem[:default])
    else
      values << elem
    end
  end

  # Return result.
  {}.tap do |h|
    h[:values] = values
    h[:default] = default if default
  end
end