Class: Dry::Types::Sum

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

Overview

Sum type

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#leftType (readonly) Originally defined in module Composition

Returns:

#optionsHash (readonly) Originally defined in module Options

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.

Returns:

#rightType (readonly) Originally defined in module Composition

Returns:

Class Method Details

.operatorObject



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

def self.operator
  :|
end

Instance Method Details

#&(other) ⇒ Intersection, Intersection::Constrained Originally defined in module Builder

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.

Compose two types into an Intersection type

Parameters:

Returns:

#>(other) ⇒ Implication, Implication::Constrained Originally defined in module Builder

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.

Compose two types into an Implication type

Parameters:

Returns:

#call(input = Undefined) ⇒ Object #call(input = Undefined) {|output| ... } ⇒ Object Also known as: [] Originally defined in module Type

Apply type to a value

Overloads:

  • #call(input = Undefined) ⇒ Object

    Possibly unsafe coercion attempt. If a value doesn't match the type, an exception will be raised.

    Parameters:

    • input (Object) (defaults to: Undefined)

    Returns:

    • (Object)
  • #call(input = Undefined) {|output| ... } ⇒ Object

    When a block is passed, #call will never throw an exception on failed coercion, instead it will call the block.

    Parameters:

    • input (Object) (defaults to: Undefined)

    Yield Parameters:

    • output (Object)

      Partially coerced value

    Returns:

    • (Object)

#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

#constrained?false Originally defined in module Composition

Returns:

  • (false)

#constrained_typeClass Originally defined in module Builder

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.

Returns:

  • (Class)

#constructor(constructor = nil, **options, &block) ⇒ Constructor Also known as: append, prepend, >>, << Originally defined in module Builder

Define a constructor for the type

Parameters:

  • constructor (#call, nil) (defaults to: nil)
  • options (Hash)
  • block (#call, nil)

Returns:

#constructor_typeClass Originally defined in module Builder

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.

Returns:

  • (Class)

#default(input = Undefined, options = EMPTY_HASH, &block) ⇒ Default Originally defined in module Builder

Turn a type into a type with a default value

Parameters:

  • input (Object) (defaults to: Undefined)
  • block (#call, nil)
  • [Boolean] (Hash)

    a customizable set of options

Returns:

Raises:

#default?false Originally defined in module Composition

Returns:

  • (false)

#enum(*values) ⇒ Enum Originally defined in module Builder

Define an enum on top of the existing type

Parameters:

Returns:

#failure(input, _error = nil) ⇒ Object Originally defined in module Composition

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.

#fallback(value = Undefined, shared: false, &_fallback) ⇒ Constructor Originally defined in module Builder

Use the given value on type mismatch

Parameters:

  • value (Object) (defaults to: Undefined)
  • fallback (#call, nil)
  • [Boolean] (Hash)

    a customizable set of options

Returns:

#initialize(left, right, **options) ⇒ Object Originally defined in module Composition

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:

#laxLax Originally defined in module Builder

Turn a type into a lax type that will rescue from type-errors and return the original input

Returns:

#maybeMaybe Originally defined in module Builder

Turn a type into a maybe type

Returns:

#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

#nameString Originally defined in module Composition

Returns:

  • (String)

#optionalSum Originally defined in module Builder

Turn a type into an optional type

Returns:

#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

#pristineDry::Types::Type Originally defined in module Meta

Resets meta

Returns:

#success(input) ⇒ Object Originally defined in module Composition

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.

#to_ast(meta: true) ⇒ Object Originally defined in module Composition

See Also:

#to_procProc Originally defined in module Composition

Wrap the type with a proc

Returns:

  • (Proc)

#to_sString Also known as: inspect Originally defined in module Printable

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.

Returns:

  • (String)

#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

#valid?(input = Undefined) ⇒ Boolean Also known as: === Originally defined in module Type

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.

Whether a value is a valid member of the type

Returns:

  • (Boolean)

#with(**options) ⇒ Type Originally defined in module Meta

Parameters:

  • options (Hash)

    new_options

Returns:

#|(other) ⇒ Sum, Sum::Constrained Originally defined in module Builder

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.

Compose two types into a Sum type

Parameters:

Returns: