类:Mongoid::Association::Referenced::HasMany::Enumerable

继承:
对象
  • 对象
显示全部
扩展方式:
可转发
包括:
可枚举
定义于:
lib/mongoid/association/referenced/has_many/enumerable.rb

Overview

此类是所有引用关联的包装器,这些关联的目标可以是条件或 _loaded 文档大量。 这可以处理这两种情况或两种情况的组合。

实例属性摘要折叠

实例方法摘要折叠

构造函数详情

#initialize(target, base = nil, association = nil) ⇒ Enumerable

使用条件或数组初始化新的可枚举项。

例子:

使用条件初始化可枚举项。

Enumberable.new(Post.where(:person_id => id))

使用大量初始化可枚举项。

Enumerable.new([ post ])

参数:



262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 262

def 初始化(目标, 基础 = nil, 关联 = nil)
  @_base = 基础
  @_association = 关联
  if 目标.is_a?(条件)
    @_used, @exected, @_loaded, @_unloaded = {}, false, {}, 目标
  else
    @_used, @exected = {}, true
    @_loaded = 目标.注入({}) do |_target, doc|
      _target[doc._id] = doc if doc
      _target
    end
  end
end

实例属性详细信息

# _used对象

三个主要实例变量是文档集合。



21
22
23
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 21

def _used
  @_used
end

# _used 已附加的文档。 (Documentsthathavebeenappended.) ⇒对象

三个主要实例变量是文档集合。



21
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 21

attr_accessor :_used, :_loaded, :_unloaded

# _loaded ">对象

三个主要实例变量是文档集合。



21
22
23
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 21

def _loaded
  @_loaded
end

# _loaded 已加载的持久化文档。 (Persisteddocumentsthathavebeen_loaded.) ⇒对象

三个主要实例变量是文档集合。



21
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 21

attr_accessor :_used, :_loaded, :_unloaded

# _unloaded ">对象

三个主要实例变量是文档集合。



21
22
23
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 21

def _unloaded
  @_unloaded
end

#_unloaded 表示持久化Docs的条件。 (Acriteriarepresentingpersisteddocs。) ⇒对象

三个主要实例变量是文档集合。



21
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 21

attr_accessor :_used, :_loaded, :_unloaded

实例方法详细信息

# << (文档) ⇒ Document也称为: push

将文档附加到可枚举值。

例子:

附加文档。

enumerable << document

参数:

  • 文档 (文档)

    要附加的文档。

返回:



60
61
62
63
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 60

def <<(文档)
  _used[文档._id] = 文档
  self
end

# == (other) ⇒ true | false

检查该可枚举对象是否等于其他对象。

例子:

检查相等性。

enumerable == []

参数:

返回:

  • ( true | false )

    如果对象相等。



33
34
35
36
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 33

def ==(其他)
  return false 除非 其他.respond_to?(:entries)
  条目 == 其他.条目
end

# === (other) ⇒ true | false

检查可枚举项与为 case 语句提供的对象的相等性。

例子:

检查大小写相等性。

enumerable === Array

参数:

  • 其他 ( Object )

    要检查的对象。

返回:

  • ( true | false )

    如果案例中的对象相等。



47
48
49
50
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 47

def ===(其他)
  return false 除非 其他.respond_to?(:entries)
  条目 === 其他.条目
end

# _loaded?true | false

可枚举项是否已 _loaded? 如果条件已执行或我们手动加载整个内容,则为 true。

例子:

是否为可枚举 _loaded?

enumerable._loaded?

返回:

  • ( true | false )

    如果该可枚举已被 _loaded。



353
354
355
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 353

def _loaded?
  !!@exected
end

#任何? (*args) ⇒ true | false

返回关联是否包含任何文档,可以选择是否受所提供的筛选器约束。

如果关联具有任何持久化文档并且具有任何尚未持久化文档,则此方法返回 true。

如果关联已加载,此方法将检查加载的文档,而不查询数据库。 如果未加载关联,则无参数和无块版本不会加载关联;其他版本(委托给 Enumerable)可能会也可能不会完全加载关联,具体取决于它是否迭代完成。

此方法可以采用一个参数和一个区块。 参数或区块的行为将委托给标准库 Enumerable 模块。

请注意,当 Enumerable 的 any?方法同时使用区块和模式进行调用,则该方法仅使用模式。

参数:

  • *args ( Object... )

    文档必须满足的条件。 有关详细信息,请参阅可枚举文档。

返回:

  • ( true | false )

    如果关联有任何文件。



225
226
227
228
229
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 225

def 任何?(*args)
  return  if args.任何? || block_given?

  !空?
end

# as_json (options = {}) ⇒哈希

将 #as_json 发送到条目,而不进行编码。

例子:

获取JSON格式的可枚举值。

enumerable.as_json

