Class: Rescuer::Failure

Inherits:
Struct
  • Object
show all
Defined in:
lib/rescuer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exception, exceptions_to_rescue = nil) ⇒ Failure

Returns a new instance of Failure.

Raises:

  • (ArgumentError)


102
103
104
105
106
# File 'lib/rescuer.rb', line 102

def initialize(exception, exceptions_to_rescue = nil)
  super(exception, exceptions_to_rescue)
  raise ArgumentError, 'not an exception' unless exception.is_a? Exception
  freeze
end

Instance Attribute Details

#exceptionObject

Returns the value of attribute exception



101
102
103
# File 'lib/rescuer.rb', line 101

def exception
  @exception
end

#exceptions_to_rescueObject

Returns the value of attribute exceptions_to_rescue



101
102
103
# File 'lib/rescuer.rb', line 101

def exceptions_to_rescue
  @exceptions_to_rescue
end

Instance Method Details

#eachObject



140
141
142
# File 'lib/rescuer.rb', line 140

def each
  self
end

#failedObject



128
129
130
# File 'lib/rescuer.rb', line 128

def failed
  Success.new(exception)
end

#failure?Boolean



112
113
114
# File 'lib/rescuer.rb', line 112

def failure?
  true
end

#flat_mapObject



132
133
134
# File 'lib/rescuer.rb', line 132

def flat_map
  self
end

#flatten(depth = nil) ⇒ Object



161
162
163
# File 'lib/rescuer.rb', line 161

def flatten(depth = nil)
  self
end

#getObject



116
117
118
# File 'lib/rescuer.rb', line 116

def get
  raise exception
end

#get_or_else(default) ⇒ Object



120
121
122
# File 'lib/rescuer.rb', line 120

def get_or_else(default)
  default
end

#mapObject



136
137
138
# File 'lib/rescuer.rb', line 136

def map
  self
end

#or_else(other) ⇒ Object



124
125
126
# File 'lib/rescuer.rb', line 124

def or_else(other)
  other
end

#recoverObject



153
154
155
# File 'lib/rescuer.rb', line 153

def recover
  failed.map { |e| yield e }
end

#recover_withObject



157
158
159
# File 'lib/rescuer.rb', line 157

def recover_with
  failed.flat_map { |e| yield e }
end

#selectObject Also known as: find_all



144
145
146
# File 'lib/rescuer.rb', line 144

def select
  self
end

#success?Boolean



108
109
110
# File 'lib/rescuer.rb', line 108

def success?
  false
end

#transform(f_success, f_failure) ⇒ Object



149
150
151
# File 'lib/rescuer.rb', line 149

def transform(f_success, f_failure)
  failed.flat_map { |e| f_failure.call(e) }
end