Module: Nuggets::Object::RescueIfMixin

Included in:
Object
Defined in:
lib/nuggets/object/rescue_if_mixin.rb

Instance Method Summary collapse

Instance Method Details

#rescue_if(*args, &block) ⇒ Object

call-seq:

rescue_if([args]) { |err| ... }

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

Raises:

  • (::ArgumentError)


36
37
38
39
40
41
42
43
44
45
46
# 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, &block) ⇒ Object

call-seq:

rescue_unless([args]) { |err| ... }

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

Raises:

  • (::ArgumentError)


53
54
55
56
57
# 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