Module: ExceptionSwallower::ClassMethods

Defined in:
lib/exception_swallower.rb

Instance Method Summary collapse

Instance Method Details

#handle_class_method_exceptions(method_name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/exception_swallower.rb', line 22

def handle_class_method_exceptions(method_name)
  swallowed_exceptions = self.singleton_class.instance_variable_get("@swallowed_exceptions")
  return if swallowed_exceptions.nil?

  old_method = method(method_name)
  @ignoring_added_methods = true
  define_singleton_method(method_name) do |*args, &block|
    begin
      old_method.call(*args, &block)
    rescue *swallowed_exceptions; end
  end

  @ignoring_added_methods = false
end

#handle_instance_method_exceptions(method_name) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/exception_swallower.rb', line 37

def handle_instance_method_exceptions(method_name)
  swallowed_exceptions = self.singleton_class.instance_variable_get("@swallowed_exceptions")
  return if swallowed_exceptions.nil?

  old_method = instance_method(method_name)
  @ignoring_added_methods = true
  define_method(method_name) do |*args, &block|
    begin
      old_method.bind(self).call(*args, &block)
    rescue *swallowed_exceptions; end
  end

  @ignoring_added_methods = false
end

#method_added(method_name) ⇒ Object



17
18
19
20
# File 'lib/exception_swallower.rb', line 17

def method_added(method_name)
  super
  handle_instance_method_exceptions(method_name) unless @ignoring_added_methods
end

#singleton_method_added(method_name) ⇒ Object



12
13
14
15
# File 'lib/exception_swallower.rb', line 12

def singleton_method_added(method_name)
  super
  handle_class_method_exceptions(method_name) unless @ignoring_added_methods
end

#swallow_exceptions(*exceptions) ⇒ Object



8
9
10
# File 'lib/exception_swallower.rb', line 8

def swallow_exceptions(*exceptions)
  self.singleton_class.instance_variable_set("@swallowed_exceptions", exceptions)
end