Module: Mongoid::Traversable
- Extended by:
- ActiveSupport::Concern
- Included in:
- Composable
- Defined in:
- build/mongoid-7.3/lib/mongoid/traversable.rb
Overview
Provides behavior around traversing the document graph.
Defined Under Namespace
Modules: ClassMethods, DiscriminatorAssignment, DiscriminatorRetrieval
Instance Method Summary collapse
-
#_children ⇒ Array<Document>
Get all child
Documents
to thisDocument
, going n levels deep if necessary. - #_parent ⇒ Object
- #_parent=(p) ⇒ Object
-
#_reset_memoized_children! ⇒ nil
Resets the memoized children on the object.
-
#_root ⇒ Document
Return the root document in the object graph.
-
#_root? ⇒ true, false
Is this document the root document of the hierarchy?.
-
#collect_children ⇒ Array<Document>
Collect all the children of this document.
-
#flag_children_persisted ⇒ Array<Document>
Marks all children as being persisted.
-
#hereditary? ⇒ true, false
Determines if the document is a subclass of another document.
-
#parentize(document) ⇒ Document
Sets up a child/parent association.
-
#remove_child(child) ⇒ Object
Remove a child document from this parent.
-
#reset_persisted_children ⇒ Array<Document>
After children are persisted we can call this to move all their changes and flag them as persisted in one call.
Instance Method Details
#_children ⇒ Array<Document>
Get all child Documents
to this Document
, going n levels deep if necessary. This is used when calling update persistence operations from the root document, where changes in the entire tree need to be determined. Note that persistence from the embedded documents will always be preferred, since they are optimized calls… This operation can get expensive in domains with large hierarchies.
132 133 134 |
# File 'build/mongoid-7.3/lib/mongoid/traversable.rb', line 132 def _children @__children ||= collect_children end |
#_parent ⇒ Object
14 15 16 |
# File 'build/mongoid-7.3/lib/mongoid/traversable.rb', line 14 def _parent @__parent ||= nil end |
#_parent=(p) ⇒ Object
18 19 20 |
# File 'build/mongoid-7.3/lib/mongoid/traversable.rb', line 18 def _parent=(p) @__parent = p end |
#_reset_memoized_children! ⇒ nil
Resets the memoized children on the object. Called internally when an embedded array changes size.
244 245 246 247 |
# File 'build/mongoid-7.3/lib/mongoid/traversable.rb', line 244 def _reset_memoized_children! _parent._reset_memoized_children! if _parent @__children = nil end |
#_root ⇒ Document
Return the root document in the object graph. If the current document is the root object in the graph it will return self.
256 257 258 259 260 |
# File 'build/mongoid-7.3/lib/mongoid/traversable.rb', line 256 def _root object = self while (object._parent) do object = object._parent; end object end |
#_root? ⇒ true, false
Is this document the root document of the hierarchy?
270 271 272 |
# File 'build/mongoid-7.3/lib/mongoid/traversable.rb', line 270 def _root? _parent ? false : true end |
#collect_children ⇒ Array<Document>
Collect all the children of this document.
144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'build/mongoid-7.3/lib/mongoid/traversable.rb', line 144 def collect_children children = [] .each_pair do |name, association| without_autobuild do child = send(name) Array.wrap(child).each do |doc| children.push(doc) children.concat(doc._children) end if child end end children end |
#flag_children_persisted ⇒ Array<Document>
Marks all children as being persisted.
166 167 168 169 170 |
# File 'build/mongoid-7.3/lib/mongoid/traversable.rb', line 166 def flag_children_persisted _children.each do |child| child.new_record = false end end |
#hereditary? ⇒ true, false
Determines if the document is a subclass of another document.
178 179 180 |
# File 'build/mongoid-7.3/lib/mongoid/traversable.rb', line 178 def hereditary? self.class.hereditary? end |
#parentize(document) ⇒ Document
Sets up a child/parent association. This is used for newly created objects so they can be properly added to the graph.
191 192 193 |
# File 'build/mongoid-7.3/lib/mongoid/traversable.rb', line 191 def parentize(document) self._parent = document end |
#remove_child(child) ⇒ Object
Remove a child document from this parent. If an embeds one then set to nil, otherwise remove from the embeds many.
This is called from the RemoveEmbedded
persistence command.
206 207 208 209 210 211 212 213 214 |
# File 'build/mongoid-7.3/lib/mongoid/traversable.rb', line 206 def remove_child(child) name = child.association_name if child. remove_ivar(name) else relation = send(name) relation.send(:delete_one, child) end end |
#reset_persisted_children ⇒ Array<Document>
After children are persisted we can call this to move all their changes and flag them as persisted in one call.
225 226 227 228 229 230 231 |
# File 'build/mongoid-7.3/lib/mongoid/traversable.rb', line 225 def reset_persisted_children _children.each do |child| child.move_changes child.new_record = false end _reset_memoized_children! end |