模块:Mongoid::Attributes
- 扩展方式:
- ActiveSupport::Concern
- 包含在:
- 可组合
- 定义于:
- lib/mongoid/attributes.rb ,
lib/mongoid/attributes/nested.rb,
lib/mongoid/attributes/dynamic.rb,
lib/mongoid/attributes/embedded.rb,
lib/mongoid/attributes/readonly.rb,
lib/mongoid/attributes/projector.rb,
lib/mongoid/attributes/ 处理
Overview
该模块包含处理内部属性哈希的逻辑,以及如何获取和设立值。
在命名空间下定义
模块: ClassMethods 、动态、嵌入式、嵌套、处理、只读类:投影器
实例属性摘要折叠
-
#属性⇒ 对象(也:#raw_attributes)
只读
返回属性(attribute)的值。
实例方法摘要折叠
-
# assign_attributes (attrs = nil) ⇒ 对象
允许您使用 :as 选项传入其键与属性名称(同样与列名称匹配)和角色名称匹配的属性哈希,从而为特定批量分配安全角色设置所有属性。
-
#attribute_missing? (name) ⇒ true | false
确定文档中是否缺少该属性,因为从缺少字段的数据库加载文档。
-
#attribute_present? (name) ⇒ true | false
确定属性是否存在。
-
# properties_before_type_cast ⇒ 哈希
获取尚未转换的属性。
-
# has_attribute? (name) ⇒ true | false
文档是否具有提供的属性?
-
# has_attribute_before_type_cast? (name) ⇒ true | false
文档在分配和类型转换之前是否具有提供的属性?
-
# process_raw_attribute (name, raw, field) ⇒ 对象
private
处理刚刚从文档属性中读取的原始属性值。
-
# read_attribute (name) ⇒ 对象(又作:#[])
从文档属性中读取值。
-
# read_attribute_before_type_cast (name) ⇒ 对象
在类型转换之前从属性中读取值。
-
# remove_attribute (name) ⇒ 对象
从
Document属性中删除一个值。 -
# typed_attributes ⇒ 对象
返回类型转换的属性。
-
# write_attribute (name, value) ⇒ 对象(也:#[]=)
将单个属性写入文档属性哈希。
-
# write_attributes (attrs = nil) ⇒ 对象(又作:#attributes=)
将提供的属性哈希写入文档。
Readonly中包含的方法
Processing中包含的方法
实例属性详细信息
#属性⇒对象(只读)也称为: raw_attributes
返回属性(attribute)的值。
20 21 22 |
# File 'lib/mongoid/attributes.rb', line 20 def 属性 @attributes end |
实例方法详细信息
# assign_attributes (attrs = nil) ⇒对象
允许您使用 :as 选项传入其键与属性名称(同样与列名称匹配)和角色名称匹配的属性哈希,从而为特定批量分配安全角色设置所有属性。 要绕过批量分配安全性,可以使用 :Without_Protection => true 选项。
214 215 216 217 218 |
# File 'lib/mongoid/attributes.rb', line 214 def assign_attributes(attrs = nil) _assing do process_attributes(attrs) end end |
#attribute_missing? (name) ⇒ true | false
确定文档中是否缺少该属性,因为从缺少字段的数据库加载文档。
245 246 247 |
# File 'lib/mongoid/attributes.rb', line 245 def attribute_missing?(名称) !投影仪.new(__selected_fields).attribute_or_path_allowed?(名称) end |
#attribute_present? (name) ⇒ true | false
确定属性是否存在。
31 32 33 34 35 36 |
# File 'lib/mongoid/attributes.rb', line 31 def attribute_present?(名称) 属性 = read_raw_attribute(名称) !属性.空白? || 属性 == false 救援 Mongoid::Errors::AttributeNotLoaded false end |
# properties_before_type_cast ⇒哈希
获取尚未转换的属性。
44 45 46 |
# File 'lib/mongoid/attributes.rb', line 44 def properties_before_type_cast @attributes_before_type_cast ||= {} end |
# has_attribute? (name) ⇒ true | false
文档是否具有提供的属性?
56 57 58 |
# File 'lib/mongoid/attributes.rb', line 56 def has_attribute?(名称) 属性.键?(名称.to_s) end |
# has_attribute_before_type_cast? (name) ⇒ true | false
文档在分配和类型转换之前是否具有提供的属性?
70 71 72 |
# File 'lib/mongoid/attributes.rb', line 70 def has_attribute_before_type_cast?(名称) properties_before_type_cast.键?(名称.to_s) end |
# process_raw_attribute (name, raw, field) ⇒对象
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
处理刚刚从文档属性中读取的原始属性值。
102 103 104 105 106 107 |
# File 'lib/mongoid/attributes.rb', line 102 def process_raw_attribute(名称, 原始, 字段) 值 = 字段 ? 字段.demogoize(原始) : 原始 is_relation = 关系.键?(名称) attribute_will_change!(名称) if 值.可调整大小? && !is_relation 值 end |
# read_attribute (name) ⇒ Object也称为: []
从文档属性中读取值。 如果该值不存在,则返回 nil。
86 87 88 89 90 |
# File 'lib/mongoid/attributes.rb', line 86 def read_attribute(名称) 字段 = 字段[名称.to_s] 原始 = read_raw_attribute(名称) process_raw_attribute(名称.to_s, 原始, 字段) end |
# read_attribute_before_type_cast (name) ⇒对象
在类型转换之前从属性中读取值。如果尚未分配该值,则将使用 read_raw_attribute 返回该属性的现有值。
120 121 122 123 124 125 126 127 |
# File 'lib/mongoid/attributes.rb', line 120 def read_attribute_before_type_cast(名称) attr = 名称.to_s if properties_before_type_cast.键?(attr) properties_before_type_cast[attr] else read_raw_attribute(attr) end end |
# remove_attribute (name) ⇒对象
从Document属性中删除一个值。 如果该值不存在,则会正常失败。
139 140 141 142 143 144 145 146 147 148 |
# File 'lib/mongoid/attributes.rb', line 139 def remove_attribute(名称) validate_writable_field_name!(名称.to_s) as_writable_attribute!(名称) do |访问权限| _assing do attribute_will_change!(访问权限) Delayed_atomic_unsets[atomic_attribute_name(访问权限)] = [] 除非 new_record? 属性.删除(访问权限) end end end |
# typed_attributes ⇒对象
返回类型转换的属性。
255 256 257 |
# File 'lib/mongoid/attributes.rb', line 255 def typed_attributes attribute_names.map { |名称| [ 名称, 发送(名称) ] }.to_h end |
# write_attribute (name, value) ⇒ Object也称为: []=
将单个属性写入文档属性哈希。 这还将触发更新前后回调,并执行任何必要的类型转换。
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/mongoid/attributes.rb', line 162 def write_attribute(名称, 值) validate_writable_field_name!(名称.to_s) field_name = database_field_name(名称) 提高 Mongoid::Errors::AttributeNotLoaded.new(self.class, field_name) if attribute_missing?(field_name) if attribute_writable?(field_name) _assing do 本地化 = 字段[field_name].try(:localized?) properties_before_type_cast[名称.to_s] = 值 typed_value = typed_value_for(field_name, 值) 除非 attribute_will_not_change?(field_name, typed_value) || attribute_changed?(field_name) attribute_will_change!(field_name) end if 本地化 现在 = 字段[field_name].try(:localize_present?) loc_key, loc_val = typed_value.first if 现在 && loc_val.空白? 属性[field_name]&。删除(loc_key) else 属性[field_name] ||= {} 属性[field_name].合并!(typed_value) end else 属性[field_name] = typed_value end # 在写入属性时,同时将其从未设置中删除, # 这样删除然后写入不会导致删除。 Delayed_atomic_unsets.删除(field_name) typed_value end else # TODO: MONGOID-5072 end end |
# write_attributes (attrs = nil) ⇒对象也称为: attributes=
将提供的属性哈希写入文档。 这只会覆盖存在于新Hash中的现有属性,所有其他属性将被保留。
231 232 233 |
# File 'lib/mongoid/attributes.rb', line 231 def write_attributes(attrs = nil) assign_attributes(attrs) end |