Class: Dry::Types::Maybe

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

Instance Attribute Summary

Attributes included from Decorator

#type

Attributes included from Options

#options

Instance Method Summary collapse

Methods included from Builder

#constrained, #constrained_type, #constructor, #enum, #maybe, #optional, #safe, #|

Methods included from Decorator

#constrained?, #constructor, #decorate?, #default?, #initialize, #method_missing, #respond_to_missing?, #valid?

Methods included from Options

#initialize, #meta, #with

Dynamic Method Handling

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

Instance Method Details

#call(input) ⇒ Dry::Monads::Maybe Also known as: []

Parameters:

  • input (Dry::Monads::Maybe, Object)

Returns:

  • (Dry::Monads::Maybe)


14
15
16
# File 'lib/dry/types/extensions/maybe.rb', line 14

def call(input)
  input.is_a?(Dry::Monads::Maybe) ? input : Maybe(type[input])
end

#default(value) ⇒ Object

Parameters:

  • value (Object)

Raises:

  • (ArgumentError)

    if nil provided as default value

See Also:



33
34
35
36
37
38
39
# File 'lib/dry/types/extensions/maybe.rb', line 33

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

#maybe?true

Returns:

  • (true)


26
27
28
# File 'lib/dry/types/extensions/maybe.rb', line 26

def maybe?
  true
end

#try(input) ⇒ Result::Success

Parameters:

  • input (Object)

Returns:



21
22
23
# File 'lib/dry/types/extensions/maybe.rb', line 21

def try(input)
  Result::Success.new(Maybe(type[input]))
end