Class: Mongoid::Association::Eager
- Inherits:
-
Object
- Object
- Mongoid::Association::Eager
- Defined in:
- lib/mongoid/association/eager.rb
Overview
Base class for eager load preload functions.
Direct Known Subclasses
Referenced::BelongsTo::Eager, Referenced::HasAndBelongsToMany::Eager, Referenced::HasMany::Eager, Referenced::HasManyThrough::Eager, Referenced::HasOne::Eager, Referenced::HasOneThrough::Eager
Class Method Summary collapse
-
.run(associations, docs, use_lookup = false, pipeline = []) ⇒ Array
Build a preloader for the given arguments and run it.
Instance Method Summary collapse
-
#initialize(associations, docs, use_lookup = false, pipeline = []) ⇒ Base
constructor
Instantiate the eager load class.
-
#run ⇒ Array
Run the preloader.
Constructor Details
#initialize(associations, docs, use_lookup = false, pipeline = []) ⇒ Base
Instantiate the eager load class.
30 31 32 33 34 35 36 |
# File 'lib/mongoid/association/eager.rb', line 30 def initialize(associations, docs, use_lookup = false, pipeline = []) @associations = associations @docs = docs @grouped_docs = {} @use_lookup = use_lookup @pipeline = pipeline end |
Class Method Details
.run(associations, docs, use_lookup = false, pipeline = []) ⇒ Array
Build a preloader for the given arguments and run it.
12 13 14 |
# File 'lib/mongoid/association/eager.rb', line 12 def self.run(associations, docs, use_lookup = false, pipeline = []) new(associations, docs, use_lookup, pipeline).run end |
Instance Method Details
#run ⇒ Array
Run the preloader.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/mongoid/association/eager.rb', line 44 def run @loaded = [] if @use_lookup preload_with_lookup @loaded = @docs return @loaded.flatten end while shift_association preload @loaded << @docs.collect { |d| d.send(@association.name) if d.respond_to?(@association.name) } end @loaded.flatten end |