Class: Dry::Monads::Try::Value

Inherits:
Dry::Monads::Try show all
Includes:
RightBiased::Right
Defined in:
lib/dry/monads/try.rb,
lib/dry/monads/maybe.rb,
lib/dry/monads/result.rb

Overview

Represents a result of a successful execution.

Constant Summary

Constants inherited from Dry::Monads::Try

DEFAULT_EXCEPTIONS

Instance Attribute Summary collapse

Attributes inherited from Dry::Monads::Try

#exception

Instance Method Summary collapse

Methods included from RightBiased::Right

#===, #and, #apply, #deconstruct, #deconstruct_keys, #discard, #flatten, included, #or, #or_fmap, #tee, #value!, #value_or, #|

Methods inherited from Dry::Monads::Try

[], #error?, pure, run, #to_monad, #value?

Constructor Details

#initialize(exceptions, value) ⇒ Value

Returns a new instance of Value.

Parameters:

  • exceptions (Array<Exception>)

    list of exceptions to be rescued

  • value (Object)

    the value to be stored in the monad



106
107
108
109
110
111
# File 'lib/dry/monads/try.rb', line 106

def initialize(exceptions, value)
  super()

  @catchable = exceptions
  @value = value
end

Instance Attribute Details

#catchableArray<Exception> (readonly)

Returns List of exceptions to rescue.

Returns:

  • (Array<Exception>)

    List of exceptions to rescue



102
103
104
# File 'lib/dry/monads/try.rb', line 102

def catchable
  @catchable
end

Instance Method Details

#bindObject, Try::Error

Calls the passed in Proc object with value stored in self and returns the result.

If proc is nil, it expects a block to be given and will yield to it.

Examples:

success = Dry::Monads::Try::Value.new(ZeroDivisionError, 10)
success.bind(->(n) { n / 2 }) # => 5
success.bind { |n| n / 0 } # => Try::Error(ZeroDivisionError: divided by 0)

Parameters:

  • args (Array<Object>)

    arguments that will be passed to a block if one was given, otherwise the first value assumed to be a Proc (callable) object and the rest of args will be passed to this object along with the internal value

Returns:



132
133
134
135
136
# File 'lib/dry/monads/try.rb', line 132

def bind(...)
  super
rescue *catchable => e
  Error.new(e)
end

#fmapTry::Value, Try::Error

Does the same thing as #bind except it also wraps the value in an instance of a Try monad. This allows for easier chaining of calls.

Examples:

success = Dry::Monads::Try::Value.new(ZeroDivisionError, 10)
success.fmap(&:succ).fmap(&:succ).value # => 12
success.fmap(&:succ).fmap { |n| n / 0 }.fmap(&:succ).value # => nil

Parameters:

  • args (Array<Object>)

    extra arguments for the block, arguments are being processes just as in #bind

Returns:



150
151
152
153
154
# File 'lib/dry/monads/try.rb', line 150

def fmap(...)
  Value.new(catchable, bind_call(...))
rescue *catchable => e
  Error.new(e)
end

#recover(*_errors) ⇒ Try::Value

Ignores values and returns self, see Error#recover

Parameters:

  • errors (Class)

    List of Exception subclasses

Returns:



171
172
173
# File 'lib/dry/monads/try.rb', line 171

def recover(*_errors)
  self
end

#to_maybeMaybe

Returns:



418
419
420
# File 'lib/dry/monads/maybe.rb', line 418

def to_maybe
  Dry::Monads::Maybe(@value)
end

#to_resultResult::Success

Returns:



445
446
447
# File 'lib/dry/monads/result.rb', line 445

def to_result
  Dry::Monads::Result::Success.new(@value)
end

#to_sString Also known as: inspect

Returns:

  • (String)


157
158
159
160
161
162
163
# File 'lib/dry/monads/try.rb', line 157

def to_s
  if Unit.equal?(@value)
    "Try::Value()"
  else
    "Try::Value(#{@value.inspect})"
  end
end