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

#===(value) ⇒ boolean

Returns:

  • (boolean)


39
40
41
# File 'lib/dry/struct/sum.rb', line 39

def ===(value)
  left === value || right === value
end

#call(input) ⇒ Object



9
10
11
12
13
# File 'lib/dry/struct/sum.rb', line 9

def call(input)
  left.try_struct(input) do
    right.try_struct(input) { super }
  end
end

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

Parameters:

Yield Parameters:

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

Yield Returns:

  • (Dry::Types::Result)

Returns:

  • (Dry::Types::Result)


19
20
21
22
23
24
25
# File 'lib/dry/struct/sum.rb', line 19

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)


30
31
32
33
34
35
36
# File 'lib/dry/struct/sum.rb', line 30

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