class RSchema::Schemas::Coercer

A schema that applies a coercer to a value, before passing the coerced value to a subschema.

This is not a type of schema that you would typically create yourself. It is used internally to implement RSchema's coercion functionality.

Attributes

coercer[R]
subschema[R]

Public Class Methods

new(coercer, subschema) click to toggle source
# File lib/rschema/schemas/coercer.rb, line 15
def initialize(coercer, subschema)
  @coercer = coercer
  @subschema = subschema
end

Public Instance Methods

call(value, options) click to toggle source
# File lib/rschema/schemas/coercer.rb, line 20
def call(value, options)
  unless coercer.will_affect?(value)
    # short-circuit the coercer
    return @subschema.call(value, options)
  end

  result = coercer.call(value)
  if result.valid?
    @subschema.call(result.value, options)
  else
    failure(value, result.error)
  end
end
with_wrapped_subschemas(wrapper) click to toggle source
# File lib/rschema/schemas/coercer.rb, line 34
def with_wrapped_subschemas(wrapper)
  self.class.new(coercer, wrapper.wrap(subschema))
end

Private Instance Methods

failure(value, name) click to toggle source
# File lib/rschema/schemas/coercer.rb, line 40
def failure(value, name)
  Result.failure(
    Error.new(
      schema: self,
      value: value,
      symbolic_name: name || :coercion_failure,
    ),
  )
end