Class: Fear::Failure

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

Constant Summary collapse

EXTRACTOR =
proc do |try|
  if Fear::Failure === try
    Fear.some([try.exception])
  else
    Fear.none
  end
end

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)


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

def initialize(exception)
  @exception = exception
end

Instance Attribute Details

#exceptionObject (readonly)

Returns the value of attribute exception.



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

def exception
  @exception
end

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

  • other (Any)

Returns:

  • (Boolean)


82
83
84
# File 'lib/fear/failure.rb', line 82

def ==(other)
  other.is_a?(Failure) && exception == other.exception
end

#===(other) ⇒ Boolean

Used in case statement

Parameters:

  • other (any)

Returns:

  • (Boolean)


89
90
91
92
93
94
95
# File 'lib/fear/failure.rb', line 89

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

#failure?true

Returns:

  • (true)


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

def failure?
  true
end

#flattenFailure

Returns self.

Returns:



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

def flatten
  self
end

#getObject

Raises:



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

def get
  raise exception
end

#inspectString Also known as: to_s

Returns:

  • (String)


98
99
100
# File 'lib/fear/failure.rb', line 98

def inspect
  "#<Fear::Failure exception=#{exception.inspect}>"
end

#or_else(*args) ⇒ Try

Returns of calling block.

Returns:

  • (Try)

    of calling block



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

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

#recover {|| ... } ⇒ Fear::Try

Yield Parameters:

Yield Returns:

  • (any)

Returns:



67
68
69
70
71
72
73
# File 'lib/fear/failure.rb', line 67

def recover
  Fear.matcher { |m| yield(m) }
    .and_then { |v| Success.new(v) }
    .call_or_else(exception) { self }
rescue StandardError => error
  Failure.new(error)
end

#recover_with {|| ... } ⇒ Fear::Try

Yield Parameters:

Yield Returns:

Returns:



56
57
58
59
60
61
62
# File 'lib/fear/failure.rb', line 56

def recover_with
  Fear.matcher { |m| yield(m) }
    .and_then { |result| result.tap { Utils.assert_type!(result, Success, Failure) } }
    .call_or_else(exception) { self }
rescue StandardError => error
  Failure.new(error)
end

#selectFailure

Returns self.

Returns:



49
50
51
# File 'lib/fear/failure.rb', line 49

def select
  self
end

#success?Boolean

Returns:

  • (Boolean)


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

def success?
  false
end

#to_eitherLeft

Returns:



76
77
78
# File 'lib/fear/failure.rb', line 76

def to_either
  Left.new(exception)
end