Class: Mayak::Monads::Try::Failure

Inherits:
Object
  • Object
show all
Extended by:
T::Generic, T::Sig
Includes:
Mayak::Monads::Try
Defined in:
lib/mayak/monads/try.rb

Constant Summary collapse

Value =
type_member

Instance Method Summary collapse

Methods included from Mayak::Monads::Try

#==, #as, check, #failure_as, guard, #recover_with_try, sequence

Constructor Details

#initialize(value) ⇒ Failure

Returns a new instance of Failure.



400
401
402
# File 'lib/mayak/monads/try.rb', line 400

def initialize(value)
  @failure = T.let(value, StandardError)
end

Instance Method Details

#either(failure_branch, success_branch) ⇒ Object



479
480
481
# File 'lib/mayak/monads/try.rb', line 479

def either(failure_branch, success_branch)
  failure_branch.call(@failure)
end

#failureObject



440
441
442
# File 'lib/mayak/monads/try.rb', line 440

def failure
  @failure
end

#failure?Boolean

Returns:

  • (Boolean)


450
451
452
# File 'lib/mayak/monads/try.rb', line 450

def failure?
  true
end

#failure_or(value) ⇒ Object



465
466
467
# File 'lib/mayak/monads/try.rb', line 465

def failure_or(value)
  @failure
end

#filter_or(error, &blk) ⇒ Object



435
436
437
# File 'lib/mayak/monads/try.rb', line 435

def filter_or(error, &blk)
  self
end

#flat_map(&blk) ⇒ Object



423
424
425
426
427
428
# File 'lib/mayak/monads/try.rb', line 423

def flat_map(&blk)
  T.cast(
    self,
    Try[T.type_parameter(:NewValue)]
  )
end

#flat_map_failure(&blk) ⇒ Object



507
508
509
# File 'lib/mayak/monads/try.rb', line 507

def flat_map_failure(&blk)
  blk.call(@failure)
end

#map(&blk) ⇒ Object



410
411
412
413
414
415
# File 'lib/mayak/monads/try.rb', line 410

def map(&blk)
  T.cast(
    self,
    Try[T.type_parameter(:NewValue)]
  )
end

#map_failure(&blk) ⇒ Object



497
498
499
# File 'lib/mayak/monads/try.rb', line 497

def map_failure(&blk)
  Mayak::Monads::Try::Failure[Value].new(blk.call(@failure))
end

#recover(value) ⇒ Object



549
550
551
# File 'lib/mayak/monads/try.rb', line 549

def recover(value)
  ::Mayak::Monads::Try::Success[T.any(T.type_parameter(:NewValue), Value)].new(value)
end

#recover_on(error_type, &blk) ⇒ Object



535
536
537
538
539
540
541
# File 'lib/mayak/monads/try.rb', line 535

def recover_on(error_type, &blk)
  if @failure.is_a?(error_type)
    ::Mayak::Monads::Try::Success[T.any(T.type_parameter(:NewValue), Value)].new(blk.call(@failure))
  else
    self
  end
end

#recover_with(&blk) ⇒ Object



559
560
561
# File 'lib/mayak/monads/try.rb', line 559

def recover_with(&blk)
  ::Mayak::Monads::Try::Success[T.any(T.type_parameter(:NewValue), Value)].new(blk.call(@failure))
end

#success?Boolean

Returns:

  • (Boolean)


445
446
447
# File 'lib/mayak/monads/try.rb', line 445

def success?
  false
end

#success_or(value) ⇒ Object



460
461
462
# File 'lib/mayak/monads/try.rb', line 460

def success_or(value)
  value
end

#tee(&blk) ⇒ Object



488
489
490
# File 'lib/mayak/monads/try.rb', line 488

def tee(&blk)
  self
end

#to_maybeObject



522
523
524
# File 'lib/mayak/monads/try.rb', line 522

def to_maybe
  ::Mayak::Monads::Maybe::None[Value].new
end

#to_result(&blk) ⇒ Object



517
518
519
# File 'lib/mayak/monads/try.rb', line 517

def to_result(&blk)
  Mayak::Monads::Result::Failure.new(blk.call(@failure))
end