模块:Mongoid::Association::Relatable
- 包括:
- Constrainable , Options
- 包含在:
- Embedded::EmbeddedIn 、 Embedded::EmbedsMany 、 Embedded::EmbedsOne 、 Mongoid::Association::Referenced ::BelongsTo 、Mongoid ::Association::Referenced::HasAndBelongsToMany 、 Mongoid::Association::Referenced::HasMany 、 Mongoid: :Association::Referenced::HasOne
- 定义于:
- lib/mongoid/association/relatable.rb
Overview
该模块提供关联类型之间共享的行为。
常量摘要折叠
- SHARED_OPTIONS =
所有关联类型之间共享的选项。
[ :class_name, :inverse_of, :validate, :extend ].冻结
- PRIMARY_KEY_DEFAULT =
主键默认值。
'_id'.冻结
实例属性摘要折叠
-
#名称⇒ 符号
只读
关联的名称。
-
# options ⇒ 哈希
只读
此关联上的选项。
-
#parent_inclusions ⇒ Array<String>
包含树中位于该关联之上的关联。
实例方法摘要折叠
-
# == (other) ⇒ 对象
将此关联与另一个关联进行比较。
-
#可绑定? (doc) ⇒ true | false
尝试使用此关联绑定对象是否会引发错误。
-
# counter_cache_column_name ⇒ string
获取计数器缓存列名称。
-
# create_relation (所有者, 目标) ⇒ 代理
使用所有者和目标创建关联代理对象。
-
#具有破坏性? ⇒ true | false
依赖方法是否具有破坏性。
-
#扩展⇒ 模块
获取扩展。
-
# foreign_key_check ⇒ string
获取用于检查外键是否已更改的方法的名称。
-
#foreign_key_setter ⇒ string
外键设置器方法的名称。
-
# get_callbacks (callback_type) ⇒ Array<Proc |符号>
获取给定类型的回调。
-
# in_to? ⇒ true | false
此关联是 embedded_in 还是 Belongs_to 关联?
-
#initialize (_class, name, opts = {}, &block) ⇒ 对象
初始化关联。
-
# inverse (other = nil) ⇒ 符号
获取反向名称。
-
# inverse_association (other = nil) ⇒ Mongoid::Association::Relatable
获取反向关联元数据。
-
# inverse_class ⇒ string (又名:#inverse_klass)
拥有此关联的对象的类。
-
# inverse_class_name ⇒ string
拥有此关联的对象的类名。
-
# inverse_setter (other = nil) ⇒ string
反向 setter 方法的名称。
-
# inverse_type ="nil"
获取反向类型。
-
# inverse_type_setter ⇒ string
获取用于在多态关联上设置文档类型的字段的 setter。
-
# inverses (other = nil) ⇒ Array<Symbol>
获取反向名称。
-
#键⇒ 符号 | string
如果此关联存储外键,则为外键字段。
-
#很多? ⇒ true | false
此关联是 embeds_many 还是 has_many 关联?
-
# one? ⇒ true | false
此关联是 embeds_one 还是 has_one 关联?
-
#路径(文档)⇒ Mongoid::Atomic::Paths::Root
此关联的原子路径。
-
# relation_class ⇒ string (又名:#klass)
关联对象的类。
-
# relation_class_name ⇒ string (又作:#class_name)
- 类名,可能是未限定的或
-
关联对象的前缀。
-
# setter ⇒ string
此对象上用于分配关联对象的 setter 名称。
-
# type_setter ⇒ string
获取类型 setter。
-
# validate? ⇒ true | false
是否应验证关联对象。
选项中包含的方法
#as , #autoBuilding? 、 #autosave 、 #cascading_callbacks? , #counter_cached? , #cycle? 、#depend、# forced_nil_inverse ? , #indexed? 、 #inverse_of 、 #order 、 #poly态? 、 #primary_key 、 #store_as 、 #touch_field 、 #touchable? , #type
Constrainable中包含的方法
实例属性详细信息
# name =" Symbol " (只读)
关联的名称。
33 34 35 |
# File 'lib/mongoid/association/relatable.rb', line 33 def 名称 @name end |
# options ⇒哈希(只读)
此关联上的选项。
38 39 40 |
# File 'lib/mongoid/association/relatable.rb', line 38 def @options end |
#parent_inclusions ⇒ Array<String>
包含树中位于该关联之上的关联。
306 307 308 |
# File 'lib/mongoid/association/relatable.rb', line 306 def parent_inclusions @parent_inclusions end |
实例方法详细信息
# == (other) ⇒对象
将此关联与另一个关联进行比较。
62 63 64 65 66 67 |
# File 'lib/mongoid/association/relatable.rb', line 62 def ==(其他) lation_class_name == 其他.lation_class_name && inverse_class_name == 其他.inverse_class_name && 名称 == 其他.名称 && == 其他. end |
#可绑定? (doc) ⇒ true | false
尝试使用此关联绑定对象是否会引发错误。
93 |
# File 'lib/mongoid/association/relatable.rb', line 93 def 可绑定?(doc); false; end |
# counter_cache_column_name ⇒ string
获取计数器缓存列名称。
270 271 272 273 274 |
# File 'lib/mongoid/association/relatable.rb', line 270 def counter_cache_column_name @counter_cache_column_name ||= (@options[:counter_cache].is_a?(字符串) || @options[:counter_cache].is_a?(符号)) ? @options[:counter_cache] : " #{ inverse || inverse_class_name . demodulize . underscore .pluralize } } _count " end |
# create_relation (所有者, 目标) ⇒代理
使用所有者和目标创建关联代理对象。
256 257 258 |
# File 'lib/mongoid/association/relatable.rb', line 256 def create_relation(所有者, 目标) 关系.new(所有者, 目标, self) end |
#具有破坏性? ⇒ true | false
依赖方法是否具有破坏性。
263 264 265 |
# File 'lib/mongoid/association/relatable.rb', line 263 def 具有破坏性? @Destructive ||= !!(依赖 && (依赖 == :delete_all || 依赖 == :destroy)) end |
#扩展⇒模块
获取扩展。
279 280 281 |
# File 'lib/mongoid/association/relatable.rb', line 279 def 扩展 @extension ||= @options[:extend] end |
# foreign_key_check ⇒ string
获取用于检查外键是否已更改的方法的名称。
245 246 247 |
# File 'lib/mongoid/association/relatable.rb', line 245 def foreign_key_check @foreign_key_check ||= " #{ foreign_key } _previously_changed? " if (store_foreign_key? && foreign_key) end |
#foreign_key_setter ⇒ string
外键设置器方法的名称。
215 216 217 218 219 |
# File 'lib/mongoid/association/relatable.rb', line 215 def foreign_key_setter # 注意:您无法检查此关联是否存储了外键 # 查看 HasOne 和 HasMany 绑定,它们引用了 foreign_key_setter @foreign_key_setter ||= " #{ foreign_key } = " if foreign_key end |
# get_callbacks (callback_type) ⇒ Array<Proc |符号>
获取给定类型的回调。
75 76 77 |
# File 'lib/mongoid/association/relatable.rb', line 75 def get_callbacks(callback_type) 阵列([callback_type]) end |
# in_to? ⇒ true | false
此关联是 embedded_in 关联还是 Belongs_to 关联?
333 334 335 |
# File 'lib/mongoid/association/relatable.rb', line 333 def in_to? [引用::属于关联, Embedded::EmbeddedIn].任何? { |a| self.is_a?(a) } end |
#initialize (_class, name, opts = {}, &block) ⇒对象
初始化关联。
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/mongoid/association/relatable.rb', line 46 def 初始化(_class, 名称, opts = {}, 和块) @owner_class = _class @name = 名称 @options = opts @extension = nil @module_path = _class.名称 ? _class.名称.拆分(' :: ')[0..-2].连接 (JOIN)(' :: ') : ' ' @module_path << ' :: ' 除非 @module_path.空? create_extension!(和块) validate! end |
# inverse (other = nil) ⇒符号
获取反向名称。
286 287 288 289 |
# File 'lib/mongoid/association/relatable.rb', line 286 def 反向(其他 = nil) 候选 = inverses(其他) 候选.检测 { |C| C } if 候选 end |
# inverse_association (other = nil) ⇒ Mongoid::Association::Relatable
获取反向关联元数据。
118 119 120 |
# File 'lib/mongoid/association/relatable.rb', line 118 def inverse_association(其他 = nil) (其他 || lation_class).关系[反向(其他)] end |
# inverse_class ⇒ string也称为: inverse_klass
拥有此关联的对象的类。
185 186 187 |
# File 'lib/mongoid/association/relatable.rb', line 185 def inverse_class @owner_class end |
# inverse_class_name ⇒ string
拥有此关联的对象的类名。
178 179 180 |
# File 'lib/mongoid/association/relatable.rb', line 178 def inverse_class_name @inverse_class_name ||= @owner_class.名称 end |
# inverse_setter (other = nil) ⇒ string
反向 setter 方法的名称。
208 209 210 |
# File 'lib/mongoid/association/relatable.rb', line 208 def inverse_setter(其他 = nil) @inverse_setter ||= " #{ inverses ( other ) . first } = " 除非 inverses(其他).空白? end |
# inverse_type =" nil "
获取反向类型。
125 |
# File 'lib/mongoid/association/relatable.rb', line 125 def inverse_type; end |
# inverse_type_setter ⇒ string
获取用于在多态关联上设置文档类型的字段的 setter。
235 236 237 |
# File 'lib/mongoid/association/relatable.rb', line 235 def inverse_type_setter @inverse_type_setter ||= " #{ inverse_type } = " if inverse_type end |
# inverses (other = nil) ⇒ Array<Symbol>
获取反向名称。
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/mongoid/association/relatable.rb', line 101 def inverses(其他 = nil) return [ inverse_of ] if inverse_of return [] if @options.键?(:inverse_of) && !inverse_of if 多态? polymorphic_inverses(其他) else describe_inverses(其他) end end |
#键⇒符号 | string
如果此关联存储外键,则为外键字段。 否则,为主键。
194 195 196 |
# File 'lib/mongoid/association/relatable.rb', line 194 def key store_foreign_key? ? foreign_key : primary_key end |
#很多? ⇒ true | false
此关联是 embeds_many 还是 has_many 关联?
318 319 320 |
# File 'lib/mongoid/association/relatable.rb', line 318 def 很多? [引用::有很多, Embedded::嵌入许多].任何? { |a| self.is_a?(a) } end |
# one? ⇒ true | false
此关联是 embeds_one 还是 has_one 关联?
325 326 327 |
# File 'lib/mongoid/association/relatable.rb', line 325 def 一个? [引用::有一个, Embedded::嵌入一个].任何? { |a| self.is_a?(a) } end |
#路径(文档)⇒ Mongoid::Atomic::Paths::Root
此关联的原子路径。
224 225 226 |
# File 'lib/mongoid/association/relatable.rb', line 224 def 路径(文档) 关系.路径(文档) end |
# relation_class ⇒ string也称为: klass
关联对象的类。
此方法返回与relation_class_name
相对应的类实例,该实例是相对于托管文档类进行解析的。
如果该类不存在,此方法将引发 NameError。 发生这种情况的原因可能是尚未定义目标类。 请注意,多态关联通常没有明确定义的目标类,因为目标类可以从一个对象更改为另一个对象,对多态关联调用此方法通常会失败并显示 NameError 或产生误导性结果(如果一个类确实发生在使用与关联名称相同的名称定义)。
167 168 169 170 171 172 |
# File 'lib/mongoid/association/relatable.rb', line 167 def lation_class @klass ||= 开始 cls_name = @options[:class_name] || ActiveSupport::变形器.分类(名称) resolve_name(inverse_class, cls_name) end end |
# relation_class_name ⇒ string也称为: class_name
此方法的返回值不应用于确定两个关联是否具有相同的目标类,因为返回值并不总是完全限定的类名。 要比较类,请使用relation_class
方法检索关联目标的类实例。
- 类名,可能是未限定的或
-
前缀,关联的
对象。
此方法返回关联定义中使用的类名称。 如果在关联中给出 :class_name 选项,则在此处返回该选项的确切值。 如果未给出 :class_name 选项,则类的名称根据关联名称计算得出,但不会解析为实际的类。
此方法返回的类名可能与定义的类不对应,因为尚未加载相应的类,或者因为关联引用了完全不存在的类。 要获取关联类,请使用relation_class
方法。
148 149 150 |
# File 'lib/mongoid/association/relatable.rb', line 148 def lation_class_name @class_name ||= @options[:class_name] || ActiveSupport::变形器.分类(名称) end |
# setter ⇒ string
此对象上用于分配关联对象的 setter 名称。
201 202 203 |
# File 'lib/mongoid/association/relatable.rb', line 201 def setter @setter ||= " #{ name } = " end |
# type_setter ⇒ string
仅与采用 :as 选项的多态关联相关。
获取类型 setter。
83 84 85 |
# File 'lib/mongoid/association/relatable.rb', line 83 def type_setter @type_setter ||= " #{ type } = " if 类型 end |
# validate? ⇒ true | false
是否应验证关联对象。
295 296 297 298 299 300 301 |
# File 'lib/mongoid/association/relatable.rb', line 295 def validate? @validate ||= if @options[:validate].nil? validation_default else !!@options[:validate] end end |