Class: Dry::Types::Maybe

Inherits:
Object
  • Object
show all
Includes:
Builder, Decorator, Printable, Type
Defined in:
lib/dry/types/extensions/maybe.rb

Overview

Maybe extension provides Maybe types where values are wrapped using Either monad

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Dry::Types::Decorator

Instance Attribute Details

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

#typeType (readonly) Originally defined in module Decorator

Returns:

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 = Undefined) ⇒ Dry::Monads::Maybe

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 (Dry::Monads::Maybe, Object) (defaults to: Undefined)

Returns:

  • (Dry::Monads::Maybe)


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

def call_safe(input = Undefined)
  case input
  when ::Dry::Monads::Maybe
    input
  when Undefined
    None()
  else
    Maybe(type.call_safe(input) { |output = input| return yield(output) })
  end
end

#call_unsafe(input = Undefined) ⇒ Dry::Monads::Maybe

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 (Dry::Monads::Maybe, Object) (defaults to: Undefined)

Returns:

  • (Dry::Monads::Maybe)


28
29
30
31
32
33
34
35
36
37
# File 'lib/dry/types/extensions/maybe.rb', line 28

def call_unsafe(input = Undefined)
  case input
  when ::Dry::Monads::Maybe
    input
  when Undefined
    None()
  else
    Maybe(type.call_unsafe(input))
  end
end

#constrained(options) ⇒ Constrained Originally defined in module Builder

Turn a type into a constrained type

Parameters:

Returns:

#constrained?Boolean Originally defined in module Decorator

Returns:

  • (Boolean)

#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(value) ⇒ Object

Parameters:

  • value (Object)

Raises:

  • (ArgumentError)

    if nil provided as default value

See Also:



84
85
86
87
88
89
90
# File 'lib/dry/types/extensions/maybe.rb', line 84

def default(value)
  if value.nil?
    raise ArgumentError, "nil cannot be used as a default of a maybe type"
  else
    super
  end
end

#default?true

Returns:

  • (true)


73
74
75
# File 'lib/dry/types/extensions/maybe.rb', line 73

def default?
  true
end

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

Define an enum on top of the existing type

Parameters:

Returns:

#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(type) ⇒ Object Originally defined in module Decorator

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:

#optionalSum Originally defined in module Builder

Turn a type into an optional type

Returns:

#respond_to_missing?(meth, include_private = false) ⇒ Boolean Originally defined in module Decorator

Parameters:

  • meth (Symbol)
  • include_private (Boolean) (defaults to: false)

Returns:

  • (Boolean)

#to_procProc Originally defined in module Decorator

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 = Undefined) ⇒ Result::Success

Parameters:

  • input (Object) (defaults to: Undefined)

Returns:



60
61
62
63
64
65
66
67
68
# File 'lib/dry/types/extensions/maybe.rb', line 60

def try(input = Undefined)
  result = type.try(input)

  if result.success?
    Result::Success.new(Maybe(result.input))
  else
    result
  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(**new_options) ⇒ Type 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.

Parameters:

  • new_options (Hash)

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: