- Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathexception_handler2.rb
37 lines (29 loc) · 762 Bytes
/
exception_handler2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__),'..','lib'))
require'aspector'
# Example class to which we will apply our aspects
classExampleClass
defself.test(input)
putsinput.upcase
end
deftest(input)
putsinput.upcase
end
end
# Aspect used to handle exceptions
classExceptionHandler < Aspector::Base
targetdo
defhandle_exception(proxy, *args, &block)
proxy.call(*args, &block)
rescue=>e
puts"Rescued: #{e}"
end
end
around:handle_exception
end
ExceptionHandler.apply(ExampleClass,method: :test,class_methods: true)
ExampleClass.test('good')
ExampleClass.test(nil)
ExceptionHandler.apply(ExampleClass,method: :test)
instance=ExampleClass.new
instance.test('good')
instance.test(nil)