Module: Dry::Types::Constructor::Wrapper
- Defined in:
- lib/dry/types/constructor/wrapper.rb
Instance Method Summary collapse
- #call_safe(input, &block) ⇒ 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, &block) ⇒ Logic::Result, ...
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.
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.
18 19 20 |
# File 'lib/dry/types/constructor/wrapper.rb', line 18 def call_unsafe(input) fn.(input, type) end |
#constructor ⇒ Constructor Also known as: append, >>
Define a constructor for the type
47 |
# File 'lib/dry/types/constructor/wrapper.rb', line 47 define_method(:constructor, Builder.instance_method(:constructor)) |
#lax ⇒ Constructor
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
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, **, &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, &block) ⇒ Logic::Result, ...
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 |