模块:Mongoid::Fields::ClassMethods
- 定义于:
- lib/mongoid/fields.rb ,
lib/mongoid/fields.rb
实例方法摘要折叠
-
#attribute_names ⇒ Array<String>
返回此对象可用属性的名称大量。
-
# clean_localized_field_names (name) ⇒ 字段
从给定字段名称中删除 _translations。
-
# database_field_name (name) ⇒ string
获取所提供字段存储在数据库中的名称。
-
#extract_id_field (attributes) ⇒ 对象
private
根据此类中定义的别名,从指定属性哈希中提取ID字段。
-
# 字段 (name, options = {}) ⇒ Field
定义文档上可访问的所有字段。对于定义的每个字段,都会将 getter 和 setter 作为实例方法添加到文档中。
-
# id_fields ⇒ Array<Symbol | string >
private
以字符串和符号形式返回此模型类的ID字段列表。
-
# replace_field (name, type) ="Serializable"
将字段替换为新类型。
-
# traverse_association_tree (key) {|The, The,是否| ... } ⇒ 字段
private
向下遍历关联树并搜索给定键的字段。
-
# using_object_id? ⇒ true | false
确定是否使用
BSON::ObjectIds
作为 ID 的便捷方法。
实例方法详细信息
#attribute_names ⇒ Array<String>
返回此对象可用属性的名称大量。
以与 ORM 无关的方式提供字段名称。 Rails v 3.1 + 使用此方法自动包装JSON请求中的参数。
454 455 456 |
# File 'lib/mongoid/fields.rb', line 454 def attribute_names 字段.密钥 end |
# clean_localized_field_names (name) ⇒字段
从给定字段名称中删除 _translations。 仅当尚不存在具有相同名称的字段名称或关系(即带有 _translations 后缀)时,才会执行此操作。 对现有字段的检查是以递归方式完成的
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/mongoid/fields.rb', line 100 def clean_localized_field_names(名称) 名称 = database_field_name(名称.to_s) klass = self [].点击 do |res| ar = 名称.拆分(' 。 ') ar.each_with_index do |fn, i| key = fn 除非 klass.字段.键?(fn) || klass.关系.键?(fn) if fn.end_with?(TRANSLATIONS_SFX) key = fn.delete_suffix(TRANSLATIONS_SFX) else key = fn end end res.推动(key) if klass.字段.键?(fn) res.推动(ar.删除(i+1).连接 (JOIN)(' 。 ')) 除非 i == ar.长度 - 1 中断 elsif klass.关系.键?(fn) klass = klass.关系[key].klass end end end.连接 (JOIN)(' 。 ') end |
# database_field_name (name) ⇒ string
获取所提供字段存储在数据库中的名称。 用于确定字段是否有别名。
464 465 466 |
# File 'lib/mongoid/fields.rb', line 464 def database_field_name(名称) 字段.database_field_name(名称, 关系, aliased_fields, aliased_associations) end |
#extract_id_field (attributes) ⇒对象
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
根据此类中定义的别名,从指定属性哈希中提取ID字段。
83 84 85 86 87 88 89 90 |
# File 'lib/mongoid/fields.rb', line 83 def extract_id_field(属性) id_fields.每 do |k| if v = 属性[k] return v end end nil end |
# 字段 (name, options = {}) ⇒ Field
定义文档上可访问的所有字段。对于定义的每个字段,都会将 getter 和 setter 作为实例方法添加到文档中。
483 484 485 486 487 488 489 490 491 |
# File 'lib/mongoid/fields.rb', line 483 def 字段(名称, = {}) 命名 = 名称.to_s 验证器::宏.验证(self, 名称, ) 添加 = add_field(命名, ) descendants.每 do |子类| 子类.add_field(命名, ) end 添加 end |
# id_fields ⇒ Array<Symbol | string >
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
以字符串和符号形式返回此模型类的ID字段列表。
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mongoid/fields.rb', line 64 def id_fields IDS.dup.点击 do |id_fields| aliased_fields.每 do |k, v| if v == '_id' id_fields << k.to_sym id_fields << k end end end end |
# replace_field (name, type) =" Serializable "
将字段替换为新类型。
502 503 504 505 |
# File 'lib/mongoid/fields.rb', line 502 def replace_field(名称, 类型) remove_defaults(名称) add_field(名称, 字段[名称]..合并(merge)(类型: 类型)) end |
# traverse_association_tree (key) {|The, The,是否| ... } ⇒字段
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
向下遍历关联树并搜索给定键的字段。
533 534 535 |
# File 'lib/mongoid/fields.rb', line 533 def traverse_association_tree(key, 和块) 字段.traverse_association_tree(key, 字段, 关系, aliased_associations, 和块) end |
# using_object_id? ⇒ true | false
确定是否使用BSON::ObjectIds
作为 ID 的便捷方法。
514 515 516 |
# File 'lib/mongoid/fields.rb', line 514 def using_object_id? 字段[" _id "].object_id_field? end |