Class: Remap::Base

Inherits:
Mapper show all
Extended by:
Dry::Configurable, Forwardable, Operation, State
Includes:
Dry::Core::Constants, Dry::Core::Memoizable
Defined in:
lib/remap/base.rb

Constant Summary collapse

CONTRACT =
Dry::Schema.JSON do
  # NOP
end

Constants included from State

State::Schema

Class Method Summary collapse

Instance Method Summary collapse

Methods included from State

state

Methods included from Mapper::Operations

#&, #^, #|

Class Method Details

.call!(state, &error) ⇒ Object



95
96
97
98
99
# File 'lib/remap/base.rb', line 95

def self.call!(state, &error)
  new(state.options).call(state._.set(mapper: self), &error)
rescue Dry::Struct::Error => e
  raise ArgumentError, "Option missing to mapper [#{self}]: #{e}"
end

.contract(&context) ⇒ Object

Holds the current context



33
34
35
# File 'lib/remap/base.rb', line 33

def self.contract(&context)
  config.contract = Dry::Schema.JSON(&context)
end

.define(target = Nothing, method: :new, strategy: :argument, &context) ⇒ Object

Defines a mapper with a constructor used to wrap the output

Examples:

A mapper from path :a to path :b

class Mapper < Remap
  define do
    map :a, to: :b
  end
end

Mapper.call(a: 1) # => { b: 1 }

Parameters:

  • constructor (#call)


75
76
77
78
79
80
81
82
83
84
# File 'lib/remap/base.rb', line 75

def self.define(target = Nothing, method: :new, strategy: :argument, &context)
  unless context
    raise ArgumentError, "Missing block"
  end

  config.context = Compiler.call(&context)
  config.constructor = Constructor.call(method: method, strategy: strategy, target: target)
rescue Dry::Struct::Error => e
  raise ArgumentError, e.message
end

.inspectString

Pretty print the mapper

Returns:

  • (String)


59
60
61
# File 'lib/remap/base.rb', line 59

def self.inspect
  "<#{self.class} #{rule}, #{self}>"
end

.option(field, type: Types::Any) ⇒ Object

Defines a a constructor argument for the mapper

Parameters:

  • name (Symbol)
  • type (#call) (defaults to: Types::Any)


46
47
48
49
50
51
52
53
54
# File 'lib/remap/base.rb', line 46

def self.option(field, type: Types::Any)
  attribute(field, type)

  unless (key = schema.keys.find { _1.name == field })
    raise ArgumentError, "Could not locate [#{field}] in [#{self}]"
  end

  config.options << ->(*) { option(field, type: key) }
end

.ruleObject

See Also:

  • Dry::Validation::Contract.rule


38
39
40
# File 'lib/remap/base.rb', line 38

def self.rule(...)
  config.rules << ->(*) { rule(...) }
end

Instance Method Details

#call(state, &error) ⇒ State

Creates a mapper tree using #context and uses State#state as argument

Returns:

See Also:



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/remap/base.rb', line 108

def call(state, &error)
  unless error
    raise ArgumentError, "Missing block"
  end

  state.tap do |input|
    contract.call(input, state.options).tap do |result|
      unless result.success?
        return error[state.failure(result.errors.to_h)]
      end
    end
  end

  state.then(&config.context).then(&config.constructor)
end