Class: Dry::Types::Maybe
- Inherits:
-
Object
- Object
- Dry::Types::Maybe
- Defined in:
- lib/dry/types/extensions/maybe.rb
Overview
Maybe extension provides Maybe types where values are wrapped using ‘Either` monad
Instance Attribute Summary
Attributes included from Decorator
Attributes included from Options
Instance Method Summary collapse
- #call_safe(input = Undefined) ⇒ Dry::Monads::Maybe private
- #call_unsafe(input = Undefined) ⇒ Dry::Monads::Maybe private
- #default(value) ⇒ Object
- #default? ⇒ true
- #try(input = Undefined) ⇒ Result::Success
Methods included from Printable
Methods included from Builder
#&, #>, #constrained, #constrained_type, #constructor, #constructor_type, #enum, #fallback, #lax, #maybe, #optional, #|
Methods included from Decorator
#constrained?, #initialize, #respond_to_missing?, #to_proc
Methods included from Options
Methods included from Type
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Dry::Types::Decorator
Instance Method Details
#call_safe(input = Undefined) ⇒ Dry::Monads::Maybe
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.
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/dry/types/extensions/maybe.rb', line 44 def call_safe(input = Undefined) case input when ::Dry::Monads::Maybe input when Undefined None() else Maybe(type.call_safe(input) { |output = input| return yield(output) }) end end |
#call_unsafe(input = Undefined) ⇒ Dry::Monads::Maybe
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.
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/dry/types/extensions/maybe.rb', line 28 def call_unsafe(input = Undefined) case input when ::Dry::Monads::Maybe input when Undefined None() else Maybe(type.call_unsafe(input)) end end |
#default(value) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/dry/types/extensions/maybe.rb', line 84 def default(value) if value.nil? raise ArgumentError, "nil cannot be used as a default of a maybe type" else super end end |
#default? ⇒ true
73 74 75 |
# File 'lib/dry/types/extensions/maybe.rb', line 73 def default? true end |
#try(input = Undefined) ⇒ Result::Success
60 61 62 63 64 65 66 67 68 |
# File 'lib/dry/types/extensions/maybe.rb', line 60 def try(input = Undefined) result = type.try(input) if result.success? Result::Success.new(Maybe(result.input)) else result end end |