参数:

  • 选项 哈希 (默认为: {}

    可选参数。

返回:

  • (哈希)

    条目全部作为哈希加载。



469
470
471
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 469

def as_json(选项 = {})
  条目.as_json(选项)
end

# clearArray< Document >

清除此可枚举值中的所有文档。 如果传递了一个区块,它将让出内存中的每个文档。

例子:

清除可枚举项。

enumerable.clear

使用区块清除可枚举项。

enumerable.clear do |doc|
  doc.unbind
end

返回:

  • ( Array< Document > )

    已清除的 _used Docs 。



79
80
81
82
83
84
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 79

def 清除
  if block_given?
    in_memory { |doc| 产量(doc) }
  end
  _loaded.清除  _used.清除
end

# cloneArray< Document >

注意:

这会将所有文档加载到内存中。

克隆可枚举值中的每个文档。

例子:

克隆可枚举值。

enumerable.clone

返回:

  • ( Array< Document > )

    可枚举值的数组克隆。



94
95
96
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 94

def 克隆
  收集 { |doc| doc.克隆 }
end

#delete(document) {|doc| ... } ⇒ Document

从可枚举对象中删除提供的文档。

例子:

删除文档。

enumerable.delete(document)

参数:

  • 文档 (文档)

    要删除的文档。

产量:

  • ( doc )

返回:

  • (文档)

    已删除的文档。



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 106

def 删除(文档)
  doc = (_loaded.删除(文档._id) || _used.删除(文档._id))
  除非 doc
    if _unloaded && _unloaded.WHERE(_id: 文档._id).存在吗?
      产量(文档) if block_given?
      return 文档
    end
  end
  产量(doc) if block_given?
  doc
end

# delete_if (&block) ⇒ Array< Document >

注意:

此操作会从数据库加载所有文档。

删除可枚举 for区块中返回 true 的每个文档。

例子:

删除所有匹配的文档。

enumerable.delete_if do |doc|
  dod._id == _id
end

返回:

  • ( Array< Document > )

    剩余的Docs 。



129
130
131
132
133
134
135
136
137
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 129

def delete_if()
  load_all!
  Deleted = in_memory.SELECT()
  Deleted. do |doc|
    _loaded.删除(doc._id)
    _used.删除(doc._id)
  end
  self
end

#eachtrue

迭代此可枚举值必须处理几种不同的情况。

如果该可枚举项的条件是 _loaded 到内存中,那么它就会让出所有 _loaded Docs和所有 _used Docs 。

如果该可枚举对象尚未加载条件,则会在加载文档时遍历游标,然后遍历已添加的Docs 。

如果没有传递任何区块,则会返回一个包含所有Docs的枚举器。

例子:

对可枚举值进行迭代。

enumerable.each do |doc|
  puts doc
end

返回一个包含所有Docs的枚举器


a = enumerable.each

返回:

  • ( true )

    该可枚举项现已 _loaded。



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 162

def 
  除非 block_given?
    return to_enum
  end
  if _loaded?
    _loaded.each_pair do |id, doc|
      文档 = _used.删除(doc._id) || doc
      set_base(文档)
      产量(文档)
    end
  else
    unloaded_documents. do |doc|
      文档 = _used.删除(doc._id) || _loaded.删除(doc._id) || doc
      _loaded[文档._id] = 文档
      set_base(文档)
      产量(文档)
    end
  end
  _used.each_pair do |id, doc|
    产量(doc)
  end
  @exected = true
end

#为空?true | false

可枚举项是否为空? 将根据是否为 _loaded 来确定计数是否为零。

例子:

可枚举项是否为空?

enumerable.empty?

返回:

  • ( true | false )

    如果可枚举为空。



193
194
195
196
197
198
199
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 193

def 空?
  if _loaded?
    in_memory.空?
  else
    _used.空? && !_unloaded.存在吗?
  end
end

#first(limit = nil) ⇒ Document

注意:

在没有对条件定义其他排序的情况下自动对_id添加排序,可能会导致性能问题。 如果在使用 #first 或 #last 时遇到意外的性能下降,请改用 #take。 请注意,#take 不保证顺序。

获取可枚举值中的第一个文档。 将首先检查持久保存的文档。 不加载整个可枚举值。

例子:

获取第一个文档。

enumerable.first

参数:

  • limit 整数 (默认为: nil

    要返回的文档数量。

返回:

  • (文档)

    找到的第一个文档。



246
247
248
249
250
251
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 246

def first(limit = nil)
  _loaded.try(:values).try(:first) ||
      _used[(ul = _unloaded.try(:first, limit)).try(:_id)] ||
      ul ||
      _used.values.try(:first)
end

# in_memory数组< Document >

注意:

当传递一个区块时,它会生成每个文档。

返回该可枚举值中已 _loaded 或 _Added 的所有文档。

例子:

获取内存中的Docs 。

enumerable.in_memory

返回:

  • ( Array< Document > )

    内存中的Docs 。



309
310
311
312
313
314
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 309

def in_memory
  docs = (_loaded.values + _used.values)
  docs. do |doc|
    产量(doc) if block_given?
  end
end

# include? (doc) ⇒ true | false

目标是否包括所提供的文档?

例子:

目标是否包括文档?

enumerable.include?(document)

参数:

  • doc (文档)

    要检查的文档。

返回:

  • ( true | false )

    如果文档位于 target.



284
285
286
287
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 284

def 包括?(doc)
  return  除非 _unloaded
  _unloaded.WHERE(_id: doc._id).存在吗? || _used.has_key?(doc._id)
end

#检查string

检查只会检查条目是否有良好的数组式打印。

例子:

检查可枚举值。

enumerable.inspect

返回:

  • ( string )

    被检查的枚举。



296
297
298
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 296

def 检查
  条目.检查
end

#last(limit = nil) ⇒ Document

注意:

在没有对条件定义其他排序的情况下自动对_id添加排序,可能会导致性能问题。 如果在使用 #first 或 #last 时遇到意外的性能下降,请改用 #take。 请注意,#take 不保证顺序。

获取可枚举值中的最后一个文档。 将首先检查新文档。 不加载整个可枚举值。

例子:

获取最后一个文档。

enumerable.last

参数:

  • limit 整数 (默认为: nil

    要返回的文档数量。

返回:

  • (文档)

    找到的最后一个文档。



331
332
333
334
335
336
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 331

def last(limit = nil)
  _used.values.try(:last) ||
      _loaded.try(:values).try(:last) ||
      _used[(ul = _unloaded.try(:last, limit)).try(:_id)] ||
      ul
end

# marshal_dumpArray<Object>

提供将可枚举代理转储到 Marshal.dump 所需的数据。

例子:

转储代理。

Marshal.dump(proxy)

返回:

  • ( Array<Object> )

    已转储的数据。



363
364
365
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 363

def marshal_dump
  [_used, _loaded, _unloaded, @exected]
end

# marshal_load (data) ⇒ Array<Object>

加载 Marshal.load 可枚举代理所需的数据。

例子:

加载代理。

Marshal.load(proxy)

返回:

  • ( Array<Object> )

    已转储的数据。



373
374
375
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 373

def marshal_load(数据)
  @_used, @_loaded, @_unloaded, @exected = 数据
end

#resetfalse

将可枚举重置回其持久状态。

例子:

重置可枚举值。

enumerable.reset

返回:

  • ( false )

    始终为 false。



383
384
385
386
387
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 383

def 重置
  _loaded.清除
  _used.清除
  @exected = false
end

# reset_unloaded (criteria) ⇒对象

用新对象重置底层已卸载的条件对象。 使用我的 HABTM 关联来保持底层数组同步。

例子:

重置已卸载的文档。

enumerable.reset_unloaded(criteria)

参数:

  • 条件 ( Criteria )

    要替换的条件。



396
397
398
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 396

def reset_unloaded(条件)
  @_unloaded = 条件 if _unloaded.is_a?(条件)
end

# respond_to? (name, include_private = false) ⇒ true | false

此枚举是否响应提供的方法?

例子:

可枚举对象是否响应该方法?

enumerable.respond_to?(:sum)

参数:

  • 名称 ( string | Symbol )

    方法的名称。

  • include_private ( true | false ) (默认为: false

    是否包含私有方法。

返回:

  • ( true | false )

    可枚举对象是否响应。



410
411
412
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 410

def respond_to?(名称, include_private = false)
  [].respond_to?(名称, include_private) || 
end

# sizeInteger也称为:长度

获取此可枚举值的总大小。 这是所有持久化和非持久化文档的组合。

例子:

获取尺寸。

enumerable.size

返回:

  • ( Integer )

    可枚举项的大小。



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 421

def size
  # 如果 _unloaded 存在,则它将匹配文档设立
  # 属于此关联且已持久化的
  # 到数据库。在以下情况下必须考虑这设立文档:
  # 计算关联的大小,以及任何具有
  # 自从添加以来。
  if _unloaded
    if _used.任何?
      # 请注意,_used 可能包括已_unloaded 的记录
      # matches.如果为关联分配了一个大量,则会出现这种情况
      # 个项目,其中一些已经是关联的元素。
      #
      # 因此,我们需要确保 _unloaded.count 排除任何元素
      # 已存在于 _used 中。

      数数 = _unloaded.not(:_id.in => _used.values.map(:id)).数数
      数数 + _used.values.数数
    else
      _unloaded.数数
    end

  else
    _loaded.数数 + _used.数数
  end
end

# to_json (options = {}) ⇒ string

将 #to_json 发送到条目。

例子:

获取JSON格式的可枚举值。

enumerable.to_json

参数:

  • 选项 哈希 (默认为: {}

    可选参数。

返回:

  • ( string )

    条目全部作为string加载。



457
458
459
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 457

def to_json(选项 = {})
  条目.to_json(选项)
end

#uniqArray<Document>

注意:

此操作会从数据库加载所有文档。

返回可枚举中的所有唯一文档。

例子:

获取所有唯一文档。

enumerable.uniq

返回:

  • ( Array< Document > )

    唯一的文档。



481
482
483
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 481

def uniq
  条目.uniq
end