216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
# File 'lib/dsl/ferrari.rb', line 216
def when(*args)
clazz = AtomBuilder === args[0] ? nil : args.shift
is_not = false
is_collect = false
mode = :equals
while clazz.is_a? Symbol
if clazz == :not || clazz == :~
is_not = true
elsif clazz == :is_a? || clazz == :kind_of? || clazz == :instance_of?
mode = :inherits
elsif clazz == :collect
is_collect = true
elsif clazz == :exists?
raise 'The \'exists\' quantifier is not yet supported.'
end
clazz = args.empty? ? nil : args.shift
end
if clazz == nil
clazz = Object
mode = :inherits
end
deftemplate = Core::Template.new clazz, mode
atoms = []
@when_counter += 1
htag = Symbol === args[0] ? args.shift : GeneratedTag.new
head = Core::HeadAtom.new htag, deftemplate
@tags[htag] = @when_counter
args.each do |arg|
if arg.kind_of? Hash
arg.each do |ab,tag|
ab.tag = tag
ab.deftemplate = deftemplate
@tags[tag] = @when_counter
@methods[tag] = ab.name
atoms.push *ab.build_atoms(@tags, @methods, @when_counter)
end
elsif arg.kind_of? AtomBuilder
arg.tag = GeneratedTag.new
arg.deftemplate = deftemplate
@methods[arg.tag] = arg.name
atoms.push *arg.build_atoms(@tags, @methods, @when_counter)
elsif arg.kind_of? FunctionBuilder
atoms.push arg.build_atom(GeneratedTag.new, deftemplate)
elsif arg == false
raise 'The != operator is not allowed.'
else
raise "Invalid condition: #{arg}"
end
end
if is_not
p = mode==:inherits ? Core::NotInheritsPattern.new(head, atoms) :
Core::NotPattern.new(head, atoms)
else
p = mode==:inherits ? Core::InheritsPattern.new(head, atoms) :
is_collect ? Core::CollectPattern.new(head, atoms) :
Core::ObjectPattern.new(head, atoms)
end
@pattern = @pattern ? Core::AndPattern.new(@pattern, p) : p
end
|