32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/anchor/hooks.rb', line 32
def add_hook_for_method(type, method, &block)
level, name = case method; when Hash; method.shift; when Symbol; [:instance, method]; end
in_scope = :metaclass if level == :singleton or not self.is_a?(Class)
return Anchor.hook(class << self; self; end) do
self.send(type, name, &block)
end if in_scope == :metaclass
instance_exec do
hooks = @hooks ||= {}
@hooks[name] ||= {}
@hooks[name][type] ||= []
@hooks[name][type] << block
orginal_methods = @orginal_methods ||= {}
unless @orginal_methods[name]
orginal_methods[name] = self.instance_method(name)
define_method name do
ihooks = hooks[name]
orginal_method = orginal_methods[name].bind(self)
ihooks[:before].each {|proc| proc.call} if ihooks[:before]
retval=orginal_method.call
ihooks[:after].each {|proc| proc.call} if ihooks[:after]
retval
end
end
end
end
|