Module: Dry::Types::Constructor::Wrapper

Defined in:
lib/dry/types/constructor/wrapper.rb

Instance Method Summary collapse

Instance Method Details

#call_safe(input) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Object)


11
# File 'lib/dry/types/constructor/wrapper.rb', line 11

def call_safe(input, &) = fn.(input, type, &)

#call_unsafe(input) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Object)


16
# File 'lib/dry/types/constructor/wrapper.rb', line 16

def call_unsafe(input) = fn.(input, type)

#constructorConstructor Also known as: append, >>

Define a constructor for the type

Parameters:

  • constructor (#call, nil)
  • options (Hash)
  • block (#call, nil)

Returns:



43
# File 'lib/dry/types/constructor/wrapper.rb', line 43

define_method(:constructor, Builder.instance_method(:constructor))

#laxConstructor

Returns:



73
74
75
76
77
# File 'lib/dry/types/constructor/wrapper.rb', line 73

def lax
  # return self back because wrapping function
  # can handle failed type check
  self
end

#prepend(new_fn = nil, **options, &block) ⇒ Constructor Also known as: <<

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

Parameters:

  • new_fn (#call, nil) (defaults to: nil)
  • options (Hash)
  • block (#call, nil)

Returns:



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dry/types/constructor/wrapper.rb', line 56

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

#try(input) ⇒ Logic::Result, ...

Parameters:

  • input (Object)
  • block (#call, nil)

Returns:

  • (Logic::Result, Types::Result)
  • (Object)

    if block given and try fails



25
26
27
28
29
30
31
32
# File 'lib/dry/types/constructor/wrapper.rb', line 25

def try(input, &)
  value = fn.(input, type)
rescue CoercionError => e
  failure = failure(input, e)
  block_given? ? yield(failure) : failure
else
  type.try(value, &)
end