类:Mongoid::Association::Referenced::HasMany::Proxy
- 扩展方式:
- Forwardable, ClassMethods
- 定义于:
- lib/mongoid/association/referenced/has_many/proxy.rb
Overview
has_many 关联的透明代理。 对主题文档调用关联 getter 方法时,将返回此类的实例。 该类继承自 Mongoid::Association::Proxy,并将其大部分方法转发到关联的目标,即必须加载的对方集合上的文档大量。
在命名空间下定义
模块: 类方法
常量摘要
从Proxy继承的常量
实例属性摘要
从Proxy继承的属性
#_association 、 #_base 、 #_target
实例方法摘要折叠
-
# << (*args) ⇒ Array<Document> (也:#push)
将文档或文档数组附加到关联中。
-
# 构建 (attributes = {}, type = nil) ⇒ 文档(又作:#new)
从属性构建新文档并将其附加到此关联而不保存。
-
#concat(documents) ⇒ Array<Document>
将文档大量附加到关联中。
-
# 删除 (文档) ⇒ 文档(也:#delete_one)
从关联中删除文档。
-
# delete_all (conditions = nil) ⇒ Integer
在给定的条件下,从数据库中删除所有相关文档。
-
#destroy_all (conditions = nil) ⇒ Integer
在给定的条件下销毁数据库中的所有相关文档。
-
#each (&block) ⇒ Array<Document>
遍历关联中的每个文档并生成提供的区块。
-
#是否存在? (id_or_conditions = :none) ⇒ true | false
确定数据库中是否存在此关联中的任何文档。
-
# find (*args) {|Object| ... } ⇒ 文档 | Array<Document> | nil
根据 ID 或条件在关联上查找匹配文档。
-
#initialize (base, target,association) ⇒ 代理
构造函数
实例化一个新的references_many 关联。
-
# nullify ⇒ 对象(也:#nullify_all)
通过删除外键和引用来删除基本文档和目标文档之间的所有关联,并在此过程中孤立目标文档。
-
# purge ⇒ Many (也:#clear)
清除关联。
-
# Replacement (replacement) ⇒ Many
用提供的目标文档替换关联中的现有文档。
-
# unscoped ⇒ 条件
获取未应用默认范围的文档条件。
类方法中包含的方法
从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
包含在封送处理中的方法
构造函数详情
动态方法处理
此类通过method_missing方法处理动态方法
#method_missing ⇒ Criteria |对象(私有)
如果目标数组没有响应提供的方法,则尝试在类上查找命名范围或条件,然后向那里发送调用。
如果数组上存在该方法,则使用默认代理行为。
TODO:确保我们一致使用 respond_to_missing
anywhere we define method_missing.
rubocop:disable Style/MissingRespondToMissing
465 466 467 468 469 470 471 472 473 |
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 465 ruby2_keywords def method_missing(名称, *args, 和块) if _target.respond_to?(名称) _target.发送(名称, *args, 和块) else klass.发送(:with_scope, 条件) do 条件.public_send(名称, *args, 和块) end end end |
实例方法详细信息
# << (*args) ⇒ Array< Document >也称为: push
将文档或文档数组附加到关联中。 将在此进程中设置父项并更新索引。
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 70 def <<(*args) docs = args.展平 return concat(docs) if docs.size > 1 if (doc = docs.first) 附加(doc) doc.保存 if 持久化? && !_Assigning? && !doc.已验证? end self end |
# 构建 (attributes = {}, type = nil) ⇒文档也称为: new
从属性构建新文档并将其附加到此关联而不保存。
115 116 117 118 119 120 121 122 123 |
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 115 def 构建(属性 = {}, 类型 = nil) 工厂.execute_build(类型 || 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 } end end |
# concat (documents) ⇒ Array< Document >
将文档数组附加到关联中。 对文档执行批量插入,而不是一次持久保存一个文档。
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 92 def concat(文档) docs, inserts = [], [] 文档.每 do |doc| 来年 除非 doc 附加(doc) save_or_delay(doc, docs, inserts) if 持久化? end persist_delayed(docs, inserts) self end |
# delete (document) ⇒ Document也称为: delete_one
从关联中删除文档。 这会将文档的外键设置为零。 如果关联上的依赖选项是 :delete_all 或 :destroy,则会进行相应的删除。
137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 137 def 删除(文档) execute_callbacks_round(:删除, 文档) do 结果 = _target.删除(文档) do |doc| if doc unbind_one(doc) 级联!(doc) 除非 _Assigning? end end 结果.点击 { reset_unloaded } end end |
# delete_all (conditions = nil) ⇒ Integer
在给定的条件下,从数据库中删除所有相关文档。
166 167 168 |
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 166 def delete_all(条件 = nil) remove_all(条件, :delete_all) end |
#destroy_all (conditions = nil) ⇒ Integer
在给定的条件下销毁数据库中的所有相关文档。
182 183 184 |
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 182 def destroy_all(条件 = nil) remove_all(条件, :destroy_all) end |
#each (&block) ⇒数组 < Document >
这会将整个关联加载到内存中。
遍历关联中的每个文档并生成提供的区块。
197 198 199 200 201 202 203 |
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 197 def 每(和块) if 块 _target.每(和块) else to_enum end end |
#是否存在? (id_or_conditions = :none) ⇒ true | false
确定数据库中是否存在此关联中的任何文档。
如果关联包含文档,但所有文档仅存在于应用程序中,即尚未持久保存到数据库中,则此方法返回 false。
即使关联已加载到内存中,此方法也会在每次调用时查询数据库。
227 228 229 |
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 227 def 存在吗?(id_or_conditions = : none) 条件.存在吗?(id_or_conditions) end |
#find(*args) {|Object| ... } ⇒ Document | Array<Document> | nil
每个参数都可以是单独的 ID、ID 数组或嵌套数组。 每个数组都将被展平。
这会将匹配的文档保留在内存中,以便稍后迭代。
根据 ID 或条件在关联上查找匹配文档。
此方法委托给Mongoid::Criteria#find 。 如果没有为该方法指定区块,它将根据所提供的 _id 值返回一个或多个文档。
如果为该方法提供了区块,则它将返回当前 Criteria对象找到的第一个符合区块返回 true 值的文档。
262 263 264 265 266 |
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 262 def find(*args, 和块) 匹配 = 条件.find(*args, 和块) 阵列(匹配).每 { |doc| _target.推动(doc) } 匹配 end |
# nullify ⇒ Object也称为: nullify_all
通过删除外键和引用来删除基本文档和目标文档之间的所有关联,并在此过程中孤立目标文档。
274 275 276 277 278 279 280 |
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 274 def nullify 条件.update_all(foreign_key => nil) _target.清除 do |doc| unbind_one(doc) doc.Changed_attributes.删除(foreign_key) end end |
# purge =" Many " 也称为: clear
清除关联。 如果文档已持久保存,则会从数据库中删除这些文档。
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 291 def purge return nullify 除非 _association.具有破坏性? after_remove_error = nil 条件.delete_all 很多 = _target.清除 do |doc| execute_callback :before_remove, doc unbind_one(doc) doc.销毁 = true 开始 execute_callback :after_remove, doc 救援 StandardError => e after_remove_error = e end end 提高 after_remove_error if after_remove_error 很多 end |
# Replacement (replacement) ⇒ Many
用提供的目标文档替换关联中的现有文档。 如果新目标为零,则执行必要的删除。
324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 324 def 替换(替换) if 替换 new_docs, docs = 替换.compact, [] new_id = new_docs.map(和:_id) remove_not_in(new_id) new_docs.每 do |doc| docs.推动(doc) if doc.发送(foreign_key) != _base.发送(_association.primary_key) end concat(docs) else purge end self end |
# unscoped ⇒条件
获取未应用默认范围的文档条件。
346 347 348 |
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 346 def 未限定作用域 klass.未限定作用域.WHERE(foreign_key => _base.发送(_association.primary_key)) end |