Class: Dry::Data::SumType
- Inherits:
-
Object
- Object
- Dry::Data::SumType
- Defined in:
- lib/dry/data/sum_type.rb
Instance Attribute Summary collapse
-
#left ⇒ Object
readonly
Returns the value of attribute left.
-
#right ⇒ Object
readonly
Returns the value of attribute right.
Instance Method Summary collapse
- #call(input) ⇒ Object (also: #[])
-
#initialize(left, right) ⇒ SumType
constructor
A new instance of SumType.
- #name ⇒ Object
- #valid?(input) ⇒ Boolean
Constructor Details
#initialize(left, right) ⇒ SumType
Returns a new instance of SumType.
10 11 12 |
# File 'lib/dry/data/sum_type.rb', line 10 def initialize(left, right) @left, @right = left, right end |
Instance Attribute Details
#left ⇒ Object (readonly)
Returns the value of attribute left.
6 7 8 |
# File 'lib/dry/data/sum_type.rb', line 6 def left @left end |
#right ⇒ Object (readonly)
Returns the value of attribute right.
8 9 10 |
# File 'lib/dry/data/sum_type.rb', line 8 def right @right end |
Instance Method Details
#call(input) ⇒ Object Also known as: []
18 19 20 21 22 23 24 25 26 |
# File 'lib/dry/data/sum_type.rb', line 18 def call(input) value = left.try(input) if left.valid?(value) value else right[value] end end |
#name ⇒ Object
14 15 16 |
# File 'lib/dry/data/sum_type.rb', line 14 def name [left, right].map(&:name).join(' | ') end |
#valid?(input) ⇒ Boolean
29 30 31 |
# File 'lib/dry/data/sum_type.rb', line 29 def valid?(input) left.valid?(input) || right.valid?(input) end |