Class: Dry::Types::Sum
- Inherits:
-
Object
- Object
- Dry::Types::Sum
- Includes:
- Composition
- Defined in:
- lib/dry/types/sum.rb
Overview
Sum type
Instance Attribute Summary
Attributes included from Composition
Attributes included from Options
Class Method Summary collapse
Instance Method Summary collapse
- #call_safe(input, &block) ⇒ Object private
- #call_unsafe(input) ⇒ Object private
- #constrained ⇒ Constrained, Sum
-
#meta(data = Undefined) ⇒ Object
Manage metadata to the type.
- #optional? ⇒ Boolean
- #primitive?(value) ⇒ Boolean private
- #try(input) ⇒ Object
Methods included from Composition
#constrained?, #default?, #failure, included, #initialize, #name, #success, #to_ast, #to_proc
Methods included from Printable
Methods included from Meta
Methods included from Options
Methods included from Builder
#&, #>, #constrained_type, #constructor, #constructor_type, #default, #enum, #fallback, #lax, #maybe, #optional, #|
Methods included from Type
Class Method Details
.operator ⇒ Object
11 |
# File 'lib/dry/types/sum.rb', line 11 def self.operator = :| |
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.
32 33 34 |
# File 'lib/dry/types/sum.rb', line 32 def call_safe(input, &block) left.call_safe(input) { right.call_safe(input, &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.
23 24 25 |
# File 'lib/dry/types/sum.rb', line 23 def call_unsafe(input) left.call_safe(input) { right.call_unsafe(input) } end |
#constrained ⇒ Constrained, Sum
83 84 85 86 87 88 89 |
# File 'lib/dry/types/sum.rb', line 83 def constrained(...) if optional? right.constrained(...).optional else super end end |
#meta(data = Undefined) ⇒ Object
Manage metadata to the type. If the type is an optional, #meta delegates to the right branch
66 67 68 69 70 71 72 73 74 |
# File 'lib/dry/types/sum.rb', line 66 def (data = Undefined) if Undefined.equal?(data) optional? ? right. : super elsif optional? self.class.new(left, right.(data), **) else super end end |
#optional? ⇒ Boolean
16 |
# File 'lib/dry/types/sum.rb', line 16 def optional? = primitive?(nil) |
#primitive?(value) ⇒ Boolean
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.
56 57 58 |
# File 'lib/dry/types/sum.rb', line 56 def primitive?(value) left.primitive?(value) || right.primitive?(value) end |
#try(input) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/dry/types/sum.rb', line 39 def try(input) left.try(input) do right.try(input) do |failure| if block_given? yield(failure) else failure end end end end |