class Dry::Types::Maybe

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

@api public

Public Instance Methods

call_safe(input = Undefined) { |output| ... } click to toggle source

@param [Dry::Monads::Maybe, Object] input

@return [Dry::Monads::Maybe]

@api private

# File lib/dry/types/extensions/maybe.rb, line 41
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) click to toggle source

@param [Dry::Monads::Maybe, Object] input

@return [Dry::Monads::Maybe]

@api private

# File lib/dry/types/extensions/maybe.rb, line 25
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) click to toggle source

@param [Object] value

@see Dry::Types::Builder#default

@raise [ArgumentError] if nil provided as default value

@api public

Calls superclass method Dry::Types::Builder#default
# File lib/dry/types/extensions/maybe.rb, line 81
def default(value)
  if value.nil?
    raise ArgumentError, "nil cannot be used as a default of a maybe type"
  else
    super
  end
end
default?() click to toggle source

@return [true]

@api public

# File lib/dry/types/extensions/maybe.rb, line 70
def default?
  true
end
try(input = Undefined) click to toggle source

@param [Object] input

@return [Result::Success]

@api public

# File lib/dry/types/extensions/maybe.rb, line 57
def try(input = Undefined)
  result = type.try(input)

  if result.success?
    Result::Success.new(Maybe(result.input))
  else
    result
  end
end