module Dry::Types::Constructor::Wrapper

Public Instance Methods

<<(new_fn = nil, **options, &block)
Alias for: prepend
call_safe(input, &block) click to toggle source

@return [Object]

@api private

# File lib/dry/types/constructor/wrapper.rb, line 11
def call_safe(input, &block)
  fn.(input, type, &block)
end
call_unsafe(input) click to toggle source

@return [Object]

@api private

# File lib/dry/types/constructor/wrapper.rb, line 18
def call_unsafe(input)
  fn.(input, type)
end
lax() click to toggle source

@return [Constructor]

@api public

# File lib/dry/types/constructor/wrapper.rb, line 77
def lax
  # return self back because wrapping function
  # can handle failed type check
  self
end
prepend(new_fn = nil, **options, &block) click to toggle source

Build a new constructor by prepending a block to the coercion function

@param [#call, nil] new_fn @param [Hash] options @param [#call, nil] block

@return [Constructor]

@api public

# File lib/dry/types/constructor/wrapper.rb, line 60
def prepend(new_fn = nil, **options, &block)
  prep_fn = Function[new_fn || block]

  decorated =
    if prep_fn.wrapper?
      type.constructor(prep_fn, **options)
    else
      type.prepend(prep_fn, **options)
    end

  __new__(decorated)
end
Also aliased as: <<
try(input) { |failure| ... } click to toggle source

@param [Object] input @param [#call,nil] block

@return [Logic::Result, Types::Result] @return [Object] if block given and try fails

@api public

# File lib/dry/types/constructor/wrapper.rb, line 29
def try(input, &block)
  value = fn.(input, type)
rescue CoercionError => e
  failure = failure(input, e)
  block_given? ? yield(failure) : failure
else
  type.try(value, &block)
end

Private Instance Methods

__new__(type) click to toggle source

Replace underlying type

@api private

# File lib/dry/types/constructor/wrapper.rb, line 88
def __new__(type)
  self.class.new(type, *@__args__.drop(1), **@options)
end