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

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

Instance Method Summary collapse

Instance Method Details

#call_safe(input, &block) ⇒ 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
12
13
# File 'lib/dry/types/constructor/wrapper.rb', line 11

def call_safe(input, &block)
  fn.(input, type, &block)
end

#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)


18
19
20
# File 'lib/dry/types/constructor/wrapper.rb', line 18

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

#constructorConstructor Also known as: append, >>

Define a constructor for the type

Parameters:

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

Returns:



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

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

#laxConstructor

Returns:



77
78
79
80
81
# 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) ⇒ 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:



60
61
62
63
64
65
66
67
68
69
70
71
# 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

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

Parameters:

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

Returns:

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

    if block given and try fails



29
30
31
32
33
34
35
36
# 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