Class: Dry::Types::Sum

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

Overview

Sum type

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



11
12
13
# File 'lib/dry/types/sum.rb', line 11

def self.operator
  :|
end

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:

  • input (Object)

Returns:

  • (Object)


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

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.

Parameters:

  • input (Object)

Returns:

  • (Object)


27
28
29
# File 'lib/dry/types/sum.rb', line 27

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

#constrained(options) ⇒ Constrained, Sum

Parameters:

Returns:

See Also:



87
88
89
90
91
92
93
# File 'lib/dry/types/sum.rb', line 87

def constrained(options)
  if optional?
    right.constrained(options).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]


70
71
72
73
74
75
76
77
78
# File 'lib/dry/types/sum.rb', line 70

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:

  • (Boolean)


18
19
20
# File 'lib/dry/types/sum.rb', line 18

def optional?
  primitive?(nil)
end

#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:

  • value (Object)

Returns:

  • (Boolean)


60
61
62
# File 'lib/dry/types/sum.rb', line 60

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

#try(input) ⇒ Object

Parameters:

  • input (Object)


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dry/types/sum.rb', line 43

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