Class: DataMapper::Associations::HasNAssociation

Inherits:
Object
  • Object
show all
Defined in:
lib/data_mapper/associations/has_n_association.rb

Direct Known Subclasses

BelongsToAssociation, HasManyAssociation

Constant Summary collapse

OPTIONS =
[
  :class,
  :class_name,
  :foreign_key
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, association_name, options) ⇒ HasNAssociation

Returns a new instance of HasNAssociation.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/data_mapper/associations/has_n_association.rb', line 17

def initialize(klass, association_name, options)
  @constant = klass
  @adapter = database.adapter
  @table = @adapter.table(klass)
  @association_name = association_name.to_sym
  @options = options || Hash.new
  
  define_accessor(klass)
  
  Persistence::dependencies.add(associated_constant_name) do |klass|
    @foreign_key_column = associated_table[foreign_key_name]
    
    unless @foreign_key_column
      associated_constant.property(foreign_key_name, foreign_key_type)
    
      @foreign_key_column = associated_table[foreign_key_name]
    
      if @foreign_key_column.nil?
        raise ForeignKeyNotFoundError.new(<<-EOS.compress_lines)
          key_table => #{key_table.inspect},
          association_table => #{associated_table.inspect},
          association_name => #{name},
          foreign_key_name => #{foreign_key_name.inspect},
          foreign_key_type => #{foreign_key_type.inspect},
          constant => #{constant.inspect},
          associated_constant => #{associated_constant.inspect}
        EOS
      end
    end
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/data_mapper/associations/has_n_association.rb', line 9

def options
  @options
end

Instance Method Details

#activate!Object



117
118
119
# File 'lib/data_mapper/associations/has_n_association.rb', line 117

def activate!
  foreign_key_column
end

#associated_columnsObject



105
106
107
# File 'lib/data_mapper/associations/has_n_association.rb', line 105

def associated_columns
  associated_table.columns.reject { |column| column.lazy? }
end

#associated_constantObject



57
58
59
# File 'lib/data_mapper/associations/has_n_association.rb', line 57

def associated_constant
  @associated_constant || @associated_constant = Kernel.const_get(associated_constant_name)
end

#associated_constant_nameObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/data_mapper/associations/has_n_association.rb', line 61

def associated_constant_name
  @associated_constant_name || begin
    
    if @options.has_key?(:class) || @options.has_key?(:class_name)
      @associated_constant_name = (@options[:class] || @options[:class_name])
      
      if @associated_constant_name.kind_of?(String)
        @associated_constant_name = Inflector.classify(@associated_constant_name)
      elsif @associated_constant_name.kind_of?(Class)
        @associated_constant_name = @associated_constant_name.name
      end  
    else
      @associated_constant_name = Inflector.classify(@association_name)
    end
    
    @associated_constant_name
  end
  
end

#associated_tableObject



101
102
103
# File 'lib/data_mapper/associations/has_n_association.rb', line 101

def associated_table
  @association_table || @association_table = @adapter.table(associated_constant)
end

#constantObject



53
54
55
# File 'lib/data_mapper/associations/has_n_association.rb', line 53

def constant
  @constant
end

#finder_optionsObject



109
110
111
# File 'lib/data_mapper/associations/has_n_association.rb', line 109

def finder_options
  @finder_options || @finder_options = @options.reject { |k,v| self.class::OPTIONS.include?(k) }
end

#foreign_key_columnObject



85
86
87
# File 'lib/data_mapper/associations/has_n_association.rb', line 85

def foreign_key_column
  @foreign_key_column
end

#foreign_key_nameObject



89
90
91
# File 'lib/data_mapper/associations/has_n_association.rb', line 89

def foreign_key_name
  @foreign_key_name || @foreign_key_name = (@options[:foreign_key] || key_table.default_foreign_key)
end

#foreign_key_typeObject



93
94
95
# File 'lib/data_mapper/associations/has_n_association.rb', line 93

def foreign_key_type
  @foreign_key_type || @foreign_key_type = key_table.key.type
end

#key_tableObject



97
98
99
# File 'lib/data_mapper/associations/has_n_association.rb', line 97

def key_table
  @key_table || @key_table = @adapter.table(constant)
end

#nameObject



49
50
51
# File 'lib/data_mapper/associations/has_n_association.rb', line 49

def name
  @association_name
end

#primary_key_columnObject



81
82
83
# File 'lib/data_mapper/associations/has_n_association.rb', line 81

def primary_key_column
  @primary_key_column || @primary_key_column = key_table.key
end

#to_sqlObject



113
114
115
# File 'lib/data_mapper/associations/has_n_association.rb', line 113

def to_sql
  "JOIN #{associated_table.to_sql} ON #{foreign_key_column.to_sql(true)} = #{primary_key_column.to_sql(true)}"
end