模块:Mongoid::Traversable

扩展方式:
ActiveSupport::Concern
包含在:
可组合
定义于:
lib/mongoid/traversable.rb

Overview

Mongoid::Document 中包含的 Mixin 模块提供了遍历文档图表的行为。

在命名空间下定义

模块: ClassMethodsDiscriminatorAssignmentDiscriminatorRetrieval

类方法摘要折叠

实例方法摘要折叠

类方法详细信息

__redefine(所有者,名称,值) 对象

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mongoid/traversable.rb', line 17

def __redefine(所有者, 名称, )
  if 所有者.singleton_class?
    所有者.redefine_method(名称) {  }
    所有者.发送(:public, 名称)
  end
  所有者.redefine_singleton_method(名称) {  }
  所有者.singleton_class.发送(:public, 名称)
  所有者.redefine_singleton_method(" #{ name } = ") do |new_value|
    if 所有者.等于?(self)
       = new_value
    else
      ::Mongoid::可遍历.重新定义(self, 名称, new_value)
    end
  end
  所有者.singleton_class.发送(:public, " #{ name } = ")
end

实例方法详细信息

# _children (reset: false) ⇒数组 < Document >

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

获取此 的所有子DocumentsDocument

返回:

  • ( Array< Document > )

    层次结构中的所有子文档。



238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/mongoid/traversable.rb', line 238

def _children(重置: false)
  # 请参阅上面对 `_parent` 方法的讨论,至于为什么变量
  # 此处需要有两个下划线。
  #
  # rubocop:disable Naming/MemoizedInstanceVariableName
  if 重置
    @__children = nil
  else
    @__children ||= collect_children
  end
  # rubocop:启用命名/MemoizedInstanceVariableName
end

# _descendants (reset: false) ⇒ Array< Document >

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

以递归方式获取此Document的所有后代Documents 。 从根文档调用更新持久性操作时使用,需要确定整个树中的更改。 请注意,嵌入式文档的持久性始终是首选,因为它们是优化的调用……在具有大型层次结构的域中,此操作的成本可能会很高。

返回:

  • ( Array< Document > )

    层次结构中的所有后代文档。



261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/mongoid/traversable.rb', line 261

def _descendants(重置: false)
  # 请参阅上面对 `_parent` 方法的讨论,至于为什么变量
  # 此处需要有两个下划线。
  #
  # rubocop:disable Naming/MemoizedInstanceVariableName
  if 重置
    @__descendants = nil
  else
    @__descendants ||= collect_descendants
  end
  # rubocop:启用命名/MemoizedInstanceVariableName
end

#_parentMongoid::Document | nil

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

检索此文档。

返回:



109
110
111
# File 'lib/mongoid/traversable.rb', line 109

def _parent
  @__parent || nil
end

# _parent= (文档) ⇒对象

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

设置此文档。

参数:



121
122
123
# File 'lib/mongoid/traversable.rb', line 121

def _parent=(文档)
  @__parent = 文档
end

# _reset_memoized_descendants!nil

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

重置对象上已记忆的后代。 当嵌入式大量更改大小时在内部调用。

返回:

  • ( nil )

    nil.



388
389
390
391
392
# File 'lib/mongoid/traversable.rb', line 388

def _reset_memoized_descendants!
  _parent&。_reset_memoized_descendants!
  _children 重置: true
  _descendants 重置: true
end

#_rootDocument

返回对象图表中的根文档。 如果当前文档是图表中的根对象,它将返回 self。

例子:

获取层次结构中的根文档。

document._root

返回:

  • (文档)

    层次结构中的根文档。



401
402
403
404
405
# File 'lib/mongoid/traversable.rb', line 401

def _root
  对象 = self
  对象 = 对象._parent while 对象._parent
  对象
end

# _root?true | false

此文档是层次结构的根文档吗?

例子:

文档是根文档吗?

document._root?

返回:

  • ( true | false )

    如果文档是根文档。



413
414
415
# File 'lib/mongoid/traversable.rb', line 413

def _root?
  _parent ? false : true
end

#collect_children =" Array< Document >"

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

收集此文档的所有子文档。

返回:

  • ( Array< Document > )

    孩子们。



279
280
281
282
283
284
285
286
287
288
# File 'lib/mongoid/traversable.rb', line 279

def collect_children
  [].点击 do |子项|
    embedded_relations.each_pair do |名称, _association|
      without_autobuild do
        子项 = 发送(名称)
        子项.concat(阵列.换行(子项)) if 子项
      end
    end
  end
end

#collect_descendants =" Array< Document >"

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

收集此文档的所有子文档。

返回:



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/mongoid/traversable.rb', line 295

def collect_descendants
  子项 = []
  to_expand = _children
  已扩展 = {}

  直到 to_expand.空?
    扩展 = to_expand
    to_expand = []
    扩展. do |子项|
      来年 if 已扩展[子项]

      # 如果_id为 nil,则不要标记为已展开,因为文档是按
      # 他们的 _id,多个具有 nil id 的嵌入式文档将进行比较
      # 同样,某些文档不会被扩展。
      已扩展[子项] = true if 子项._id
      子项 << 子项
      to_expand += 子项._children
    end
  end

  子项
end

# flag_descendants_persisted =" Array< Document >"

将所有后代标记为持久化。

返回:

  • ( Array< Document > )

    已标记的后代。



321
322
323
324
325
# File 'lib/mongoid/traversable.rb', line 321

def flag_descendants_persisted
  _descendants. do |子项|
    子项.new_record = false
  end
end

#会遗传吗?true | false

确定文档是否是另一个文档的子类。

例子:

检查文档是否为子类

Square.new.hereditary?

返回:

  • ( true | false )

    如果可遗传,则为 true,否则为 false。



333
334
335
# File 'lib/mongoid/traversable.rb', line 333

def 会遗传吗?
  self.class.会遗传吗?
end

#parentize (文档) ⇒文档

设置子/父关联。 这用于新创建的对象,以便它们可以正确添加到图表。

例子:

设置父文档。

document.parentize(parent)

参数:

  • 文档 (文档)

    父文档。

返回:



346
347
348
# File 'lib/mongoid/traversable.rb', line 346

def (文档)
  self._parent = 文档
end

# remove_child (child) ⇒对象

从此父文档中删除子文档。 如果是嵌入,则将其设立为零,否则从嵌入删除许多。

这是从RemoveEmbedded持久性命令中调用的。

例子:

删除子文档。

document.remove_child(child)

参数:

  • 子项 (文档)

    要删除的子(嵌入式)文档。



359
360
361
362
363
364
365
366
367
368
# File 'lib/mongoid/traversable.rb', line 359

def remove_child(子项)
  名称 = 子项.Association_name
  if 子项.embedded_one?
    属性.删除(子项._association.store_as)
    remove_ivar(名称)
  else
    关系 = 发送(名称)
    关系._remove(子项)
  end
end

# reset_persisted_descendants =" Array< Document >"

在后代持久化后,我们可以调用此方法来移动其所有更改,并在一次调用中将其标记为持久化。

返回:



374
375
376
377
378
379
380
# File 'lib/mongoid/traversable.rb', line 374

def reset_persisted_descendants
  _descendants. do |子项|
    子项.move_changes
    子项.new_record = false
  end
  _reset_memoized_descendants!
end