Module: Mongoid::Traversable
Overview
Provides behavior around traversing the document graph.
Defined Under Namespace
Modules: ClassMethods, DiscriminatorAssignment, DiscriminatorRetrieval
Instance Method Summary collapse
-
#_children ⇒ Array<Document>
private
Get all child
Documents
to thisDocument
. -
#_descendants ⇒ Array<Document>
private
Get all descendant
Documents
of thisDocument
recursively. - #_parent ⇒ Object
- #_parent=(p) ⇒ Object
-
#_reset_memoized_descendants! ⇒ nil
private
Resets the memoized descendants 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>
private
Collect all the children of this document.
-
#collect_descendants ⇒ Array<Document>
private
Collect all the descendants of this document.
-
#flag_descendants_persisted ⇒ Array<Document>
Marks all descendants 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_descendants ⇒ Array<Document>
After descendants 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>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get all child Documents
to this Document
127 128 129 |
# File 'lib/mongoid/traversable.rb', line 127 def _children @__children ||= collect_children end |
#_descendants ⇒ Array<Document>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get all descendant Documents
of this Document
recursively. 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.
141 142 143 |
# File 'lib/mongoid/traversable.rb', line 141 def _descendants @__descendants ||= collect_descendants end |
#_parent ⇒ Object
11 12 13 |
# File 'lib/mongoid/traversable.rb', line 11 def _parent @__parent ||= nil end |
#_parent=(p) ⇒ Object
15 16 17 |
# File 'lib/mongoid/traversable.rb', line 15 def _parent=(p) @__parent = p end |
#_reset_memoized_descendants! ⇒ nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Resets the memoized descendants on the object. Called internally when an embedded array changes size.
266 267 268 269 270 |
# File 'lib/mongoid/traversable.rb', line 266 def _reset_memoized_descendants! _parent._reset_memoized_descendants! if _parent @__children = nil @__descendants = 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.
279 280 281 282 283 |
# File 'lib/mongoid/traversable.rb', line 279 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?
291 292 293 |
# File 'lib/mongoid/traversable.rb', line 291 def _root? _parent ? false : true end |
#collect_children ⇒ Array<Document>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Collect all the children of this document.
150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/mongoid/traversable.rb', line 150 def collect_children children = [] .each_pair do |name, association| without_autobuild do child = send(name) if child children += Array.wrap(child) end end end children end |
#collect_descendants ⇒ Array<Document>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Collect all the descendants of this document.
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/mongoid/traversable.rb', line 168 def collect_descendants children = [] = [] = {} .each_pair do |name, association| without_autobuild do child = send(name) if child += Array.wrap(child) end end end until .empty? = = [] .each do |child| next if [child] # Don't mark expanded if _id is nil, since documents are compared by # their _ids, multiple embedded documents with nil ids will compare # equally, and some documents will not be expanded. [child] = true if child._id children << child += child._children end end children end |
#flag_descendants_persisted ⇒ Array<Document>
Marks all descendants as being persisted.
199 200 201 202 203 |
# File 'lib/mongoid/traversable.rb', line 199 def flag_descendants_persisted _descendants.each do |child| child.new_record = false end end |
#hereditary? ⇒ true | false
Determines if the document is a subclass of another document.
211 212 213 |
# File 'lib/mongoid/traversable.rb', line 211 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.
224 225 226 |
# File 'lib/mongoid/traversable.rb', line 224 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.
237 238 239 240 241 242 243 244 245 246 |
# File 'lib/mongoid/traversable.rb', line 237 def remove_child(child) name = child.association_name if child. self.attributes.delete(child._association.store_as) remove_ivar(name) else relation = send(name) relation._remove(child) end end |
#reset_persisted_descendants ⇒ Array<Document>
After descendants are persisted we can call this to move all their changes and flag them as persisted in one call.
252 253 254 255 256 257 258 |
# File 'lib/mongoid/traversable.rb', line 252 def reset_persisted_descendants _descendants.each do |child| child.move_changes child.new_record = false end _reset_memoized_descendants! end |