Class: ADSL::Extract::Rails::ActiveRecordMetaclassGenerator

Inherits:
Object
  • Object
show all
Includes:
Parser
Defined in:
lib/adsl/extract/rails/active_record_metaclass_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ar_class) ⇒ ActiveRecordMetaclassGenerator

Returns a new instance of ActiveRecordMetaclassGenerator.



14
15
16
# File 'lib/adsl/extract/rails/active_record_metaclass_generator.rb', line 14

def initialize(ar_class)
  @ar_class = ar_class
end

Class Method Details

.adsl_ast_class_name(klass) ⇒ Object



26
27
28
# File 'lib/adsl/extract/rails/active_record_metaclass_generator.rb', line 26

def self.adsl_ast_class_name(klass)
  klass.name.sub('::', '_')
end

.remove_by_from_method(method) ⇒ Object



30
31
32
# File 'lib/adsl/extract/rails/active_record_metaclass_generator.rb', line 30

def self.remove_by_from_method(method)
  method.to_s.match(/^([^_]+)_.*/)[1].to_sym if /^[^_]+_by_.*$/ =~ method.to_s
end

Instance Method Details

#create_destroys(new_class) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/adsl/extract/rails/active_record_metaclass_generator.rb', line 103

def create_destroys(new_class)
  refls = reflections :this_class => nil
  new_class.send :define_method, :destroy do |*args|
    stmts = []
    object = if self.adsl_ast.objset_has_side_effects?
      var_name = ASTIdent.new(:text => "__delete_#{ self.class.adsl_ast_class_name }_temp_var")
      stmts << ASTAssignment.new(:var_name => var_name.dup, :objset => self.adsl_ast)
      self.class.new :adsl_ast => ASTVariable.new(:var_name => var_name.dup)
    else
      self
    end
    
    refls.each do |refl|
      next unless [:delete, :delete_all, :destroy, :destroy_all].include? refl.options[:dependent]
        
      if refl.options[:dependent] == :destroy or refl.options[:dependent] == :destroy_all
        if refl.through_reflection.nil?
          stmts += object.send(refl.name).destroy
        else
          stmts += object.send(refl.through_reflection.name).destroy
        end
      else
        if refl.through_reflection.nil?
          stmts += object.send(refl.name).delete
        else
          stmts += object.send(refl.through_reflection.name).delete
        end
      end
    end

    stmts << ASTDeleteObj.new(:objset => object.adsl_ast)

    stmts
  end
  new_class.send(:define_method, :destroy!    ){ |*args| destroy *args }
  new_class.send(:define_method, :destroy_all ){ |*args| destroy *args }

  new_class.send :define_method, :delete do |*args|
    [ASTDeleteObj.new(:objset => adsl_ast)]
  end
  new_class.send(:define_method, :delete!   ){ |*args| delete *args }
  new_class.send(:define_method, :delete_all){ |*args| delete *args }
  new_class.send(:define_method, :clear     ){ |*args| delete *args }
end

#generate_classObject



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

    # no-ops
    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   # used to allow foreign key assignment

    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    # will_paginate
    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
        # the scope is on the right hand side; so we can just replace all AllOfs in the right
        # with the left hand side
        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)
      # maybe this is a scope invocation?
      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)
      # maybe this is a scope invocation? hard to say
      super || ActiveRecordMetaclassGenerator.remove_by_from_method(method) || self.class.respond_to?(method)
    end

    # note that this build method does not apply to objsets
    # acquired using :through associations
    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
    
    # note that this method does not apply to objsets
    # acquired using :through associations
    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

      # calculations
      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|
          # has_many association.delete(ids)
          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)
          # does not support composite :through associations
          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|
      # delete the join objects originating from this, not invoking callbacks
      # create new ones
      # connect this with all the join objects, and each join object with a corresponding 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

#parent_classnameObject



18
19
20
21
22
23
24
# File 'lib/adsl/extract/rails/active_record_metaclass_generator.rb', line 18

def parent_classname
  if @ar_class.superclass == ActiveRecord::Base
    nil
  else
    ASTIdent.new :text => ActiveRecordMetaclassGenerator.adsl_ast_class_name(@ar_class.superclass)
  end
end

#reflection_to_adsl_ast(reflection) ⇒ Object



34
35
36
37
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
# File 'lib/adsl/extract/rails/active_record_metaclass_generator.rb', line 34

def reflection_to_adsl_ast(reflection)
  assoc_name = reflection.name.to_s
  target_class = reflection.class_name
  cardinality = reflection.collection? ? [0, 1.0/0.0] : [0, 1]
  inverse_of = case reflection.macro
  when :belongs_to; nil
  when :has_one, :has_many
    inverse_of_col_name = reflection.has_inverse? ? reflection.inverse_of : reflection.foreign_key
    candidates = target_class.constantize.reflections.values.select do |r|
      r.macro == :belongs_to && r.foreign_key.to_sym == inverse_of_col_name.to_sym
    end
    if candidates.empty?
      # just dont treat it as an inverse
      nil
    else
      raise "Multiple opposite relations found for #{reflection}: #{canditates}" if candidates.length > 1
      candidates.first.name.to_s
    end
  when :has_and_belongs_to_many
    join_table = reflection.options[:join_table]
    candidates = target_class.constantize.reflections.values.select do |r|
      r.macro == :has_and_belongs_to_many && r.options[:join_table] == join_table
    end
    raise "No inverse relations found for #{reflection}" if candidates.empty?
    raise "Multiple opposite relations found for #{reflection}: #{canditates}" if candidates.length > 1
    foreign_name = candidates.first.name.to_s
    assoc_name < foreign_name ? nil : foreign_name
  else
    raise "Unknown association macro `#{reflection.macro}' on #{reflection}"
  end

  ASTRelation.new(
    :cardinality => cardinality,
    :to_class_name => ASTIdent.new(:text => target_class.sub('::', '_')),
    :name => ASTIdent.new(:text => assoc_name.to_s),
    :inverse_of_name => (inverse_of.nil? ? nil : ASTIdent.new(:text => inverse_of.sub('::', '_')))
  )
end

#reflections(options = {}) ⇒ Object



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/adsl/extract/rails/active_record_metaclass_generator.rb', line 73

def reflections(options = {})
  # true => include only
  # false => exclude
  # nil => ignore filter
  options = {
    :this_class => true,
    :polymorphic => false,
    :through => nil,
  }.merge options

  refs = @ar_class.reflections.values.dup

  case options[:this_class]
  when true;  refs.select!{ |ref| ref.active_record == @ar_class }
  when false; refs.select!{ |ref| ref.active_record != @ar_class}
  end
  
  case options[:polymorphic]
  when true;  refs.select!{ |ref| ref.options[:as] or ref.options[:polymorphic] }
  when false; refs.select!{ |ref| !ref.options[:as] and !ref.options[:polymorphic] }
  end

  case options[:through]
  when true;  refs.select!{ |ref| ref.through_reflection }
  when false; refs.select!{ |ref| ref.through_reflection.nil? }
  end
  
  refs
end