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

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

Overview

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

实例属性摘要折叠

实例方法摘要折叠

构造函数详情

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

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

例子:

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

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

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

Enumerable.new([ post ])

参数:



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

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

实例属性详细信息

# _used对象

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



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

def _used
  @_used
end

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

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



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

attr_accessor :_used, :_loaded, :_unloaded

# _loaded ">对象

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



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

def _loaded
  @_loaded
end

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

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



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

attr_accessor :_used, :_loaded, :_unloaded

# _unloaded ">对象

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



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

def _unloaded
  @_unloaded
end

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

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



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

attr_accessor :_used, :_loaded, :_unloaded

实例方法详细信息

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

将文档附加到可枚举值。

例子:

附加文档。

enumerable << document

参数:

  • 文档 (文档)

    要附加的文档。

返回:



63
64
65
66
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 63

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

# == (other) ⇒ true | false

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

例子:

检查相等性。

enumerable == []

参数:

返回:

  • ( true | false )

    如果对象相等。



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

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

  条目 == 其他.条目
end

# === (other) ⇒ true | false

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

例子:

检查大小写相等性。

enumerable === Array

参数:

  • 其他 ( Object )

    要检查的对象。

返回:

  • ( true | false )

    如果案例中的对象相等。



49
50
51
52
53
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 49

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

  条目 === 其他.条目
end

# _loaded?true | false

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

例子:

是否为可枚举 _loaded?

enumerable._loaded?

返回:

  • ( true | false )

    如果该可枚举已被 _loaded。



351
352
353
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 351

def _loaded?
  !!@exected
end

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

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

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

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

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

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

参数:

  • *args ( Object... )

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

返回:

  • ( true | false )

    如果关联有任何文件。



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

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

  !空?
end

# as_json (options = {}) ⇒哈希

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

例子:

获取JSON格式的可枚举值。

enumerable.as_json

参数:

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

    可选参数。

返回:

  • (哈希)

    条目全部作为哈希加载。



504
505
506
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 504

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

# avg (字段) ⇒ Float | nil

获取可枚举中所有 document 的提供字段的平均值。加载关联后,在内存中进行计算,而不查询数据库。

例子:

获取字段的平均值。

enumerable.avg(:likes)

参数:

  • 字段 (符号)

    要求平均值的字段。

返回:

  • ( Float | nil )

    平均值,如果没有 document,则为 nil。



534
535
536
537
538
539
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 534

def avg(字段)
  values = field_values_for(字段)
  return nil if values.空?

  values.总和 / values.size.to_f
end

# clear (&block) ⇒ 数组 <Document>

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

例子:

清除可枚举项。

enumerable.clear

使用区块清除可枚举项。

enumerable.clear do |doc|
  doc.unbind
end

返回:

  • ( Array< Document > )

    已清除的 _used Docs 。



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

def 清除()
  in_memory() if block_given?
  _loaded.清除  _used.清除
end

# cloneArray< Document >

注意:

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

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

例子:

克隆可枚举值。

enumerable.clone

返回:

  • ( Array< Document > )

    可枚举值的数组克隆。



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

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

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

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

例子:

删除文档。

enumerable.delete(document)

参数:

  • 文档 (文档)

    要删除的文档。

产量:

  • ( doc )

返回:

  • (文档)

    已删除的文档。



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

def 删除(文档)
  doc = _loaded.删除(文档._id) || _used.删除(文档._id)
  if !doc && _unloaded && _unloaded.WHERE(_id: 文档._id).存在吗?
    产量(文档) if block_given?
    return 文档
  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 。



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

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。



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

def 
  return to_enum 除非 block_given?

  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 )

    如果可枚举为空。



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

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

#first(limit = nil) ⇒ Document

注意:

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

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

例子:

获取第一个文档。

enumerable.first

