Module: Fear::Try::Mixin

Included in:
Mixin
Defined in:
lib/fear/try.rb

Overview

Include this mixin to access convenient factory methods.

Examples:

include Fear::Try::Mixin

Try { 4/2 } #=> #<Fear::Success value=2>
Try { 4/0 } #=> #<Fear::Failure value=#<ZeroDivisionError: divided by 0>>
Success(2)  #=> #<Fear::Success value=2>

Instance Method Summary collapse

Instance Method Details

#Failure(exception) ⇒ Failure

Parameters:

  • exception (StandardError)

Returns:



251
252
253
254
# File 'lib/fear/try.rb', line 251

def Failure(exception)
  fail TypeError, "not an error: #{exception}" unless exception.is_a?(StandardError)
  Failure.new(exception)
end

#Success(value) ⇒ Success

Parameters:

  • value (any)

Returns:



259
260
261
# File 'lib/fear/try.rb', line 259

def Success(value)
  Success.new(value)
end

#TryTry

Constructs a Try using the block. This method will ensure any non-fatal exception )is caught and a Failure object is returned.

Returns:



242
243
244
245
246
# File 'lib/fear/try.rb', line 242

def Try
  Success.new(yield)
rescue => error
  Failure.new(error)
end