-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterceptor.rb
52 lines (40 loc) · 1.26 KB
/
interceptor.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
38
39
40
41
42
43
44
45
46
47
48
49
module Interceptor
def attach_interceptor(method_selection, &interceptor)
method_selection.each do |method_name|
(class << self; self end).class_eval do
orig_method_name = "orig_#{method_name}"
alias_method orig_method_name, method_name
define_method method_name do |*args|
yield method(orig_method_name), *args
end
end if self.respond_to? method_name.to_s
end if methods.respond_to? 'each'
self
end
end
class SwedishDict
def initialize()
@dictionary = {'car' => 'bil', 'chair' => 'stol'}
end
def translate(original_word)
@dictionary[original_word]
end
end
normal_dict = SwedishDict.new
extended_dict = normal_dict.clone.extend(Interceptor)
extended_dict.attach_interceptor [:translate] do |meth, *args|
puts "LOG: entering method"
result = meth.call *args
puts "LOG: got result '#{result}'"
puts "LOG: leaving method"
result
end
puts normal_dict.translate 'car'
# outputs:
# bil
puts extended_dict.translate 'car' #
# outputs:
# LOG: entering method
# LOG: got result 'bil'
# LOG: leaving method
# bil