class Dry::Types::Constrained

Constrained types apply rules to the input

@api public

Attributes

rule[R]

@return [Dry::Logic::Rule]

Public Class Methods

new(type, **options) click to toggle source

@param [Type] type

@param [Hash] options

@api public

Calls superclass method Dry::Types::Decorator::new
# File lib/dry/types/constrained.rb, line 28
def initialize(type, **options)
  super
  @rule = options.fetch(:rule)
end

Public Instance Methods

===(value) click to toggle source

@param [Object] value

@return [Boolean]

@api public

# File lib/dry/types/constrained.rb, line 108
def ===(value)
  valid?(value)
end
call_safe(input) { || ... } click to toggle source

@return [Object]

@api private

# File lib/dry/types/constrained.rb, line 49
def call_safe(input, &block)
  if rule[input]
    type.call_safe(input, &block)
  else
    yield
  end
end
call_unsafe(input) click to toggle source

@return [Object]

@api private

# File lib/dry/types/constrained.rb, line 36
def call_unsafe(input)
  result = rule.(input)

  if result.success?
    type.call_unsafe(input)
  else
    raise ConstraintError.new(result, input)
  end
end
constrained(options) click to toggle source

@param [Hash] options

The options hash provided to {Types.Rule} and combined
using {&} with previous {#rule}

@return [Constrained]

@see Dry::Logic::Operators#and

@api public

# File lib/dry/types/constrained.rb, line 92
def constrained(options)
  with(rule: rule & Types.Rule(options))
end
constrained?() click to toggle source

@return [true]

@api public

# File lib/dry/types/constrained.rb, line 99
def constrained?
  true
end
constructor_type() click to toggle source

@api private

# File lib/dry/types/constrained.rb, line 127
def constructor_type
  type.constructor_type
end
lax() click to toggle source

Build lax type. Constraints are not applicable to lax types hence unwrapping

@return [Lax] @api public

# File lib/dry/types/constrained.rb, line 116
def lax
  type.lax
end
to_ast(meta: true) click to toggle source

@see Nominal#to_ast @api public

# File lib/dry/types/constrained.rb, line 122
def to_ast(meta: true)
  [:constrained, [type.to_ast(meta: meta), rule.to_ast]]
end
try(input) { |failure| ... } click to toggle source

Safe coercion attempt. It is similar to call with a block given but returns a Result instance with metadata about errors (if any).

@overload try(input)

@param [Object] input
@return [Logic::Result]

@overload try(input)

@param [Object] input
@yieldparam [Failure] failure
@yieldreturn [Object]
@return [Object]

@api public

# File lib/dry/types/constrained.rb, line 72
def try(input, &block)
  result = rule.(input)

  if result.success?
    type.try(input, &block)
  else
    failure = failure(input, ConstraintError.new(result, input))
    block_given? ? yield(failure) : failure
  end
end

Private Instance Methods

decorate?(response) click to toggle source

@param [Object] response

@return [Boolean]

@api private

Calls superclass method Dry::Types::Decorator#decorate?
# File lib/dry/types/constrained.rb, line 138
def decorate?(response)
  super || response.is_a?(Constructor)
end