参数:

  • limit 整数 (默认为: nil

    要返回的文档数量。

返回:

  • (文档)

    找到的第一个文档。



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

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 。



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

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.



281
282
283
284
285
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 281

def 包括?(doc)
  return  除非 _unloaded

  _unloaded.WHERE(_id: doc._id).存在吗? || _used.has_key?(doc._id)
end

#检查string

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

例子:

检查可枚举值。

enumerable.inspect

返回:

  • ( string )

    被检查的枚举。



294
295
296
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 294

def 检查
  条目.检查
end

#last(limit = nil) ⇒ Document

注意:

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

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

例子:

获取最后一个文档。

enumerable.last

参数:

  • limit 整数 (默认为: nil

    要返回的文档数量。

返回:

  • (文档)

    找到的最后一个文档。



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

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> )

    已转储的数据。



361
362
363
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 361

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

# marshal_load (data) ⇒ Array<Object>

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

例子:

加载代理。

Marshal.load(proxy)

返回:

  • ( Array<Object> )

    已转储的数据。



371
372
373
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 371

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

#max(field = nil) ⇒ Numeric | nil

获取可枚举中所有document的提供字段的最大值。加载关联后,在内存中进行计算,而不查询数据库。

例子:

获取字段的最大值。

enumerable.max(:likes)

参数:

  • 字段 符号 (默认为: nil

    字段最大值。

返回:

  • (Numeric | nil)

    最大值;如果没有 document,则为 nil。



567
568
569
570
571
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 567

def Max(字段 = nil)
  return () if block_given?

  field_values_for(字段).Max
end

#min(field = nil) ⇒ Numeric | nil

获取可枚举中所有 document 的所提供字段的最小值。加载关联后,在内存中进行计算,而不查询数据库。

例子:

Get 字段的最小值。

enumerable.min(:likes)

参数:

  • 字段 符号 (默认为: nil

    要最小化的字段。

返回:

  • (Numeric | nil)

    最小值;如果没有 document,则为 nil。



551
552
553
554
555
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 551

def min(字段 = nil)
  return () if block_given?

  field_values_for(字段).min
end

# pluck(*keys) ⇒ 数组 | Array<Array>

从目标中的document中提取给定的字段名称。如果集合已加载,则从加载的document中提取;否则,它会从卸载的条件中提取数据。无论如何,它也会从任何添加的document中提取内容。

参数:

  • *fields (符号... )

    要提取的字段名称。

返回:

  • ( 数组 | 数组<Array> )

    字段值的数组。如果给定了多个字段,则返回一个数组的大量。



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 384

def 采摘(*密钥)
  [].点击 do |结果|
    if _loaded? || _used.任何?
      klass = @_association.klass
      已准备就绪 = prepare_puck(密钥, document_class: klass)
    end

    if _loaded?
      docs = _loaded.values.map { |v| BSON::文档.new(v.属性) }
      结果.concat pluck_from_documents(docs, 已准备就绪[:field_names], document_class: klass)
    elsif _unloaded
      条件 = if _used.任何?
                   ids_to_exclude = _used.密钥
                   _unloaded.not(:_id.in => ids_to_exclude)
                 else
                   _unloaded
                 end

      结果.concat 条件.采摘(*密钥)
    end

    if _used.任何?
      docs = _used.values.map { |v| BSON::文档.new(v.属性) }
      结果.concat pluck_from_documents(docs, 已准备就绪[:field_names], document_class: klass)
    end
  end
end

#resetfalse

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

例子:

重置可枚举值。

enumerable.reset

返回:

  • ( false )

    始终为 false。



418
419
420
421
422
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 418

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

# reset_unloaded (criteria) ⇒对象

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

例子:

重置已卸载的文档。

enumerable.reset_unloaded(criteria)

参数:

  • 条件 ( Criteria )

    要替换的条件。



431
432
433
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 431

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 )

    可枚举对象是否响应。



445
446
447
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 445

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

# sizeInteger也称为:长度

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

例子:

获取尺寸。

enumerable.size

返回:

  • ( Integer )

    可枚举项的大小。



456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 456

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

#sum (field = nil) ⇒数值

获取可枚举中所有document的提供字段的总和。加载关联后,在内存中进行计算,而不查询数据库。

例子:

获取字段的总和。

enumerable.sum(:likes)

参数:

  • 字段 符号 (默认为: nil

    要求和的字段。

返回:

  • (数值)

    总和值。



518
519
520
521
522
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 518

def 总和(字段 = nil)
  return (字段 || 0) if block_given?

  field_values_for(字段).总和 || 0
end

# to_json (options = {}) ⇒ string

将 #to_json 发送到条目。

例子:

获取JSON格式的可枚举值。

enumerable.to_json

参数:

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

    可选参数。

返回:

  • ( string )

    条目全部作为string加载。



492
493
494
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 492

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

#uniqArray<Document>

注意:

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

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

例子:

获取所有唯一文档。

enumerable.uniq

返回:

  • ( Array< Document > )

    唯一的文档。



581
582
583
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 581

def uniq
  条目.uniq
end