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

Attributes included from Decorator

#type

Attributes included from Options

#options

Instance Method Summary collapse

Methods included from Printable

#to_s

Methods included from Builder

#&, #>, #constrained, #constrained_type, #constructor, #constructor_type, #enum, #fallback, #lax, #maybe, #optional, #|

Methods included from Decorator

#constrained?, #initialize, #respond_to_missing?, #to_proc

Methods included from Options

#initialize, #with

Methods included from Type

#call, #valid?

Dynamic Method Handling

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

Instance Method Details

#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

#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

#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