Module: Mongoid::Traversable

Extended by:
ActiveSupport::Concern
Included in:
Composable
Defined in:
build/mongoid-master/lib/mongoid/traversable.rb

Overview

Provides behavior around traversing the document graph.

Defined Under Namespace

Modules: ClassMethods, DiscriminatorAssignment, DiscriminatorRetrieval

Instance Method Summary collapse

Instance Method Details

#_childrenArray<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

Returns:

  • (Array<Document>)

    All child documents in the hierarchy.



123
124
125
# File 'build/mongoid-master/lib/mongoid/traversable.rb', line 123

def _children
  @__children ||= collect_children
end

#_descendantsArray<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.

Returns:

  • (Array<Document>)

    All descendant documents in the hierarchy.



137
138
139
# File 'build/mongoid-master/lib/mongoid/traversable.rb', line 137

def _descendants
  @__descendants ||= collect_descendants
end

#_parentObject



11
12
13
# File 'build/mongoid-master/lib/mongoid/traversable.rb', line 11

def _parent
  @__parent ||= nil
end

#_parent=(p) ⇒ Object



15
16
17
# File 'build/mongoid-master/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.

Returns:

  • (nil)

    nil.



262
263
264
265
266
# File 'build/mongoid-master/lib/mongoid/traversable.rb', line 262

def _reset_memoized_descendants!
  _parent._reset_memoized_descendants! if _parent
  @__children = nil
  @__descendants = nil
end

#_rootDocument

Return the root document in the object graph. If the current document is the root object in the graph it will return self.

Examples:

Get the root document in the hierarchy.

document._root

Returns:

  • (Document)

    The root document in the hierarchy.



275
276
277
278
279
# File 'build/mongoid-master/lib/mongoid/traversable.rb', line 275

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?

Examples:

Is the document the root?

document._root?

Returns:

  • (true | false)

    If the document is the root.



287
288
289
# File 'build/mongoid-master/lib/mongoid/traversable.rb', line 287

def _root?
  _parent ? false : true
end

#collect_childrenArray<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.

Returns:



146
147
148
149
150
151
152
153
154
155
156
157
# File 'build/mongoid-master/lib/mongoid/traversable.rb', line 146

def collect_children
  children = []
  embedded_relations.each_pair do |name, association|
    without_autobuild do
      child = send(name)
      if child
        children += Array.wrap(child)
      end
    end
  end
  children
end

#collect_descendantsArray<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.

Returns:

  • (Array<Document>)

    The descendants.



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
# File 'build/mongoid-master/lib/mongoid/traversable.rb', line 164

def collect_descendants
  children = []
  to_expand = []
  expanded = {}
  embedded_relations.each_pair do |name, association|
    without_autobuild do
      child = send(name)
      if child
        to_expand += Array.wrap(child)
      end
    end
  end
  until to_expand.empty?
    expanding = to_expand
    to_expand = []
    expanding.each do |child|
      next if expanded[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.
      expanded[child] = true if child._id
      children << child
      to_expand += child._children
    end
  end
  children
end

#flag_descendants_persistedArray<Document>

Marks all descendants as being persisted.

Returns:

  • (Array<Document>)

    The flagged descendants.



195
196
197
198
199
# File 'build/mongoid-master/lib/mongoid/traversable.rb', line 195

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.

Examples:

Check if the document is a subclass

Square.new.hereditary?

Returns:

  • (true | false)

    True if hereditary, false if not.



207
208
209
# File 'build/mongoid-master/lib/mongoid/traversable.rb', line 207

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.

Examples:

Set the parent document.

document.parentize(parent)

Parameters:

  • document (Document)

    The parent document.

Returns:



220
221
222
# File 'build/mongoid-master/lib/mongoid/traversable.rb', line 220

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.

Examples:

Remove the child.

document.remove_child(child)

Parameters:

  • child (Document)

    The child (embedded) document to remove.



233
234
235
236
237
238
239
240
241
242
# File 'build/mongoid-master/lib/mongoid/traversable.rb', line 233

def remove_child(child)
  name = child.association_name
  if child.embedded_one?
    self.attributes.delete(child._association.store_as)
    remove_ivar(name)
  else
    relation = send(name)
    relation.send(:delete_one, child)
  end
end

#reset_persisted_descendantsArray<Document>

After descendants are persisted we can call this to move all their changes and flag them as persisted in one call.

Returns:

  • (Array<Document>)

    The descendants.



248
249
250
251
252
253
254
# File 'build/mongoid-master/lib/mongoid/traversable.rb', line 248

def reset_persisted_descendants
  _descendants.each do |child|
    child.move_changes
    child.new_record = false
  end
  _reset_memoized_descendants!
end