Module: Mongoid::Traversable

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

Overview

Provides behavior around traversing the document graph.

Since:

  • 4.0.0

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

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

Examples:

Get all the document’s children.

person._children

Returns:

  • (Array<Document>)

    All child documents in the hierarchy.

Since:

  • 4.0.0



31
32
33
# File 'build/mongoid-7.1/lib/mongoid/traversable.rb', line 31

def _children
  @__children ||= collect_children
end

#_parentObject

Since:

  • 4.0.0



12
13
14
# File 'build/mongoid-7.1/lib/mongoid/traversable.rb', line 12

def _parent
  @__parent ||= nil
end

#_parent=(p) ⇒ Object

Since:

  • 4.0.0



16
17
18
# File 'build/mongoid-7.1/lib/mongoid/traversable.rb', line 16

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.

Examples:

Reset the memoized children.

document._reset_memoized_children!

Returns:

  • (nil)

    nil.

Since:

  • 5.0.0



143
144
145
146
# File 'build/mongoid-7.1/lib/mongoid/traversable.rb', line 143

def _reset_memoized_children!
  _parent._reset_memoized_children! if _parent
  @__children = 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.

Since:

  • 4.0.0



155
156
157
158
159
# File 'build/mongoid-7.1/lib/mongoid/traversable.rb', line 155

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.

Since:

  • 3.1.0



169
170
171
# File 'build/mongoid-7.1/lib/mongoid/traversable.rb', line 169

def _root?
  _parent ? false : true
end

#collect_childrenArray<Document>

Collect all the children of this document.

Examples:

Collect all the children.

document.collect_children

Returns:

Since:

  • 2.4.0



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'build/mongoid-7.1/lib/mongoid/traversable.rb', line 43

def collect_children
  children = []
  embedded_relations.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_persistedArray<Document>

Marks all children as being persisted.

Examples:

Flag all the children.

document.flag_children_persisted

Returns:

  • (Array<Document>)

    The flagged children.

Since:

  • 3.0.7



65
66
67
68
69
# File 'build/mongoid-7.1/lib/mongoid/traversable.rb', line 65

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.

Examples:

Check if the document is a subclass

Square.new.hereditary?

Returns:

  • (true, false)

    True if hereditary, false if not.

Since:

  • 4.0.0



77
78
79
# File 'build/mongoid-7.1/lib/mongoid/traversable.rb', line 77

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:

Since:

  • 4.0.0



90
91
92
# File 'build/mongoid-7.1/lib/mongoid/traversable.rb', line 90

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.

Since:

  • 2.0.0.beta.1



105
106
107
108
109
110
111
112
113
# File 'build/mongoid-7.1/lib/mongoid/traversable.rb', line 105

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

#reset_persisted_childrenArray<Document>

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

Examples:

Reset the children.

document.reset_persisted_children

Returns:

Since:

  • 2.1.0



124
125
126
127
128
129
130
# File 'build/mongoid-7.1/lib/mongoid/traversable.rb', line 124

def reset_persisted_children
  _children.each do |child|
    child.move_changes
    child.new_record = false
  end
  _reset_memoized_children!
end