Class: Dry::Types::Sum
- Inherits:
-
Object
- Object
- Dry::Types::Sum
- Defined in:
- lib/dry/types/sum.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Constrained
Instance Attribute Summary collapse
-
#left ⇒ Object
readonly
Returns the value of attribute left.
-
#right ⇒ Object
readonly
Returns the value of attribute right.
Attributes included from Options
Instance Method Summary collapse
- #call(input) ⇒ Object (also: #[])
-
#initialize(left, right, options = {}) ⇒ Sum
constructor
A new instance of Sum.
- #name ⇒ Object
- #primitive?(value) ⇒ Boolean
- #try(input, &block) ⇒ Object
- #valid?(value) ⇒ Boolean
Methods included from Options
Methods included from Builder
#constrained, #constrained_type, #constructor, #default, #enum, #maybe, #optional, #safe, #|
Constructor Details
#initialize(left, right, options = {}) ⇒ Sum
Returns a new instance of Sum.
20 21 22 23 24 |
# File 'lib/dry/types/sum.rb', line 20 def initialize(left, right, = {}) super @left, @right = left, right freeze end |
Instance Attribute Details
#left ⇒ Object (readonly)
Returns the value of attribute left.
10 11 12 |
# File 'lib/dry/types/sum.rb', line 10 def left @left end |
#right ⇒ Object (readonly)
Returns the value of attribute right.
12 13 14 |
# File 'lib/dry/types/sum.rb', line 12 def right @right end |
Instance Method Details
#call(input) ⇒ Object Also known as: []
30 31 32 33 34 |
# File 'lib/dry/types/sum.rb', line 30 def call(input) try(input) do |result| raise ConstraintError, result end.input end |
#name ⇒ Object
26 27 28 |
# File 'lib/dry/types/sum.rb', line 26 def name [left, right].map(&:name).join(' | ') end |
#primitive?(value) ⇒ Boolean
51 52 53 |
# File 'lib/dry/types/sum.rb', line 51 def primitive?(value) left.primitive?(value) || right.primitive?(value) end |
#try(input, &block) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/dry/types/sum.rb', line 37 def try(input, &block) result = left.try(input) do right.try(input) end return result if result.success? if block yield(result) else result end end |
#valid?(value) ⇒ Boolean
55 56 57 |
# File 'lib/dry/types/sum.rb', line 55 def valid?(value) left.valid?(value) || right.valid?(value) end |