Class: Dry::Types::Sum
Defined Under Namespace
Classes: Constrained
Instance Attribute Summary collapse
Attributes included from Options
#options
Instance Method Summary
collapse
Methods included from Options
#meta, #with
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.
31
32
33
34
35
|
# File 'lib/dry/types/sum.rb', line 31
def initialize(left, right, options = {})
super
@left, @right = left, right
freeze
end
|
Instance Attribute Details
#left ⇒ Object
Returns the value of attribute left.
10
11
12
|
# File 'lib/dry/types/sum.rb', line 10
def left
@left
end
|
#right ⇒ Object
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:
[]
53
54
55
|
# File 'lib/dry/types/sum.rb', line 53
def call(input)
try(input).input
end
|
#constrained? ⇒ Boolean
49
50
51
|
# File 'lib/dry/types/sum.rb', line 49
def constrained?
false
end
|
#default? ⇒ Boolean
41
42
43
|
# File 'lib/dry/types/sum.rb', line 41
def default?
false
end
|
#maybe? ⇒ Boolean
45
46
47
|
# File 'lib/dry/types/sum.rb', line 45
def maybe?
false
end
|
#name ⇒ Object
37
38
39
|
# File 'lib/dry/types/sum.rb', line 37
def name
[left, right].map(&:name).join(' | ')
end
|
#primitive?(value) ⇒ Boolean
72
73
74
|
# File 'lib/dry/types/sum.rb', line 72
def primitive?(value)
left.primitive?(value) || right.primitive?(value)
end
|
#try(input, &block) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/dry/types/sum.rb', line 58
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
76
77
78
|
# File 'lib/dry/types/sum.rb', line 76
def valid?(value)
left.valid?(value) || right.valid?(value)
end
|