Class: Remap::Base
- Extended by:
- Operation
- Includes:
- ActiveSupport::Configurable, Dry::Core::Constants
- Defined in:
- lib/remap/base.rb
Overview
Direct Known Subclasses
Class Method Summary collapse
-
.call!(state) {|Failure| ... } ⇒ Result
Similar to call, but takes a state.
-
.contract(&context) ⇒ void
Defines a schema for the mapper If the schema fail, the mapper will fail.
-
.define(target = Nothing, method: :new, strategy: :argument, &context) ⇒ void
Defines a mapper rules and possible constructor.
-
.option(field, type: Types::Any) ⇒ void
Defines a required option for the mapper.
-
.rule ⇒ void
Defines a rule for the mapper If the rule fail, the mapper will fail.
Instance Method Summary collapse
-
#call(state) {|Failure| ... } ⇒ State
Mappers state according to the mapper rules.
Methods included from Mapper::Operations
Class Method Details
.call!(state) {|Failure| ... } ⇒ Result
Similar to call, but takes a state
258 259 260 |
# File 'lib/remap/base.rb', line 258 def self.call!(state, &error) new(state.).call(state._.set(mapper: self), &error) end |
.contract(&context) ⇒ void
This method returns an undefined value.
Defines a schema for the mapper If the schema fail, the mapper will fail
145 146 147 |
# File 'lib/remap/base.rb', line 145 def self.contract(&context) self.contract = Dry::Schema.JSON(&context) end |
.define(target = Nothing, method: :new, strategy: :argument, &context) ⇒ void
This method returns an undefined value.
Defines a mapper rules and possible constructor
238 239 240 241 242 243 244 245 246 247 |
# File 'lib/remap/base.rb', line 238 def self.define(target = Nothing, method: :new, strategy: :argument, &context) unless context raise ArgumentError, "Missing block" end self.context = Compiler.call(&context) self.constructor = Constructor.call(method: method, strategy: strategy, target: target) rescue Dry::Struct::Error => e raise ArgumentError, e. end |
.option(field, type: Types::Any) ⇒ void
This method returns an undefined value.
Defines a required option for the mapper
198 199 200 201 202 203 204 205 206 |
# File 'lib/remap/base.rb', line 198 def self.option(field, type: Types::Any) attribute(field, type) unless (key = schema.keys.find { _1.name == field }) raise ArgumentError, "[BUG] Could not locate [#{field}] in [#{self}]" end self. = + [-> * { option(field, type: key) }] end |
.rule ⇒ void
This method returns an undefined value.
Defines a rule for the mapper If the rule fail, the mapper will fail
177 178 179 |
# File 'lib/remap/base.rb', line 177 def self.rule(...) self.rules = rules + [-> * { rule(...) }] end |
Instance Method Details
#call(state) {|Failure| ... } ⇒ State
Mappers state according to the mapper rules
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/remap/base.rb', line 271 def call(state, &error) unless error raise ArgumentError, "Base#call(state, &error) requires block" end state.tap do |input| validation.call(input, state.).tap do |result| unless result.success? return error[state.failure(result.errors.to_h)] end end end notice = catch :fatal do return context.call(state) do |failure| return error[failure] end.then(&constructor) end error[state.failure(notice)] end |