module Nuggets::Object::RescueIfMixin

Public Instance Methods

rescue_if([args]) { |err| ... } click to toggle source

Rescue exceptions matching args, or StandardError if not given, if block returns true.

# File lib/nuggets/object/rescue_if_mixin.rb, line 36
def rescue_if(*args, &block)
  raise ::ArgumentError, 'no block given' unless block

  args = [::StandardError] if args.empty?

  ::Module.new {
    define_singleton_method(:===) { |err|
      block[err] if args.any? { |arg| arg === err }
    }
  }
end
rescue_unless([args]) { |err| ... } click to toggle source

Rescue exceptions matching args, or StandardError if not given, unless block returns true.

# File lib/nuggets/object/rescue_if_mixin.rb, line 53
def rescue_unless(*args, &block)
  raise ::ArgumentError, 'no block given' unless block

  rescue_if(*args) { |err| !block[err] }
end