123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
# File 'lib/define_method_handler.rb', line 123
def define_method_handler(mname, *options, &blk)
options = options.inject(&:merge) || {}
options.merge!(@method_handler_options) if @method_handler_options
@method_handlers ||= Hash.new
@method_handlers[mname] ||= Array.new
@next_priority = (@next_priority || 0) + 1
mh = MethodHandler.new(blk, @next_priority, (options[:group].to_a + :default.to_a), options[:priority] || 0)
mh.method_name = options[:method]
@method_handlers[mname] << mh
include ChainMethods
@method_handlers[mname].sort!{|x,y|
if x.priority == y.priority
x.second_priority <=> y.second_priority
else
x.priority <=> y.priority
end
}
define_method(mname) do |*x, &callblk|
retval = nil
self.class.method_handlers[mname].reject{|mhh|
mhh.group.count { |gr|
@disabled_handler_groups ||= Hash.new
@disabled_handler_groups[mname]||= Set.new
@disabled_handler_groups[mname].include? gr
} > 0 }.reverse_each do |mhh|
if mhh.execute?(self,*x)
tmp_method = "tmpmethod#{rand(1000000)}#{Time.now.to_i}"
begin
if mhh.method_name
retval = send(mhh.method_name, *x, &callblk)
break
else
self.class.class_eval do
define_method(tmp_method, &mhh.processor)
end
retval = method(tmp_method).call(*x, &callblk)
break
end
ensure
unless mhh.method_name
begin
self.class.class_eval do
remove_method(tmp_method)
end
rescue NameError
end
end
end
end
end
retval
end
mh
end
|