Class: Dry::Types::Sum

Inherits:
Object
  • Object
show all
Includes:
Composition
Defined in:
lib/dry/types/sum.rb

Overview

Sum type

Sum types try each constituent type from left to right and return the result from the first successful type. If all types fail, the error from the rightmost (last attempted) type is raised, not necessarily the most “relevant” error.

Examples:

# Given: FixedAmount | Percentage
# Input: { type: "fixed", value: -1.1 }
# FixedAmount fails on value constraint, Percentage fails on type mismatch
# Error raised will be from Percentage (rightmost), not FixedAmount

API:

  • public

Instance Attribute Summary

Attributes included from Composition

#left, #right

Attributes included from Options

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Composition

#constrained?, #default?, #failure, included, #initialize, #name, #success, #to_ast, #to_proc

Methods included from Printable

#to_s

Methods included from Meta

#initialize, #pristine, #with

Methods included from Options

#initialize, #with

Methods included from Builder

#&, #>, #constrained_type, #constructor, #constructor_type, #default, #enum, #fallback, #lax, #maybe, #optional, #|

Methods included from Type

#call, #valid?

Class Method Details

.operatorObject

API:

  • public



21
# File 'lib/dry/types/sum.rb', line 21

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.

Parameters:

Returns:

API:

  • private



45
46
47
# File 'lib/dry/types/sum.rb', line 45

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.

Note:

Tries left type first, then right type. If both fail, raises the error from the right (last attempted) type for performance reasons.

Parameters:

Returns:

API:

  • private



36
37
38
# File 'lib/dry/types/sum.rb', line 36

def call_unsafe(input)
  left.call_safe(input) { right.call_unsafe(input) }
end

#constrainedConstrained, Sum

See Also:

Parameters:

Returns:

API:

  • public



96
97
98
99
100
101
102
# File 'lib/dry/types/sum.rb', line 96

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

See Also:

  • Dry::Types::Sum.[Meta[Meta#meta]

API:

  • public



79
80
81
82
83
84
85
86
87
# File 'lib/dry/types/sum.rb', line 79

def meta(data = Undefined)
  if Undefined.equal?(data)
    optional? ? right.meta : super
  elsif optional?
    self.class.new(left, right.meta(data), **options)
  else
    super
  end
end

#optional?Boolean

Returns:

API:

  • public



26
# File 'lib/dry/types/sum.rb', line 26

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.

Parameters:

Returns:

API:

  • private



69
70
71
# File 'lib/dry/types/sum.rb', line 69

def primitive?(value)
  left.primitive?(value) || right.primitive?(value)
end

#try(input) ⇒ Object

Parameters:

API:

  • public



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dry/types/sum.rb', line 52

def try(input)
  left.try(input) do
    right.try(input) do |failure|
      if block_given?
        yield(failure)
      else
        failure
      end
    end
  end
end