Class: Stratagem::Instrumentation::Models::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/stratagem/instrumentation/models/association.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from_class, name, foreign_key, klass, macro, options) ⇒ Association

Returns a new instance of Association.



5
6
7
8
9
10
11
12
# File 'lib/stratagem/instrumentation/models/association.rb', line 5

def initialize(from_class, name, foreign_key, klass, macro, options)
  @from_class = from_class
  @name = name
  @foreign_key = foreign_key
  @klass = klass
  @macro = macro
  @options = options
end

Instance Attribute Details

#foreign_keyObject (readonly)

Returns the value of attribute foreign_key.



3
4
5
# File 'lib/stratagem/instrumentation/models/association.rb', line 3

def foreign_key
  @foreign_key
end

#from_classObject (readonly)

Returns the value of attribute from_class.



3
4
5
# File 'lib/stratagem/instrumentation/models/association.rb', line 3

def from_class
  @from_class
end

#macroObject (readonly)

Returns the value of attribute macro.



3
4
5
# File 'lib/stratagem/instrumentation/models/association.rb', line 3

def macro
  @macro
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/stratagem/instrumentation/models/association.rb', line 3

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/stratagem/instrumentation/models/association.rb', line 3

def options
  @options
end

Instance Method Details

#klassObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/stratagem/instrumentation/models/association.rb', line 14

def klass
  if (@klass.kind_of?(String))
    if (options[:polymorphic])
      @poly_count ||= 0
      
      # all possibilities
      possible_classes = ActiveRecord::Base.sg_subclasses.sort {|a,b| a.name <=> b.name }.select do |sc|
        sc.stratagem.relations(:has_many).find {|r| r.klass == from_class } != nil
      end

      if (possible_classes.size > 0)
        @poly_count += 1
        @poly_count = 1 if @poly_count > possible_classes.size
        return possible_classes[@poly_count-1]
      else
        return nil
      end
    else
      # unknown why the class could not be loaded
      return nil
    end
  else
    return @klass
  end
end