class Dry::Types::Sum
@api public
Sum type
def call_safe(input, &block)
- Api: - private
Returns:
-
(Object)
-
Parameters:
-
input
(Object
) --
def call_safe(input, &block) left.call_safe(input) { right.call_safe(input, &block) } end
def call_unsafe(input)
- Api: - private
Returns:
-
(Object)
-
Parameters:
-
input
(Object
) --
def call_unsafe(input) left.call_safe(input) { right.call_unsafe(input) } end
def constrained(options)
- Api: - public
Other tags:
- See: Builder#constrained -
Returns:
-
(Constrained, Sum)
-
Parameters:
-
options
(Hash
) --
def constrained(options) if optional? right.constrained(options).optional else super end end
def constrained?
- Api: - public
Returns:
-
(false)
-
def constrained? false end
def default?
- Api: - public
Returns:
-
(false)
-
def default? false end
def failure(input, _error = nil)
- Api: - private
def failure(input, _error = nil) if !left.valid?(input) left.failure(input, left.try(input).error) else right.failure(input, right.try(input).error) end end
def initialize(left, right, **options)
- Api: - private
Parameters:
-
options
(Hash
) -- -
right
(Type
) -- -
left
(Type
) --
def initialize(left, right, **options) super @left, @right = left, right freeze end
def meta(data = nil)
- Api: - public
Other tags:
- See: [Meta#meta] -
def meta(data = nil) if data.nil? optional? ? right.meta : super elsif optional? self.class.new(left, right.meta(data), **options) else super end end
def name
- Api: - public
Returns:
-
(String)
-
def name [left, right].map(&:name).join(' | ') end
def optional?
- Api: - public
Returns:
-
(Boolean)
-
def optional? primitive?(nil) end
def primitive?(value)
- Api: - private
Returns:
-
(Boolean)
-
Parameters:
-
value
(Object
) --
def primitive?(value) left.primitive?(value) || right.primitive?(value) end
def success(input)
- Api: - private
def success(input) if left.valid?(input) left.success(input) elsif right.valid?(input) right.success(input) else raise ArgumentError, "Invalid success value '#{input}' for #{inspect}" end end
def to_ast(meta: true)
- Api: - public
Other tags:
- See: Nominal#to_ast -
def to_ast(meta: true) [:sum, [left.to_ast(meta: meta), right.to_ast(meta: meta), meta ? self.meta : EMPTY_HASH]] end
def to_proc
- Api: - public
Returns:
-
(Proc)
-
def to_proc proc { |value| self.(value) } end
def try(input)
- Api: - public
Parameters:
-
input
(Object
) --
def try(input) left.try(input) do right.try(input) do |failure| if block_given? yield(failure) else failure end end end end