Class: Mongoid::Relations::Eager::Base
- Inherits:
-
Object
- Object
- Mongoid::Relations::Eager::Base
- Defined in:
- lib/mongoid/relations/eager/base.rb
Overview
Base class for eager load preload functions.
Direct Known Subclasses
Instance Method Summary collapse
-
#each_loaded_document ⇒ Object
Run the preloader.
-
#group_by_key ⇒ Symbol
Return the key to group the current documents.
-
#grouped_docs ⇒ Hash
Return a hash with the current documents grouped by key.
-
#initialize(associations, docs) ⇒ Base
constructor
Instantiate the eager load class.
-
#keys_from_docs ⇒ Array
Group the documents and return the keys.
-
#preload ⇒ Object
Preload the current relation.
-
#run ⇒ Array
Run the preloader.
-
#set_on_parent(id, element) ⇒ Object
Set the pre-loaded document into its parent.
-
#shift_metadata ⇒ Object
Shift the current relation metadata.
Constructor Details
#initialize(associations, docs) ⇒ Base
Instantiate the eager load class.
21 22 23 24 25 |
# File 'lib/mongoid/relations/eager/base.rb', line 21 def initialize(associations, docs) @associations = associations @docs = docs @grouped_docs = {} end |
Instance Method Details
#each_loaded_document ⇒ Object
Run the preloader.
72 73 74 75 76 |
# File 'lib/mongoid/relations/eager/base.rb', line 72 def each_loaded_document @metadata.klass.any_in(key => keys_from_docs).each do |doc| yield doc end end |
#group_by_key ⇒ Symbol
Return the key to group the current documents.
This method should be implemented in the subclass
129 130 131 |
# File 'lib/mongoid/relations/eager/base.rb', line 129 def group_by_key raise NotImplementedError end |
#grouped_docs ⇒ Hash
Return a hash with the current documents grouped by key.
101 102 103 104 105 |
# File 'lib/mongoid/relations/eager/base.rb', line 101 def grouped_docs @grouped_docs[@metadata.name] ||= @docs.group_by do |doc| doc.send(group_by_key) end end |
#keys_from_docs ⇒ Array
Group the documents and return the keys
115 116 117 |
# File 'lib/mongoid/relations/eager/base.rb', line 115 def keys_from_docs grouped_docs.keys end |
#preload ⇒ Object
Preload the current relation.
This method should be implemented in the subclass
62 63 64 |
# File 'lib/mongoid/relations/eager/base.rb', line 62 def preload raise NotImplementedError end |
#run ⇒ Array
Run the preloader.
47 48 49 50 51 52 |
# File 'lib/mongoid/relations/eager/base.rb', line 47 def run while preload end @docs end |
#set_on_parent(id, element) ⇒ Object
Set the pre-loaded document into its parent.
87 88 89 90 91 |
# File 'lib/mongoid/relations/eager/base.rb', line 87 def set_on_parent(id, element) grouped_docs[id].each do |d| set_relation(d, element) end end |
#shift_metadata ⇒ Object
Shift the current relation metadata
35 36 37 |
# File 'lib/mongoid/relations/eager/base.rb', line 35 def @metadata = @associations.shift end |