Class: Remap::Base
- 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
Class Method Summary collapse
- .call!(state, &error) ⇒ Object
-
.contract(&context) ⇒ Object
Holds the current context.
-
.define(target = Nothing, method: :new, strategy: :argument, &context) ⇒ Object
Defines a mapper with a constructor used to wrap the output.
-
.inspect ⇒ String
Pretty print the mapper.
-
.option(field, type: Types::Any) ⇒ Object
Defines a a constructor argument for the mapper.
- .rule ⇒ Object
Instance Method Summary collapse
-
#call(state, &error) ⇒ State
Creates a mapper tree using #context and uses State#state as argument.
Methods included from 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.).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
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. end |
.inspect ⇒ String
Pretty print the mapper
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
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. << ->(*) { option(field, type: key) } end |
.rule ⇒ Object
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
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.).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 |