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 collapse
- #options ⇒ Hash included from Options readonly private
- #type ⇒ Type included from Decorator readonly
Instance Method Summary collapse
-
#&(other) ⇒ Intersection, Intersection::Constrained
included
from Builder
private
Compose two types into an Intersection type.
-
#>(other) ⇒ Implication, Implication::Constrained
included
from Builder
private
Compose two types into an Implication type.
-
#call(input = Undefined, &block) ⇒ Object
(also: #[])
included
from Type
Apply type to a value.
- #call_safe(input = Undefined) ⇒ Dry::Monads::Maybe private
- #call_unsafe(input = Undefined) ⇒ Dry::Monads::Maybe private
-
#constrained ⇒ Constrained
included
from Builder
Turn a type into a constrained type.
- #constrained? ⇒ Boolean included from Decorator
- #constrained_type ⇒ Class included from Builder private
-
#constructor(constructor = nil, **options, &block) ⇒ Constructor
(also: #append, #prepend, #>>, #<<)
included
from Builder
Define a constructor for the type.
- #constructor_type ⇒ Class included from Builder private
- #default(value) ⇒ Object
- #default? ⇒ true
-
#enum(*values) ⇒ Enum
included
from Builder
Define an enum on top of the existing type.
-
#fallback(value = Undefined, shared: false, &_fallback) ⇒ Constructor
included
from Builder
Use the given value on type mismatch.
- #initialize(type) ⇒ Object included from Decorator
-
#lax ⇒ Lax
included
from Builder
Turn a type into a lax type that will rescue from type-errors and return the original input.
-
#maybe ⇒ Maybe
included
from Builder
Turn a type into a maybe type.
-
#optional ⇒ Sum
included
from Builder
Turn a type into an optional type.
- #respond_to_missing?(meth, include_private = false) ⇒ Boolean included from Decorator
-
#to_proc ⇒ Proc
included
from Decorator
Wrap the type with a proc.
- #to_s ⇒ String (also: #inspect) included from Printable private
- #try(input = Undefined) ⇒ Result::Success
-
#valid?(input = Undefined) ⇒ Boolean
(also: #===)
included
from Type
private
Whether a value is a valid member of the type.
- #with(**new_options) ⇒ Type included from Options private
-
#|(other) ⇒ Sum, Sum::Constrained
included
from Builder
private
Compose two types into a Sum type.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Dry::Types::Decorator
Instance Attribute Details
Instance Method Details
#&(other) ⇒ Intersection, Intersection::Constrained Originally defined in module Builder
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.
Compose two types into an Intersection type
#>(other) ⇒ Implication, Implication::Constrained Originally defined in module Builder
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.
Compose two types into an Implication type
#call(input = Undefined) ⇒ Object #call(input = Undefined) {|output| ... } ⇒ Object Also known as: [] Originally defined in module Type
Apply type to a value
#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 |
#constrained ⇒ Constrained Originally defined in module Builder
Turn a type into a constrained type
#constrained? ⇒ Boolean Originally defined in module Decorator
#constrained_type ⇒ Class Originally defined in module Builder
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.
#constructor(constructor = nil, **options, &block) ⇒ Constructor Also known as: append, prepend, >>, << Originally defined in module Builder
Define a constructor for the type
#constructor_type ⇒ Class Originally defined in module Builder
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.
#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 |
#enum(*values) ⇒ Enum Originally defined in module Builder
Define an enum on top of the existing type
#fallback(value = Undefined, shared: false, &_fallback) ⇒ Constructor Originally defined in module Builder
Use the given value on type mismatch
#initialize(type) ⇒ Object Originally defined in module Decorator
#lax ⇒ Lax Originally defined in module Builder
Turn a type into a lax type that will rescue from type-errors and return the original input
#respond_to_missing?(meth, include_private = false) ⇒ Boolean Originally defined in module Decorator
#to_proc ⇒ Proc Originally defined in module Decorator
Wrap the type with a proc
#to_s ⇒ String Also known as: inspect Originally defined in module Printable
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.
#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 |
#valid?(input = Undefined) ⇒ Boolean Also known as: === Originally defined in module Type
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.
Whether a value is a valid member of the type
#with(**new_options) ⇒ Type Originally defined in module Options
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.
#|(other) ⇒ Sum, Sum::Constrained Originally defined in module Builder
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.
Compose two types into a Sum type