类:Mongoid::Association::Embedded::EmbedsMany::Proxy
Overview
embeds_many 关联的透明代理。 对父文档调用关联 getter 方法时,会返回此类的实例。 该类继承自 Mongoid::Association::Proxy,并将其大多数方法转发到关联目标,即子文档大量。
在命名空间下定义
模块: 类方法
实例方法摘要折叠
-
# << (*args) ⇒ 对象(也:#push)
将文档或文档数组附加到关联中。
-
# _remove (文档) ⇒ 对象
private
仅从内存中的集合中in memory only删除单个 document。
-
# as_document ⇒ Array<Hash>
获取此关联作为其在数据库中的表示形式。
-
# 构建 (attributes = {}, type = nil) ⇒ 文档(又作:#new)
在关联中构建新文档并将其附加到目标。
-
# clear ⇒ self
清除关联。
-
# concat ( Docs ) ⇒ Array<Document>
将文档大量附加到关联中。
-
#count(*args, &block) ⇒ Integer
返回关联中实际已保存到数据库的文档数量。
-
# 删除 (文档) ⇒ 文档 | nil (又作:#delete_one)
从目标中删除提供的文档。
-
# delete_all (conditions = {}) ⇒ Integer
删除关联中的所有文档而不运行回调。
-
# delete_if ⇒ EmbedsMany::Proxy |枚举器
删除提供的区块返回 true 的所有文档。
-
#destroy_all (conditions = {}) ⇒ Integer
在运行回调时销毁关联中的所有文档。
-
#是否存在? (id_or_conditions = :none) ⇒ true | false
确定数据库中是否存在此关联中的任何文档。
-
#initialize (base, target,association) ⇒ Many
构造函数
实例化一个新的 embeds_many 关联。
-
#pop (count = nil) ⇒ 文档 | Array<Document> | nil
从关联中弹出文档。
-
# shift (count = nil) ⇒ 文档 | Array<Document> | nil
将文档从关联中移出。
-
# Replace ( Docs ) ⇒ Many
用提供的目标文档替换关系中的现有文档。
-
# unscoped ⇒ 条件
返回删除了所有先前作用域的关联。
类方法中包含的方法
ager_loader ,嵌入式? , foreign_key_suffix
Batchable 中包含的方法
#batch_clear 、 #batch_insert 、 #batch_remove 、 #batch_replace
位置包含的方法
从Many继承的方法
#blank?、#cache_version、#create、#create!、#find_or_create_by、#find_or_create_by!、#find_or_initialize_by、#nil?、#respond_to?、#scoped、#serializable_hash
构造函数详情
#initialize (base, target,association) ⇒ Many
实例化一个新的 embeds_many 关联。
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 70 def 初始化(基础, 目标, 关联) 超 do _target.each_with_index do |doc, 索引(index)| 集成(doc) doc._index = 索引(index) end update_attributes_hash @_unscoped = _target.dup @_target = 范围(_target) end end |
动态方法处理
此类通过method_missing方法处理动态方法
#method_missing ⇒ Criteria |对象(私有)
如果目标数组没有响应提供的方法,则尝试在类上查找命名范围或条件,然后向那里发送调用。
如果数组上存在该方法,则使用默认代理行为。
TODO:确保在定义 method_missing 的任何地方一致使用 respond_to_missing。rubocop:disable Style/MissingRespondToMissing
525 526 527 528 529 530 531 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 525 ruby2_keywords def method_missing(名称, *args, 和块) return 超 if _target.respond_to?(名称) klass.发送(:with_scope, 条件) do 条件.public_send(名称, *args, 和块) end end |
实例方法详细信息
# << (*args) ⇒对象也称为:推送
将文档或文档数组附加到关联中。 将在此进程中设置父项并更新索引。
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 92 def <<(*args) docs = args.展平 return 除非 docs.任何? return concat(docs) if docs.size > 1 docs.first.点击 do |doc| 附加(doc) doc.保存 if 持久化? && !_Assigning? end self end |
# _remove (document) ⇒对象
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
仅从内存中的集合中删除单个 document。它不会保留更改。
239 240 241 242 243 244 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 239 def _remove(文档) _target.delete_one(文档) _unscoped.delete_one(文档) update_attributes_hash reIndex end |
# as_document ⇒ Array<Hash>
获取此关联作为其在数据库中的表示形式。
113 114 115 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 113 def as_document as_attributes.收集 { |attrs| BSON::文档.new(attrs) } end |
# 构建 (attributes = {}, type = nil) ⇒文档也称为: new
在关联中构建新文档并将其附加到目标。 如果要指定子类,则采用可选类型。
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 141 def 构建(属性 = {}, 类型 = nil) 工厂.execute_build(类型 || _association.klass, 属性, execute_callbacks: false).点击 do |doc| 附加(doc) doc.apply_post_processed_defaults 产量 doc if block_given? doc.run_pending_callbacks doc.run_callbacks(:build) { doc } _base._reset_memoized_descendants! end end |
# clear ⇒ self
清除关联。 如果文档已持久保存,则会从数据库中删除这些文档。
如果托管 document 未持久化,但其_id与持久化 document 匹配,则对关联调用 #clear 将从数据库中删除该关联的 document,即使应用程序中的 document 设立(在托管中加载)不同于,并且数据库也可能不包含关联中的任何持久化 document。
168 169 170 171 172 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 168 def 清除 batch_clear(_target.dup) update_attributes_hash self end |
# concat ( Docs ) ⇒数组 < Document >
将文档数组附加到关联中。 对文档执行批量插入,而不是一次持久保存一个文档。
126 127 128 129 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 126 def concat(docs) batch_insert(docs) 除非 docs.空? self end |
#count(*args, &block) ⇒ Integer
返回关联中实际已保存到数据库的文档数量。
如果需要文档总数,请使用 #size。
如果存在参数或区块,#count 将委托给target上的 #count 方法,并将包含持久化和非持久化文档。
196 197 198 199 200 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 196 def 数数(*args, 和块) return _target.数数(*args, 和块) if args.任何? || 块 _target.数数(和:persisted?) end |
# delete (document) ⇒文档| nil也称为: delete_one
从目标中删除提供的文档。 已代理此方法,以便在操作发生后对数组重新创建索引。
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 211 def 删除(文档) execute_callbacks_round(:删除, 文档) do _target.delete_one(文档).点击 do |doc| if doc && !_绑定? _unscoped.delete_one(doc) if _Assigning? _base.add_atomic_pull(doc) else doc.删除(抑制: true) unbind_one(doc) end update_attributes_hash end reIndex end end end |
# delete_all (conditions = {}) ⇒ Integer
删除关联中的所有文档而不运行回调。
257 258 259 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 257 def delete_all(条件 = {}) remove_all(条件, :删除) end |
# delete_if ⇒ EmbedsMany::Proxy |枚举器
删除提供的区块返回 true 的所有文档。
270 271 272 273 274 275 276 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 270 def delete_if return 超 除非 block_given? _target.dup.每 { |doc| 删除(doc) if 产量 doc } self end |
#destroy_all (conditions = {}) ⇒ Integer
在运行回调时销毁关联中的所有文档。
289 290 291 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 289 def destroy_all(条件 = {}) remove_all(条件, :destroy) end |
#是否存在? (id_or_conditions = :none) ⇒ true | false
确定数据库中是否存在此关联中的任何文档。
309 310 311 312 313 314 315 316 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 309 def 存在吗?(id_or_conditions = : none) 案例 id_or_conditions when : none then _target.任何?(和:persisted?) when nil, false then false when 哈希 then WHERE(id_or_conditions).任何?(和:persisted?) else WHERE(_id: id_or_conditions).任何?(和:persisted?) end end |
#pop(count = nil) ⇒ Document | Array<Document> | nil
从关联中弹出文档。 可以是单个文档或多个文档,并且会自动保留更改。
339 340 341 342 343 344 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 339 def Pop(数数 = nil) return [] if 数数&。zero? docs = _target.last(数数 || 1).每 { |doc| 删除(doc) } (数数.nil? || docs.空?) ? docs.first : docs end |
#shift(count = nil) ⇒ Document | Array<Document> | nil
将文档从关联中移出。 可以是单个文档或多个文档,并且会自动保留更改。
359 360 361 362 363 364 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 359 def 转变(数数 = nil) return [] if 数数&。zero? docs = _target.first(数数 || 1).每 { |doc| 删除(doc) } (数数.nil? || docs.空?) ? docs.first : docs end |
# Replace ( Docs ) ⇒ Many
用提供的目标文档替换关系中的现有文档。
375 376 377 378 379 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 375 def 替换(docs) batch_replace(docs) update_attributes_hash self end |
# unscoped ⇒条件
返回删除了所有先前作用域的关联。 这是数据库中Docs的精确表示。
388 389 390 391 392 393 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 388 def 未限定作用域 criterion = klass.未限定作用域 criterion. = true criterion.文档 = _unscoped.delete_if(和:marked_for_destruction?) criterion end |