Class: Dry::Monads::Try

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/monads/try.rb,
lib/dry/monads/maybe.rb,
lib/dry/monads/either.rb,
lib/dry/monads/result.rb

Overview

Represents a value which can be either success or a failure (an exception). Use it to wrap code that can raise exceptions.

Direct Known Subclasses

Error, Value

Defined Under Namespace

Modules: Mixin Classes: Error, Value

Constant Summary collapse

DEFAULT_EXCEPTIONS =
[StandardError].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#exceptionException (readonly)

Returns Caught exception.

Returns:

  • (Exception)

    Caught exception


20
21
22
# File 'lib/dry/monads/try.rb', line 20

def exception
  @exception
end

Class Method Details

.[](*exceptions, &block) ⇒ Try::Value, Try::Error

Safely runs a block

Examples:

using Try with [] and a block (Ruby 2.5+)

include Dry::Monads::Try::Mixin

def safe_db_call
  Try[DatabaseError] { db_call }
end

Parameters:

  • exceptions (Array<Exception>)

Returns:

Raises:

  • (ArgumentError)

72
73
74
75
# File 'lib/dry/monads/try.rb', line 72

def [](*exceptions, &block)
  raise ArgumentError, 'At least one exception type required' if exceptions.empty?
  run(exceptions, block)
end

.pure(value, exceptions = DEFAULT_EXCEPTIONS) ⇒ Try::Value .pure(exceptions = DEFAULT_EXCEPTIONS, &block) ⇒ Try::Value

Wraps a value with Value

Overloads:

  • .pure(value, exceptions = DEFAULT_EXCEPTIONS) ⇒ Try::Value

    Parameters:

    • value (Object)

      value for wrapping

    • exceptions (Array<Exceptions>) (defaults to: DEFAULT_EXCEPTIONS)

      list of exceptions to rescue

    Returns:

  • .pure(exceptions = DEFAULT_EXCEPTIONS, &block) ⇒ Try::Value

    Parameters:

    • exceptions (Array<Exceptions>) (defaults to: DEFAULT_EXCEPTIONS)

      list of exceptions to rescue

    • block (Proc)

      value for wrapping

    Returns:


51
52
53
54
55
56
57
58
59
# File 'lib/dry/monads/try.rb', line 51

def pure(value = Undefined, exceptions = DEFAULT_EXCEPTIONS, &block)
  if value.equal?(Undefined)
    Value.new(DEFAULT_EXCEPTIONS, block)
  elsif block.nil?
    Value.new(exceptions, value)
  else
    Value.new(value, block)
  end
end

.run(exceptions, f) ⇒ Try::Value, Try::Error

Invokes a callable and if successful stores the result in the Value type, but if one of the specified exceptions was raised it stores it in a Error.

Parameters:

  • exceptions (Array<Exception>)

    list of exceptions to rescue

  • f (#call)

    callable object

Returns:


32
33
34
35
36
# File 'lib/dry/monads/try.rb', line 32

def run(exceptions, f)
  Value.new(exceptions, f.call)
rescue *exceptions => e
  Error.new(e)
end

Instance Method Details

#error?Boolean Also known as: failure?

Returns true for an instance of a Error monad.

Returns:

  • (Boolean)

85
86
87
# File 'lib/dry/monads/try.rb', line 85

def error?
  is_a?(Error)
end

#to_monadMaybe::Some, Maybe::None

Returns self.

Returns:


93
94
95
# File 'lib/dry/monads/try.rb', line 93

def to_monad
  self
end

#value?Boolean Also known as: success?

Returns true for an instance of a Value monad.

Returns:

  • (Boolean)

79
80
81
# File 'lib/dry/monads/try.rb', line 79

def value?
  is_a?(Value)
end