类:Mongoid::Association::Embedded::EmbedsMany::Proxy
Overview
embeds_many 关联的透明代理。 对父文档调用关联 getter 方法时,会返回此类的实例。 该类继承自 Mongoid::Association::Proxy,并将其大多数方法转发到关联目标,即子文档大量。
在命名空间下定义
模块: 类方法
常量摘要
从Proxy继承的常量
实例属性摘要
从Proxy继承的属性
#_association 、 #_base 、 #_target
实例方法摘要折叠
-
# << (*args) ⇒ 对象(也:#push)
将文档或文档数组附加到关联中。
-
# _remove (文档) ⇒ 对象
private
*仅在内存中*从集合中删除单个文档。
-
# 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
确定数据库中是否存在此关联中的任何文档。
-
#find {|Object| ... } ⇒ Document | Array<Document> | nil
通过几种不同的方法在该关联中查找文档。
-
#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? 、 #create 、 #create! , #find_or_create_by , #find_or_create_by! 、 #find_or_initialize_by 、 #nil? , #respond_to? 、 #scoped 、 #serializable_hash
从Proxy继承的方法
apply_ordering 、 #extend_proxies 、 #klass 、 #reset_unloaded 、 #substitutable
包含在封送处理中的方法
构造函数详情
#initialize (base, target,association) ⇒ Many
实例化一个新的 embeds_many 关联。
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 67 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:确保我们一致使用 respond_to_missing
anywhere we define method_missing.
rubocop:disable Style/MissingRespondToMissing
551 552 553 554 555 556 557 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 551 ruby2_keywords def method_missing(名称, *args, 和块) return 超 if _target.respond_to?(名称) klass.发送(:with_scope, 条件) do 条件.public_send(名称, *args, 和块) end end |
实例方法详细信息
# << (*args) ⇒对象也称为:推送
将文档或文档数组附加到关联中。 将在此进程中设置父项并更新索引。
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 89 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 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
*仅在内存中*从集合中删除单个文档。 它不会保留更改。
236 237 238 239 240 241 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 236 def _remove(文档) _target.delete_one(文档) _unscoped.delete_one(文档) update_attributes_hash reIndex end |
# as_document ⇒ Array<Hash>
获取此关联作为其在数据库中的表示形式。
110 111 112 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 110 def as_document as_attributes.收集 { |attrs| BSON::文档.new(attrs) } end |
# 构建 (attributes = {}, type = nil) ⇒文档也称为: new
在关联中构建新文档并将其附加到目标。 如果要指定子类,则采用可选类型。
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 138 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
清除关联。 如果文档已持久保存,则会从数据库中删除这些文档。
如果主机文档未持久化,但其 _id 与持久化文档匹配,则对关联调用 #clear 将从数据库中删除该关联的文档,即使应用程序中的文档集(在主机中加载)不同于主机中的文档集。 ,并且主机也可能不包含关联中的任何持久化文档。
165 166 167 168 169 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 165 def 清除 batch_clear(_target.dup) update_attributes_hash self end |
# concat ( Docs ) ⇒数组 < Document >
将文档数组附加到关联中。 对文档执行批量插入,而不是一次持久保存一个文档。
123 124 125 126 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 123 def concat(docs) batch_insert(docs) 除非 docs.空? self end |
#count(*args, &block) ⇒ Integer
返回关联中实际已保存到数据库的文档数量。
如果需要文档总数,请使用 #size。
如果存在参数或区块,#count 将委托给target上的 #count 方法,并将包含持久化和非持久化文档。
193 194 195 196 197 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 193 def 数数(*args, 和块) return _target.数数(*args, 和块) if args.任何? || 块 _target.数数(和:persisted?) end |
# delete (document) ⇒文档| nil也称为: delete_one
从目标中删除提供的文档。 已代理此方法,以便在操作发生后对数组重新创建索引。
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 208 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
删除关联中的所有文档而不运行回调。
254 255 256 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 254 def delete_all(条件 = {}) remove_all(条件, :删除) end |
# delete_if ⇒ EmbedsMany::Proxy |枚举器
删除提供的区块返回 true 的所有文档。
267 268 269 270 271 272 273 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 267 def delete_if return 超 除非 block_given? _target.dup.每 { |doc| 删除(doc) if 产量 doc } self end |
#destroy_all (conditions = {}) ⇒ Integer
在运行回调时销毁关联中的所有文档。
286 287 288 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 286 def destroy_all(条件 = {}) remove_all(条件, :destroy) end |
#是否存在? (id_or_conditions = :none) ⇒ true | false
确定数据库中是否存在此关联中的任何文档。
306 307 308 309 310 311 312 313 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 306 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 |
#find {|Object| ... } ⇒ Document | Array<Document> | nil
通过几种不同的方法在该关联中查找文档。
此方法委托给Mongoid::Criteria#find 。 如果没有为该方法指定区块,它将根据所提供的 _id 值返回一个或多个文档。
如果为该方法提供了区块,则它将返回当前 Criteria对象找到的第一个符合区块返回 true 值的文档。
340 341 342 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 340 def find(...) 条件.find(...) end |
#pop(count = nil) ⇒ Document | Array<Document> | nil
从关联中弹出文档。 可以是单个文档或多个文档,并且会自动保留更改。
365 366 367 368 369 370 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 365 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
将文档从关联中移出。 可以是单个文档或多个文档,并且会自动保留更改。
385 386 387 388 389 390 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 385 def 转变(数数 = nil) return [] if 数数&。zero? docs = _target.first(数数 || 1).每 { |doc| 删除(doc) } (数数.nil? || docs.空?) ? docs.first : docs end |
# Replace ( Docs ) ⇒ Many
用提供的目标文档替换关系中的现有文档。
401 402 403 404 405 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 401 def 替换(docs) batch_replace(docs) update_attributes_hash self end |
# unscoped ⇒条件
返回删除了所有先前作用域的关联。 这是数据库中Docs的精确表示。
414 415 416 417 418 419 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 414 def 未限定作用域 criterion = klass.未限定作用域 criterion. = true criterion.文档 = _unscoped.delete_if(和:marked_for_destruction?) criterion end |