@@ -8,6 +8,8 @@ module EagerLoad
88 # tree follows by recursion. AssociationInclusion stands for a single
99 # association; DiscriminatedInclusion stands for a name several subclasses
1010 # share.
11+ #
12+ # @api private
1113 class Inclusion
1214 # Add this inclusion's stages to the destination.
1315 #
@@ -23,6 +25,8 @@ def contribute(destination, chain)
2325 # An inclusion that stands for a single association. The LookupPipeline holds
2426 # the stage-building helpers the kinds lean on, and a node carries its own
2527 # children, so the pipeline is built by recursion from the roots downward.
28+ #
29+ # @api private
2630 class AssociationInclusion < Inclusion
2731 class << self
2832 # Builds the right kind of inclusion for the association. Each subclass
@@ -39,6 +43,8 @@ def for(association, pipeline, children)
3943
4044 # Whether this kind handles the given association.
4145 #
46+ # @param [ Mongoid::Association::Relatable ] association The inclusion.
47+ #
4248 # @return [ true | false ] Whether it handles it.
4349 def for? ( association )
4450 raise NotImplementedError
@@ -75,15 +81,28 @@ def initialize(association, pipeline, children)
7581 # <children>
7682 # ]
7783 # } }
84+ #
85+ # @api private
7886 class JoinedInclusion < AssociationInclusion
7987 class << self
8088 # The default kind: a referenced, non-polymorphic association, i.e. the
8189 # one no sibling kind claims.
90+ #
91+ # @param [ Mongoid::Association::Relatable ] association The inclusion.
92+ #
93+ # @return [ true | false ] Whether it handles it.
8294 def for? ( association )
8395 ( superclass . subclasses - [ self ] ) . none? { |kind | kind . for? ( association ) }
8496 end
8597 end
8698
99+ # Append the $lookup, with the children in its sub-pipeline, to the
100+ # destination; or distribute it onto the embedded path when nested in one.
101+ #
102+ # @param [ Array<Hash> ] destination The pipeline (or sub-pipeline) the
103+ # stages are appended to.
104+ # @param [ Array<Mongoid::Association::Relatable> ] chain The embedded path
105+ # accumulated from the ancestors above this inclusion (empty at the top).
87106 def contribute ( destination , chain )
88107 stage = @pipeline . lookup_stage_for ( @association )
89108 @children . each { |child | child . contribute ( stage [ '$lookup' ] [ 'pipeline' ] , [ ] ) }
@@ -103,13 +122,25 @@ def contribute(destination, chain)
103122 # For Computer.eager_load(port: :device) the :port inclusion emits nothing;
104123 # it hands the path [ :port ] to :device, which EmbeddedDistributor then
105124 # turns into stages.
125+ #
126+ # @api private
106127 class EmbeddedInclusion < AssociationInclusion
107128 class << self
129+ # @param [ Mongoid::Association::Relatable ] association The inclusion.
130+ #
131+ # @return [ true | false ] Whether the association is embedded.
108132 def for? ( association )
109133 association . embedded?
110134 end
111135 end
112136
137+ # Add no stage of its own; hand this document down the embedded path so the
138+ # children distribute onto it.
139+ #
140+ # @param [ Array<Hash> ] destination The pipeline (or sub-pipeline) the
141+ # stages are appended to.
142+ # @param [ Array<Mongoid::Association::Relatable> ] chain The embedded path
143+ # accumulated from the ancestors above this inclusion (empty at the top).
113144 def contribute ( destination , chain )
114145 @children . each { |child | child . contribute ( destination , chain + [ @association ] ) }
115146 end
@@ -118,13 +149,23 @@ def contribute(destination, chain)
118149 # A polymorphic inclusion: its target collection varies per document, so it
119150 # can't be a $lookup. It adds nothing here; PolymorphicPreloader resolves it
120151 # after the roots are materialized.
152+ #
153+ # @api private
121154 class DeferredInclusion < AssociationInclusion
122155 class << self
156+ # @param [ Mongoid::Association::Relatable ] association The inclusion.
157+ #
158+ # @return [ true | false ] Whether the association is polymorphic.
123159 def for? ( association )
124160 association . polymorphic?
125161 end
126162 end
127163
164+ # Add nothing; PolymorphicPreloader resolves the association after the
165+ # roots are materialized.
166+ #
167+ # @param [ Array<Hash> ] destination The pipeline (unused).
168+ # @param [ Array<Mongoid::Association::Relatable> ] chain The embedded path (unused).
128169 def contribute ( destination , chain ) ; end
129170 end
130171 end
0 commit comments