Class: Divergent::Failure

Inherits:
Object
  • Object
show all
Includes:
Try
Defined in:
lib/divergent/try.rb

Overview

:nodoc: true

Instance Method Summary collapse

Methods included from Try

#get_or_else, #or_else, unit

Methods included from Monad

included

Constructor Details

#initialize(error) ⇒ Failure

Returns a new instance of Failure.



214
215
216
217
# File 'lib/divergent/try.rb', line 214

def initialize(error)
  raise 'error should be an StandardError' unless error.is_a? StandardError
  @error = error
end

Instance Method Details

#each(&block) ⇒ Object



235
236
237
# File 'lib/divergent/try.rb', line 235

def each(&block)
  nil
end

#failedObject



266
267
268
# File 'lib/divergent/try.rb', line 266

def failed
  Success.new(@error)
end

#failure?Boolean

Returns:

  • (Boolean)


223
224
225
# File 'lib/divergent/try.rb', line 223

def failure?
  true
end

#filter(&block) ⇒ Object



243
244
245
# File 'lib/divergent/try.rb', line 243

def filter(&block)
  self
end

#flattenObject



278
279
280
# File 'lib/divergent/try.rb', line 278

def flatten
  self
end

#fmapObject



219
220
221
# File 'lib/divergent/try.rb', line 219

def fmap()
  self
end

#getObject

Raises:

  • (@error)


231
232
233
# File 'lib/divergent/try.rb', line 231

def get
  raise @error
end

#map(&block) ⇒ Object



239
240
241
# File 'lib/divergent/try.rb', line 239

def map(&block)
  self
end

#recover(*errors) ⇒ Object



256
257
258
259
260
261
262
263
264
# File 'lib/divergent/try.rb', line 256

def recover(*errors)
  unless errors.empty? || \
         errors.any? { |clazz| @error.is_a?(clazz) }
    return self
  end

  t = yield(@error)
  Success.new(t)
end

#recover_with(*errors) {|@error| ... } ⇒ Object

Yields:

  • (@error)


247
248
249
250
251
252
253
254
# File 'lib/divergent/try.rb', line 247

def recover_with(*errors)
  unless errors.empty? || \
         errors.any? { |clazz| @error.is_a?(clazz) }
    return self
  end

  yield(@error)
end

#success?Boolean

Returns:

  • (Boolean)


227
228
229
# File 'lib/divergent/try.rb', line 227

def success?
  false
end

#to_sObject Also known as: inspect



282
283
284
# File 'lib/divergent/try.rb', line 282

def to_s
  "Failure<#{@error}>"
end

#transform(_s, f) ⇒ Object



270
271
272
273
274
275
276
# File 'lib/divergent/try.rb', line 270

def transform(_s, f)
  begin
    f.call(@error)
  rescue => e
    Failure.new(e)
  end
end