Module: Dry::Types::Constructor::Wrapper
- Defined in:
- lib/dry/types/constructor/wrapper.rb
Instance Method Summary collapse
- #call_safe(input) ⇒ Object private
- #call_unsafe(input) ⇒ Object private
-
#constructor ⇒ Constructor
(also: #append, #>>)
Define a constructor for the type.
- #lax ⇒ Constructor
-
#prepend(new_fn = nil, **options, &block) ⇒ Constructor
(also: #<<)
Build a new constructor by prepending a block to the coercion function.
- #try(input) ⇒ Logic::Result, ...
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.
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.
16 |
# File 'lib/dry/types/constructor/wrapper.rb', line 16 def call_unsafe(input) = fn.(input, type) |
#constructor ⇒ Constructor Also known as: append, >>
Define a constructor for the type
43 |
# File 'lib/dry/types/constructor/wrapper.rb', line 43 define_method(:constructor, Builder.instance_method(:constructor)) |
#lax ⇒ Constructor
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
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, **, &block) prep_fn = Function[new_fn || block] decorated = if prep_fn.wrapper? type.constructor(prep_fn, **) else type.prepend(prep_fn, **) end __new__(decorated) end |
#try(input) ⇒ Logic::Result, ...
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 |