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, #default?, #initialize, #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) ⇒ Object Also known as: []



12
13
14
# File 'lib/dry/types/extensions/maybe.rb', line 12

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

#default(value) ⇒ Object



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

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

#maybe?Boolean

Returns:

  • (Boolean)


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

def maybe?
  true
end

#try(input) ⇒ Object



17
18
19
# File 'lib/dry/types/extensions/maybe.rb', line 17

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