Class: Dry::Monads::Try

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/monads/try.rb,
lib/dry/monads/maybe.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



16
17
18
# File 'lib/dry/monads/try.rb', line 16

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)


68
69
70
71
72
# File 'lib/dry/monads/try.rb', line 68

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:



47
48
49
50
51
52
53
54
55
# File 'lib/dry/monads/try.rb', line 47

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:



28
29
30
31
32
# File 'lib/dry/monads/try.rb', line 28

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)


82
83
84
# File 'lib/dry/monads/try.rb', line 82

def error?
  is_a?(Error)
end

#to_monadTry::Value, Try::Error

Returns self.

Returns:



90
91
92
# File 'lib/dry/monads/try.rb', line 90

def to_monad
  self
end

#value?Boolean Also known as: success?

Returns true for an instance of a Value monad.

Returns:

  • (Boolean)


76
77
78
# File 'lib/dry/monads/try.rb', line 76

def value?
  is_a?(Value)
end