Method: ParamParam::Actions#define

Defined in:
lib/param_param.rb

#defineObject

Defines pipelines and binds them to symbols. Pipelines are used to process parameters provided in a form of a hash. Each pipeline is defined for a key and processes a value related to that key in provided parameters.

lambda { |rules, params| ... }

The lambda returns two hashes:

  • if a value related to a key can be procesed by an action, the result is bound to the key and added to the first hash

  • if a value related to a key can’t be processed by an action, the error is bound to the key and added to the second hash

Each action needs to be a lambda taking Optiomist as the only or the last parameter and returning either:

  • ParamParam::Success with processed option

  • ParamParam::Failure with an error



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/param_param.rb', line 46

def define
  lambda { |rules, params|
    results = rules.to_h do |key, fn|
      option = params.key?(key) ? optionize(params[key]) : Optiomist.none
      [key, fn.call(option)]
    end

    errors = results.select { |_, result| result.failure? }
                    .transform_values(&:error)
    params = results.select { |_, result| result.success? && result.value.some? }
                    .transform_values { |result| result.value.value }
    [params, errors]
  }.curry
end