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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
|
# File 'lib/adsl/extract/rails/active_record_metaclass_generator.rb', line 148
def generate_class
@ar_class.class_exec do
include ADSL::Parser
attr_accessor :adsl_ast
attr_accessible :adsl_ast
def initialize(attributes = {}, options = {})
attributes = {} if attributes.is_a?(MetaUnknown)
super({
:adsl_ast => ASTCreateObjset.new(:class_name => ASTIdent.new(:text => self.class.adsl_ast_class_name))
}.merge(attributes), options)
end
def save(*args); end
def save!(*args); end
def reorder(*args); self; end
def order(*args); self; end
def reorder(*args); self; end
def includes(*args); self; end
def all(*args); self; end
def scope_for_create; self; end
def id; self; end
def count_by_group(*args); MetaUnknown.new; end
def size; MetaUnknown.new; end
def length; MetaUnknown.new; end
def count; MetaUnknown.new; end
def map; MetaUnknown.new; end
def valid?(*args); MetaUnknown.new; end
def hash
@adsl_ast.hash
end
def take(*params)
self.class.new :adsl_ast => ASTOneOf.new(:objset => self.adsl_ast)
end
alias_method :take!, :take
alias_method :first, :take
alias_method :last, :take
alias_method :find, :take
def where(*args)
self.class.new :adsl_ast => ASTSubset.new(:objset => self.adsl_ast)
end
alias_method :only, :where
alias_method :except, :where
alias_method :my, :where
alias_method :paginate, :where def merge(other)
if other.adsl_ast.is_a? ASTAllOf
self
elsif self.adsl_ast.is_a? ASTAllOf
other
elsif other.is_a? ActiveRecord::Base
self.class.new(:adsl_ast => other.adsl_ast.block_replace{ |node|
self.adsl_ast.dup if node.is_a? ASTAllOf
})
else
self
end
end
def apply_finder_options(options)
options.include?(:conditions) ? self.class.new(:adsl_ast => ASTSubset.new(:objset => self.adsl_ast)) : self
end
def empty?
ASTEmpty.new :objset => self.adsl_ast
end
def +(other)
self.class.new :adsl_ast => ASTUnion.new(:objsets => [self.adsl_ast, other.adsl_ast])
end
def each(&block)
instrumenter = ::ADSL::Extract::Instrumenter.get_instance
var_name = ASTIdent.new(:text => block.parameters.first[1].to_s)
var = self.class.new(:adsl_ast => ADSL::Parser::ASTVariable.new(:var_name => var_name))
substmts = instrumenter.abb.in_stmt_frame var, &block
ASTForEach.new(
:objset => self.adsl_ast,
:var_name => var_name.dup,
:block => ASTBlock.new(:statements => substmts)
)
end
def include?(other)
other = other.adsl_ast if other.respond_to? :adsl_ast
if other.is_a? ASTNode and other.class.is_objset?
ASTIn.new :objset1 => other, :objset2 => self.adsl_ast
else
super
end
end
def <=(other); other.include? self; end
alias_method :>=, :include?
def ==(other)
other = other.adsl_ast if other.respond_to? :adsl_ast
if other.is_a? ASTNode and other.class.is_objset?
ASTEqual.new :objsets => [self.adsl_ast, other]
else
super
end
end
def !=(other)
other = other.adsl_ast if other.respond_to? :adsl_ast
if other.is_a? ASTNode and other.class.is_objset?
ASTNot.new(:subformula => ASTEqual.new(:objsets => [self.adsl_ast, other]))
else
super
end
end
def method_missing(method, *args, &block)
if without_by = ActiveRecordMetaclassGenerator.remove_by_from_method(method)
self.send(without_by)
elsif self.class.respond_to? method
begin
prev_scoped = self.class.scoped
self.class.scoped = self
return self.class.send method, *args, &block
ensure
self.class.scoped = prev_scoped
end
else
super
end
end
def respond_to?(method, include_all = false)
super || ActiveRecordMetaclassGenerator.remove_by_from_method(method) || self.class.respond_to?(method)
end
def build(*params)
return self unless self.adsl_ast.is_a?(ASTDereference)
self.class.new(:adsl_ast => ASTDereferenceCreate.new(
:objset => self.adsl_ast.objset,
:rel_name => self.adsl_ast.rel_name
))
end
alias_method :create, :build
alias_method :create!, :build
def <<(param)
return self unless self.adsl_ast.is_a?(ASTDereference)
return super unless param.respond_to? :adsl_ast
raise "Invalid type added on dereference: #{param.class.name} to #{self.class.name}" unless param.class <= self.class
ASTCreateTup.new(
:objset1 => self.adsl_ast.objset.dup,
:rel_name => self.adsl_ast.rel_name.dup,
:objset2 => param.adsl_ast.dup
)
end
alias_method :add, :<<
class << self
include ADSL::Parser
def ar_class
superclass
end
def adsl_ast_class_name
ActiveRecordMetaclassGenerator.adsl_ast_class_name(self)
end
def all(*params)
self.new :adsl_ast => ASTAllOf.new(:class_name => ASTIdent.new(:text => adsl_ast_class_name))
end
def scope_for_create; self; end
def scope_attributes?; false; end
def scoped; @scoped || all; end
def scoped=(scoped); @scoped = scoped; end
alias_method :order, :all
def calculate(*args); ::ADSL::Extract::Rails::MetaUnknown.new; end
alias_method :count, :calculate
alias_method :average, :calculate
alias_method :minimum, :calculate
alias_method :maximum, :calculate
alias_method :sum, :calculate
alias_method :pluck, :calculate
def find(*args)
self.all.take
end
def where(*args)
self.all.where
end
alias_method :only, :where
alias_method :except, :where
alias_method :my, :where
def build(*args)
new(*args)
end
def method_missing(method, *args, &block)
if method.to_s =~ /^find_.*$/
self.find
else
super
end
end
def respond_to?(method, include_all = false)
return true if method.to_s =~ /^find_.*$/
super
end
end
end
create_destroys @ar_class
@ar_class.send :default_scope, @ar_class.all
reflections(:polymorphic => false, :through => false).each do |assoc|
@ar_class.new.replace_method assoc.name do
self_adsl_ast = self.adsl_ast
target_class = assoc.class_name.constantize
result = target_class.new :adsl_ast => ASTDereference.new(
:objset => self_adsl_ast.dup,
:rel_name => ASTIdent.new(:text => assoc.name.to_s)
)
if assoc.macro == :has_many
result.singleton_class.send :define_method, :delete do |*args|
object = if args.empty?
self
elsif args.length == 1
self.find
else
self.where
end
if [:delete, :delete_all].include? assoc.options[:dependent]
object == self ? super() : object.delete
elsif [:destroy, :destroy_all].include? assoc.options[:dependent]
object.destroy
else
[ASTDeleteTup.new(
:objset1 => self_adsl_ast.dup,
:rel_name => ASTIdent.new(:text => assoc.name.to_s),
:objset2 => object.adsl_ast
)]
end
end
elsif assoc.macro == :has_and_belongs_to_many
result.singleton_class.send :define_method, :delete do |*args|
object = if args.empty?
self
elsif args.length == 1
self.find
else
self.where
end
[ASTDeleteTup.new(
:objset1 => self_adsl_ast.dup,
:rel_name => ASTIdent.new(:text => assoc.name.to_s),
:objset2 => object.adsl_ast
)]
end
end
result
end
@ar_class.new.replace_method "#{assoc.name}=" do |other|
ASTSetTup.new(
:objset1 => self.adsl_ast,
:rel_name => ASTIdent.new(:text => assoc.name.to_s),
:objset2 => other.adsl_ast
)
end
if assoc.macro == :belongs_to
@ar_class.class_eval <<-ruby
alias_method :#{assoc.foreign_key}, :#{assoc.name}
alias_method :#{assoc.foreign_key}=, :#{assoc.name}=
ruby
end
end
reflections(:polymorphic => false, :through => true).each do |assoc|
@ar_class.new.replace_method assoc.name do
through_assoc = assoc.through_reflection
source_assoc = assoc.source_reflection
first_step = self.send through_assoc.name
result = first_step.send source_assoc.name
result.singleton_class.class_exec do
def build(*args)
self.class.new(:adsl_ast => ASTDereferenceCreate.new(
:rel_name => self.adsl_ast.rel_name,
:objset => ASTDereferenceCreate.new(
:objset => self.adsl_ast.objset.objset,
:rel_name => self.adsl_ast.objset.rel_name
)
))
end
def <<(param)
target = param.adsl_ast
intermed_deref = self.adsl_ast
source_deref = self.adsl_ast.objset
ASTCreateTup.new(
:objset1 => ASTDereferenceCreate.new(
:objset => source_deref.objset,
:rel_name => source_deref.rel_name,
),
:rel_name => intermed_deref.rel_name,
:objset2 => target.dup
)
end
alias_method :add, :<<
end
result
end
@ar_class.new.replace_method "#{assoc.name}=" do |other|
through_assoc = assoc.through_reflection
source_assoc = assoc.source_reflection
join_class_name = through_assoc.class_name.constantize.adsl_ast_class_name
origin_name = ASTIdent.new :text => "#{self.class.name.underscore}__#{through_assoc.name}__origin"
target_name = ASTIdent.new :text => "#{self.class.name.underscore}__#{through_assoc.name}__target"
iter_name = ASTIdent.new :text => "#{self.class.name.underscore}__#{through_assoc.name}__iterator"
join_name = ASTIdent.new :text => "#{self.class.name.underscore}__#{through_assoc.name}__join_object"
[
ASTAssignment.new(:var_name => origin_name.dup, :objset => self.adsl_ast),
ASTAssignment.new(:var_name => target_name.dup, :objset => other.adsl_ast),
ASTDeleteObj.new(:objset => ASTDereference.new(
:objset => ASTVariable.new(:var_name => origin_name.dup),
:rel_name => ASTIdent.new(:text => through_assoc.name.to_s)
)),
ASTForEach.new(
:var_name => iter_name,
:objset => ASTVariable.new(:var_name => target_name.dup),
:block => ASTBlock.new(:statements => [
ASTAssignment.new(
:var_name => join_name,
:objset => ASTCreateObjset.new(:class_name => ASTIdent.new(:text => join_class_name))
),
ASTCreateTup.new(
:objset1 => ASTVariable.new(:var_name => origin_name.dup),
:rel_name => ASTIdent.new(:text => through_assoc.name.to_s),
:objset2 => ASTVariable.new(:var_name => join_name.dup)
),
ASTCreateTup.new(
:objset1 => ASTVariable.new(:var_name => join_name.dup),
:rel_name => ASTIdent.new(:text => source_assoc.name.to_s),
:objset2 => ASTVariable.new(:var_name => iter_name.dup)
)
])
)
]
end
end
reflections(:polymorphic => true).each do |assoc|
@ar_class.new.replace_method assoc.name do
ADSL::Extract::Rails::MetaUnknown.new
end
end
adsl_ast_parent_name = parent_classname
adsl_ast_relations = reflections(:polymorphic => false, :through => false).map{ |ref| reflection_to_adsl_ast ref }
@ar_class.singleton_class.send :define_method, :adsl_ast do
ASTClass.new(
:name => ASTIdent.new(:text => adsl_ast_class_name),
:parent_name => adsl_ast_parent_name,
:relations => adsl_ast_relations
)
end
@ar_class
end
|