Class: Fear::Failure

Inherits:
Object
  • Object
show all
Includes:
Try
Defined in:
lib/fear/failure.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Try

#any?, #each, #flat_map, #get_or_else, #include?, #map, #match, matcher, #to_option

Constructor Details

#initialize(exception) ⇒ Failure

Returns a new instance of Failure.

Parameters:

  • (StandardError)


9
10
11
# File 'lib/fear/failure.rb', line 9

def initialize(exception)
  @exception = exception
end

Instance Attribute Details

#exceptionObject (readonly)

Returns the value of attribute exception.



13
14
15
# File 'lib/fear/failure.rb', line 13

def exception
  @exception
end

Instance Method Details

#===(other) ⇒ Boolean

Used in case statement

Parameters:

  • other (any)

Returns:

  • (Boolean)


75
76
77
78
79
80
81
# File 'lib/fear/failure.rb', line 75

def ===(other)
  if other.is_a?(Failure)
    exception === other.exception
  else
    super
  end
end

#failure?true

Returns:

  • (true)


21
22
23
# File 'lib/fear/failure.rb', line 21

def failure?
  true
end

#flattenFailure

Returns self.

Returns:



38
39
40
# File 'lib/fear/failure.rb', line 38

def flatten
  self
end

#getObject

Raises:



26
27
28
# File 'lib/fear/failure.rb', line 26

def get
  raise exception
end

#or_else(*args) ⇒ Try

Returns of calling block.

Returns:

  • (Try)

    of calling block



31
32
33
34
35
# File 'lib/fear/failure.rb', line 31

def or_else(*args)
  super
rescue StandardError => error
  Failure.new(error)
end

#recover {|| ... } ⇒ Try

Yield Parameters:

  • (Exception)

Yield Returns:

  • (any)

Returns:



61
62
63
64
65
# File 'lib/fear/failure.rb', line 61

def recover
  Success.new(yield(exception))
rescue StandardError => error
  Failure.new(error)
end

#recover_with {|| ... } ⇒ Try

Yield Parameters:

  • (Exception)

Yield Returns:

Returns:



50
51
52
53
54
55
56
# File 'lib/fear/failure.rb', line 50

def recover_with
  yield(exception).tap do |result|
    Utils.assert_type!(result, Success, Failure)
  end
rescue StandardError => error
  Failure.new(error)
end

#selectFailure

Returns self.

Returns:



43
44
45
# File 'lib/fear/failure.rb', line 43

def select
  self
end

#success?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/fear/failure.rb', line 16

def success?
  false
end

#to_eitherLeft

Returns:



68
69
70
# File 'lib/fear/failure.rb', line 68

def to_either
  Left.new(exception)
end