模块:Mongoid::Association::Accessors
Overview
此模块包含与通过 getter 和 setter 访问关联相关的所有行为,以及如何委托构建者以创建新关联。
类方法摘要折叠
-
.define_builder!(关联)⇒ 类
为 embeds_one 关联定义构建器方法。
-
。define_creator!(关联)⇒ 类
为 embeds_one 关联定义创建者方法。
-
。define_existence_check!(关联)⇒ 类
添加关联的存在性检查。
-
。define_getter!(关联)⇒ 类
定义关联的 getter。
-
。 define_ids_getter! (关联)⇒ 类
定义关联中 document ID 的 getter。
-
。define_ids_setter!(关联)→ 对象
定义 setter 方法,允许您通过 ID 在此关联中设立 document。
-
。define_setter!(关联)⇒ 类
定义关联的 setter。
实例方法摘要折叠
-
# __build__ (name, 对象,association,selected_fields = nil) ⇒ 代理
构建相关文档并创建关联,除非该文档为零,然后对此文档设置关联。
-
# create_relation (对象,association,selected_fields = nil) ⇒ 代理
从对象和关联元数据创建关联。
-
# reset_relation_criteria (name) ⇒ 对象
重置关联代理内的条件。
-
# set_relation (name, relation) ⇒ 代理
将提供的关联设置为具有提供名称的类上的实例变量。
类方法详细信息
。define_builder!(关联)⇒ 类
为 embeds_one 关联定义构建器方法。它被定义为 #build_name。
387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/mongoid/association/accessors.rb', line 387 def self.define_builder!(关联) 名称 = 关联.名称 关联.inverse_class.点击 do |klass| klass.re_define_method("build_#{name}") do |*args| 属性, = parse_args(*args) 文档 = 工厂.构建(关联.lation_class, 属性) _Building do 子项 = 发送(" #{ name } = ", 文档) 子项.run_callbacks(:build) 子项 end end end end |
。define_creator!(关联)⇒ 类
为 embeds_one 关联定义创建者方法。它被定义为 #create_name。对象构建后会立即保存。
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
# File 'lib/mongoid/association/accessors.rb', line 412 def self.define_creator!(关联) 名称 = 关联.名称 关联.inverse_class.点击 do |klass| klass.re_define_method("create_#{name}") do |*args| 属性, = parse_args(*args) 文档 = 工厂.构建(关联.klass, 属性) doc = _assing do 发送(" #{ name } = ", 文档) end doc.保存 保存 if new_record? && 关联.store_foreign_key? doc end end end |
。define_existence_check!(关联)⇒ 类
添加关联的存在性检查。
277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/mongoid/association/accessors.rb', line 277 def self.define_existence_check!(关联) 名称 = 关联.名称 关联.inverse_class.点击 do |klass| klass.module_eval <<-END, __FILE__, __LINE__ + 1 def #{名称}? without_autobuild { !__send__(:#{名称}).blank? } end alias :has_#{名称}? :#{名称}? END end end |
。define_getter!(关联)⇒ 类
定义关联的 getter。这里没有什么特别的:只需返回关联的实例变量(如果存在)或构建事物。
299 300 301 302 303 304 305 306 307 308 |
# File 'lib/mongoid/association/accessors.rb', line 299 def self.define_getter!(关联) 名称 = 关联.名称 关联.inverse_class.点击 do |klass| klass.re_define_method(名称) do |重新加载 = false| 值 = get_relation(名称, 关联, nil, 重新加载) 值 = 发送("build_#{name}") if 值.nil? && 关联.自动构建? && !without_autobuild? 值 end end end |
。define_ids_getter!(关联)⇒ 类
定义关联中 document ID 的 getter。应仅在引用的多个关联时指定。
319 320 321 322 323 324 325 326 |
# File 'lib/mongoid/association/accessors.rb', line 319 def self.define_ids_getter!(关联) ids_method = "#{association.name.to_s.singularize}_ids" 关联.inverse_class.点击 do |klass| klass.re_define_method(ids_method) do 发送(关联.名称).采摘(:_id) end end end |
。define_ids_setter!(关联)→ 对象
定义 setter 方法,允许您通过 ID 在此关联中设立 document。定义的 setter 查找具有给定 ID 的 document,并使用找到的 document 调用常规关联 setter。应该仅为引用的许多关联定义 Ids setter。
@param [ Mongoid::Association::Relatable ] 关联 关联的关联。
@return [ Class ] 正在设立的类。
368 369 370 371 372 373 374 375 376 |
# File 'lib/mongoid/association/accessors.rb', line 368 def self.define_ids_setter!(关联) ids_method = "#{association.name.to_s.singularize}_ids=" 关联.inverse_class.aliased_associations[ids_method.印章] = 关联.名称.to_s 关联.inverse_class.点击 do |klass| klass.re_define_method(ids_method) do |id| 发送(关联.setter, 关联.lation_class.find(id.拒绝(和:blank?))) end end end |
。define_setter!(关联)⇒ 类
定义关联的 setter。这会根据某些条件执行一些操作。如果存在关联,则会进行目标替换,否则将使用提供的目标创建新关联。
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/mongoid/association/accessors.rb', line 339 def self.define_setter!(关联) 名称 = 关联.名称 关联.inverse_class.点击 do |klass| klass.re_define_method(" #{ name } = ") do |对象| without_autobuild do if 值 = get_relation(名称, 关联, 对象) 值 = __build__(名称, 值, 关联) 除非 值.respond_to?(:subsubscribe) set_relation(名称, 值.替换(对象.可替换的)) else __build__(名称, 对象.可替换的, 关联) end end end end end |
实例方法详细信息
# __build__ (name, 对象,association,selected_fields = nil) ⇒代理
构建相关文档并创建关联,除非该文档为零,然后对此文档设置关联。
25 26 27 28 |
# File 'lib/mongoid/association/accessors.rb', line 25 def __build__(名称, 对象, 关联, created_fields = nil) 关系 = create_relation(对象, 关联, created_fields) set_relation(名称, 关系) end |
# create_relation (对象,association,selected_fields = nil) ⇒代理
从对象和关联元数据创建关联。
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/mongoid/association/accessors.rb', line 42 def create_relation(对象, 关联, created_fields = nil) key = @attributes[关联.inverse_type] 类型 = key ? 关联.解析器.model_for(key) : nil 目标 = if t = 关联.构建(self, 对象, 类型, created_fields) 关联.create_relation(self, t) end # 只需要对嵌入式关联执行此操作。 待处理的回调 # 仅在物化文档时添加,这种情况只发生 # 针对嵌入式关联。 中没有调用数据库 # 构建引用关联。 if 关联. 阵列(目标).每 do |doc| doc.try(:run_pending_callbacks) end end 目标 end |
# reset_relation_criteria (name) ⇒对象
重置关联代理内的条件。 由多对多关联用于使根本的id大量保持同步。
69 70 71 72 73 |
# File 'lib/mongoid/association/accessors.rb', line 69 def reset_relation_criteria(名称) return 除非 instance_variable_defined?(" @_ #{ name } ") 发送(名称).reset_unloaded end |
# set_relation (name, relation) ⇒代理
将提供的关联设置为具有提供名称的类上的实例变量。 用作助手,只是为了保持代码简洁。
85 86 87 |
# File 'lib/mongoid/association/accessors.rb', line 85 def set_relation(名称, 关系) instance_variable_set(" @_ #{ name } ", 关系) end |