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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/simple_record/callbacks.rb', line 38
def self.included(base)
base.instance_eval <<-endofeval
def callbacks
@callbacks ||= {}
@callbacks
end
endofeval
@@callbacks.each do |callback|
base.class_eval <<-endofeval
def run_#{callback}
# puts 'CLASS CALLBACKS for ' + self.inspect + ' = ' + self.class.callbacks.inspect
return true if self.class.callbacks.nil?
cnames = self.class.callbacks['#{callback}']
cnames = [] if cnames.nil?
# cnames += super.class.callbacks['#{callback}'] unless super.class.callbacks.nil?
# puts 'cnames for #{callback} = ' + cnames.inspect
return true if cnames.nil?
cnames.each { |name|
#puts 'run_ #{name}'
if eval(name) == false # nil should be an ok return, only looking for false
return false
end
}
# super.run_#{callback}
return true
end
endofeval
base.instance_eval <<-endofeval
# puts 'defining callback=' + callback + ' for ' + self.inspect
#we first have to make an initialized array for each of the callbacks, to prevent problems if they are not called
def #{callback}(*args)
# puts 'callback called in ' + self.inspect + ' with ' + args.inspect
#make_dirty(arg_s, value)
#self[arg.to_s]=value
#puts 'value in callback #{callback}=' + value.to_s
args.each do |arg|
cnames = callbacks['#{callback}']
#puts '\tcnames1=' + cnames.inspect + ' for class ' + self.inspect
cnames = [] if cnames.nil?
cnames << arg.to_s if cnames.index(arg.to_s).nil?
#puts '\tcnames2=' + cnames.inspect
callbacks['#{callback}'] = cnames
end
end
endofeval
end
end
|