Class: Remap::Base
- Extended by:
- Mapper::API
- Includes:
- ActiveSupport::Configurable, Dry::Core::Constants, Dry::Core::Memoizable, Catchable
- Defined in:
- lib/remap/base.rb
Overview
Direct Known Subclasses
Class Method Summary collapse
-
.call!(state) {|Failure| ... } ⇒ State, T
When request is a success.
-
.configuration {|Config| ... } ⇒ void
Configuration options for the mapper.
-
.contract(&context) ⇒ void
Defines a schema for the mapper If the schema fail, the mapper will fail.
-
.define(target = Nothing, method: :new, strategy: :argument, backtrace: caller, &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.
- .validate? ⇒ Boolean
Instance Method Summary collapse
-
#call(state) {|Failure| ... } ⇒ State
Mappers state according to the mapper rules.
Methods included from Mapper::API
Methods included from Catchable
Methods included from Mapper::Operations
Class Method Details
.call!(state) {|Failure| ... } ⇒ State, T
Returns when request is a success.
264 265 266 |
# File 'lib/remap/base.rb', line 264 def self.call!(state, &error) new(state.options).call(state, &error) end |
.configuration {|Config| ... } ⇒ void
This method returns an undefined value.
Configuration options for the mapper
274 275 276 277 278 |
# File 'lib/remap/base.rb', line 274 def self.configuration(&block) config = Config.new block[config] self.config_options = config end |
.contract(&context) ⇒ void
This method returns an undefined value.
Defines a schema for the mapper If the schema fail, the mapper will fail
148 149 150 |
# File 'lib/remap/base.rb', line 148 def self.contract(&context) self.contract = Dry::Schema.define(&context) end |
.define(target = Nothing, method: :new, strategy: :argument, backtrace: caller, &context) ⇒ void
This method returns an undefined value.
Defines a mapper rules and possible constructor
rubocop:disable Layout/LineLength
242 243 244 245 246 247 248 249 |
# File 'lib/remap/base.rb', line 242 def self.define(target = Nothing, method: :new, strategy: :argument, backtrace: caller, &context) unless context raise ArgumentError, "#{self}.define requires a block" end self.constructor = Constructor.call(method: method, strategy: strategy, target: target) self.context = Compiler.call(backtrace: backtrace, &context) end |
.option(field, type: Types::Any) ⇒ void
This method returns an undefined value.
Defines a required option for the mapper
201 202 203 204 205 206 207 208 209 |
# File 'lib/remap/base.rb', line 201 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.options = options + [-> * { 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
180 181 182 |
# File 'lib/remap/base.rb', line 180 def self.rule(...) self.rules = rules + [-> { rule(...) }] end |
.validate? ⇒ Boolean
283 284 285 |
# File 'lib/remap/base.rb', line 283 def self.validate? config_options.validation end |
Instance Method Details
#call(state) {|Failure| ... } ⇒ State
Mappers state according to the mapper rules
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/remap/base.rb', line 296 def call(state, &error) state._ do |reason| raise ArgumentError, "Invalid state due to #{reason.formatted}" end state.tap do |input| validation.call(input, state.options).tap do |result| unless result.success? return error[state.failure(result.errors.to_h)] end end end s1 = catch_ignored(state) do |s0| return context.call(s0).then(&constructor).remove_id end error[s1.failure] end |