Module: Mongoid::Traversable
Relationships & Source Files | |
Namespace Children | |
Modules:
| |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
ActiveSupport::Concern
|
|
Defined in: | lib/mongoid/traversable.rb |
Overview
Mixin module included in Document
to provide behavior around traversing the document graph.
Instance Attribute Summary
-
#_parent ⇒ Mongoid::Document | nil
rw
Internal use only
Internal use only
Retrieves the parent document of this document.
-
#_parent=(document)
rw
Internal use only
Internal use only
Sets the parent document of this document.
-
#_root ⇒ Document
readonly
Return the root document in the object graph.
-
#_root? ⇒ true | false
readonly
Is this document the root document of the hierarchy?
-
#hereditary? ⇒ true | false
readonly
Determines if the document is a subclass of another document.
Instance Method Summary
-
#_children(reset: false) ⇒ Array<Document>
Internal use only
Internal use only
Get all child
Documents
to thisDocument
-
#_descendants(reset: false) ⇒ Array<Document>
Internal use only
Internal use only
Get all descendant
Documents
of thisDocument
recursively. -
#_reset_memoized_descendants! ⇒ nil
Internal use only
Internal use only
Resets the memoized descendants on the object.
-
#collect_children ⇒ Array<Document>
Internal use only
Internal use only
Collect all the children of this document.
-
#collect_descendants ⇒ Array<Document>
Internal use only
Internal use only
Collect all the descendants of this document.
-
#flag_descendants_persisted ⇒ Array<Document>
Marks all descendants as being persisted.
-
#parentize(document) ⇒ Document
Sets up a child/parent association.
-
#remove_child(child)
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.
DSL Calls
included
[ GitHub ]157 158 159 160 161 162 163 164 165 166 167 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 195 196
# File 'lib/mongoid/traversable.rb', line 157
included do class_attribute :discriminator_key, instance_accessor: false class << self delegate :discriminator_key, to: ::Mongoid prepend DiscriminatorAssignment include DiscriminatorRetrieval # @api private # # @return [ Hash<String, Class> ] The current mapping of discriminator_values to classes attr_accessor :discriminator_mapping end # Add a discriminator mapping to the parent class. This mapping is used when # receiving a document to identify its class. # # @param [ String ] value The discriminator_value that was just set # @param [ Class ] The class the discriminator_value was set on # # @api private def self.add_discriminator_mapping(value, klass = self) self.discriminator_mapping ||= {} self.discriminator_mapping[value] = klass superclass.add_discriminator_mapping(value, klass) if hereditary? end # Get the discriminator mapping from the parent class. This method returns nil if there # is no mapping for the given value. # # @param [ String ] value The discriminator_value to retrieve # # @return [ Class | nil ] klass The class corresponding to the given discriminator_value. If # the value is not in the mapping, this method returns nil. # # @api private def self.get_discriminator_mapping(value) self.discriminator_mapping[value] if self.discriminator_mapping end end
Instance Attribute Details
#_parent ⇒ Mongoid::Document | nil
(rw)
Retrieves the parent document of this document.
# File 'lib/mongoid/traversable.rb', line 74
def _parent @__parent || nil end
#_parent=(document) (rw)
Sets the parent document of this document.
# File 'lib/mongoid/traversable.rb', line 86
def _parent=(document) @__parent = document end
#_root ⇒ Document (readonly)
Return the root document in the object graph. If the current document is the root object in the graph it will return self.
#_root? ⇒ true
| false
(readonly)
Is this document the root document of the hierarchy?
# File 'lib/mongoid/traversable.rb', line 378
def _root? _parent ? false : true end
#hereditary? ⇒ true
| false
(readonly)
Determines if the document is a subclass of another document.
# File 'lib/mongoid/traversable.rb', line 298
def hereditary? self.class.hereditary? end
Instance Method Details
#_children(reset: false) ⇒ Array<Document>
Get all child Documents
to this Document
# File 'lib/mongoid/traversable.rb', line 203
def _children(reset: false) # See discussion above for the `_parent` method, as to why the variable # here needs to have two underscores. # # rubocop:disable Naming/MemoizedInstanceVariableName if reset @__children = nil else @__children ||= collect_children end # rubocop:enable Naming/MemoizedInstanceVariableName end
#_descendants(reset: false) ⇒ Array<Document>
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.
# File 'lib/mongoid/traversable.rb', line 226
def _descendants(reset: false) # See discussion above for the `_parent` method, as to why the variable # here needs to have two underscores. # # rubocop:disable Naming/MemoizedInstanceVariableName if reset @__descendants = nil else @__descendants ||= collect_descendants end # rubocop:enable Naming/MemoizedInstanceVariableName end
#_reset_memoized_descendants! ⇒ nil
Resets the memoized descendants on the object. Called internally when an embedded array changes size.
# File 'lib/mongoid/traversable.rb', line 353
def _reset_memoized_descendants! _parent&._reset_memoized_descendants! _children reset: true _descendants reset: true end
#collect_children ⇒ Array<Document>
Collect all the children of this document.
# File 'lib/mongoid/traversable.rb', line 244
def collect_children [].tap do |children| .each_pair do |name, _association| without_autobuild do child = send(name) children.concat(Array.wrap(child)) if child end end end end
#collect_descendants ⇒ Array<Document>
Collect all the descendants of this document.
# File 'lib/mongoid/traversable.rb', line 260
def collect_descendants children = [] = _children = {} 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.
# File 'lib/mongoid/traversable.rb', line 286
def flag_descendants_persisted _descendants.each do |child| child.new_record = false end 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.
# File 'lib/mongoid/traversable.rb', line 311
def parentize(document) self._parent = document end
#remove_child(child)
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.
# File 'lib/mongoid/traversable.rb', line 324
def remove_child(child) name = child.association_name if child. 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.
# File 'lib/mongoid/traversable.rb', line 339
def reset_persisted_descendants _descendants.each do |child| child.move_changes child.new_record = false end _reset_memoized_descendants! end