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
|
# File 'lib/chocolate_shell.rb', line 3
def self.included(klass)
imethods = klass.instance_methods(false)
klass.send(:define_method, "initialize") do |value|
@result = value
super()
end
klass.send(:define_method, "on_error") do |handler|
err =
if @method && handler.respond_to?(@method)
handler.__send__(@method, @result, @error) || @error
elsif @method
@error
end
@result = nil if err
[@result, err]
end
imethods.each do |method|
klass.send(:define_method, "protected_#{method}") do
return self if @failed
begin
@result = __send__("original_#{method}", @result)
rescue => e
@failed = true
@error = e
@method = method
end
self
end
klass.send(:alias_method, "original_#{method}", method)
klass.send(:alias_method, method, "protected_#{method}")
end
end
|