Class: Fear::Failure

Inherits:
Object
  • Object
show all
Includes:
RightBiased::Left, 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

Constants included from Utils

Utils::UNDEFINED

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RightBiased::Left

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

Methods included from Utils

assert_arg_or_block!, assert_type!, return_or_call_proc, with_block_or_argument

Methods included from Try

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

Constructor Details

#initialize(exception) ⇒ Failure


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

def initialize(exception)
  @exception = exception
end

Instance Attribute Details

#exceptionObject (readonly)

Returns the value of attribute exception


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

def exception
  @exception
end

Instance Method Details

#==(other) ⇒ Boolean


84
85
86
# File 'lib/fear/failure.rb', line 84

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

#===(other) ⇒ Boolean

Used in case statement


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

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

#failure?true


29
30
31
# File 'lib/fear/failure.rb', line 29

def failure?
  true
end

#flattenFailure


46
47
48
# File 'lib/fear/failure.rb', line 46

def flatten
  self
end

#getObject

Raises:


34
35
36
# File 'lib/fear/failure.rb', line 34

def get
  raise exception
end

#inspectString Also known as: to_s


100
101
102
# File 'lib/fear/failure.rb', line 100

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

#or_else(*args) ⇒ Try


39
40
41
42
43
# File 'lib/fear/failure.rb', line 39

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

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

Yield Parameters:

Yield Returns:

  • (any)

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

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:


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

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


51
52
53
# File 'lib/fear/failure.rb', line 51

def select
  self
end

#success?Boolean


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

def success?
  false
end

#to_eitherLeft


78
79
80
# File 'lib/fear/failure.rb', line 78

def to_either
  Left.new(exception)
end