Class: Dry::Struct::Sum

Inherits:
Types::Sum::Constrained
  • Object
show all
Defined in:
lib/dry/struct/sum.rb

Overview

A sum type of two or more structs As opposed to Dry::Types::Sum::Constrained this type tries no to coerce data first.

Instance Method Summary collapse

Instance Method Details

#try(input) {|failure| ... } ⇒ Dry::Types::Result

Parameters:

Yield Parameters:

  • failure (Dry::Types::Result::Failure)

Yield Returns:

  • (Dry::Types::ResultResult)

Returns:

  • (Dry::Types::Result)


13
14
15
16
17
18
19
# File 'lib/dry/struct/sum.rb', line 13

def try(input)
  if input.is_a?(Struct)
    try_struct(input) { super }
  else
    super
  end
end

#|(type) ⇒ Dry::Types::Sum

Build a new sum type

Parameters:

  • type (Dry::Types::Type)

Returns:

  • (Dry::Types::Sum)


24
25
26
27
28
29
30
# File 'lib/dry/struct/sum.rb', line 24

def |(type)
  if type.is_a?(Class) && type <= Struct || type.is_a?(Sum)
    self.class.new(self, type)
  else
    super
  end
end