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
|
# File 'lib/sentinel.rb', line 9
def observe(class_name, method_name, options = {})
sentinel = self
options = {
:method_to_notify => :notify,
:intercept => :after
}.merge(options)
if options[:intercept] == :before
calls = <<-CODE
sentinel.send("#{options[:method_to_notify]}", observer_opt, *args)
self.send("#{method_name}_without_observer", *args)
CODE
else
calls = <<-CODE
result = self.send("#{method_name}_without_observer", *args)
observer_opt[:result] = result
sentinel.send("#{options[:method_to_notify]}", observer_opt, *args)
result
CODE
end
body = <<-CODE
define_method "#{method_name}_with_observer" do |*args|
observer_opt = {:subject => self}
#{calls}
end
alias_method "#{method_name}_without_observer", method_name
alias_method method_name, "#{method_name}_with_observer"
CODE
if options[:class_method]
class_name.meta_eval do
eval body
end
else
class_name.send(:class_eval, body)
end
end
|