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, #to_a, #to_option

Constructor Details

#initialize(exception) ⇒ Failure

Returns a new instance of Failure.

Parameters:

  • (StandardError)


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

def initialize(exception)
  @exception = exception
end

Instance Attribute Details

#exceptionObject (readonly)

Returns the value of attribute exception.



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

def exception
  @exception
end

Instance Method Details

#===(other) ⇒ Boolean

Used in case statement

Parameters:

  • other (any)

Returns:

  • (Boolean)


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

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

#failure?true

Returns:

  • (true)


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

def failure?
  true
end

#flattenFailure

Returns self.

Returns:



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

def flatten
  self
end

#getObject

Raises:



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

def get
  fail exception
end

#or_else(*args) ⇒ Try

Returns of calling block.

Returns:

  • (Try)

    of calling block



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

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

#recover {|| ... } ⇒ Try

Yield Parameters:

  • (Exception)

Yield Returns:

  • (any)

Returns:



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

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

#recover_with {|| ... } ⇒ Try

Yield Parameters:

  • (Exception)

Yield Returns:

Returns:



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

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

#selectFailure

Returns self.

Returns:



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

def select
  self
end

#success?Boolean

Returns:

  • (Boolean)


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

def success?
  false
end

#to_eitherLeft

Returns:



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

def to_either
  Left.new(exception)
end