Class: DataMapper::Associations::HasManyAssociation

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/data_mapper/associations/has_many_association.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance, association_name, options) ⇒ HasManyAssociation

Returns a new instance of HasManyAssociation.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/data_mapper/associations/has_many_association.rb', line 7

def initialize(instance, association_name, options)
  @instance = instance
  @association_name = association_name.to_sym
  @options = options
  
  @associated_class = if options.has_key?(:class) || options.has_key?(:class_name)
    associated_class_name = (options[:class] || options[:class_name])
    if associated_class_name.kind_of?(String)
      Kernel.const_get(Inflector.classify(associated_class_name))
    else
      associated_class_name
    end
  else            
    Kernel.const_get(Inflector.classify(association_name))
  end
end

Class Method Details

.setup(klass, association_name, options) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/data_mapper/associations/has_many_association.rb', line 24

def self.setup(klass, association_name, options)

  # Define the association instance method (i.e. Project#tasks)
  klass.class_eval <<-EOS
    def #{association_name}
      @#{association_name} || (@#{association_name} = HasManyAssociation.new(self, "#{association_name}", #{options.inspect}))
    end
  EOS
  
end

Instance Method Details

#[](key) ⇒ Object



44
45
46
# File 'lib/data_mapper/associations/has_many_association.rb', line 44

def [](key)
  entries[key]
end

#eachObject



35
36
37
# File 'lib/data_mapper/associations/has_many_association.rb', line 35

def each
  find.each { |item| yield item }
end

#empty?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/data_mapper/associations/has_many_association.rb', line 48

def empty?
  entries.empty?
end

#findObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/data_mapper/associations/has_many_association.rb', line 52

def find
  return @results unless @results.nil?
  
  unless @instance.loaded_set.nil?
    
    # Temp variable for the instance variable name.
    instance_variable_name = "@#{foreign_key}".to_sym
    
    set = @instance.loaded_set.group_by { |instance| instance.key }
    
    # Fetch the foreign objects for all instances in the current object's loaded-set.
    @instance.session.all(@associated_class, foreign_key.to_sym => set.keys).group_by do |association|
      association.instance_variable_get(instance_variable_name)
    end.each_pair do |id, results|
      set[id].first.send(@association_name).set(results)
    end
    
  end
  
  return @results ||= []
end

#foreign_keyObject



82
83
84
# File 'lib/data_mapper/associations/has_many_association.rb', line 82

def foreign_key
  @foreign_key || (@foreign_key = (@options[:foreign_key] || @instance.session.schema[@instance.class].default_foreign_key))
end

#inspectObject



78
79
80
# File 'lib/data_mapper/associations/has_many_association.rb', line 78

def inspect
  @results.inspect
end

#set(results) ⇒ Object



74
75
76
# File 'lib/data_mapper/associations/has_many_association.rb', line 74

def set(results)
  @results = results
end

#sizeObject Also known as: length



39
40
41
# File 'lib/data_mapper/associations/has_many_association.rb', line 39

def size
  entries.size
end