模块: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)的值。
22 23 24 |
# File 'lib/mongoid/attributes.rb', line 22 def 属性 @attributes end |
实例方法详细信息
# assign_attributes (attrs = nil) ⇒对象
允许您使用 :as 选项传入其键与属性名称(同样与列名称匹配)和角色名称匹配的属性哈希,从而为特定批量分配安全角色设置所有属性。 要绕过批量分配安全性,可以使用 :Without_Protection => true 选项。
218 219 220 221 222 |
# File 'lib/mongoid/attributes.rb', line 218 def assign_attributes(attrs = nil) _assing do process_attributes(attrs) end end |
#attribute_missing? (name) ⇒ true | false
确定文档中是否缺少该属性,因为从缺少字段的数据库加载文档。
249 250 251 |
# File 'lib/mongoid/attributes.rb', line 249 def attribute_missing?(名称) !投影仪.new(__selected_fields).attribute_or_path_allowed?(名称) end |
#attribute_present? (name) ⇒ true | false
确定属性是否存在。
33 34 35 36 37 38 |
# File 'lib/mongoid/attributes.rb', line 33 def attribute_present?(名称) 属性 = read_raw_attribute(名称) !属性.空白? || 属性 == false 救援 Mongoid::Errors::AttributeNotLoaded false end |
# properties_before_type_cast ⇒哈希
获取尚未转换的属性。
46 47 48 |
# File 'lib/mongoid/attributes.rb', line 46 def properties_before_type_cast @attributes_before_type_cast ||= {} end |
# has_attribute? (name) ⇒ true | false
文档是否具有提供的属性?
58 59 60 |
# File 'lib/mongoid/attributes.rb', line 58 def has_attribute?(名称) 属性.键?(名称.to_s) end |
# has_attribute_before_type_cast? (name) ⇒ true | false
文档在分配和类型转换之前是否具有提供的属性?
72 73 74 |
# File 'lib/mongoid/attributes.rb', line 72 def has_attribute_before_type_cast?(名称) properties_before_type_cast.键?(名称.to_s) end |
# process_raw_attribute (name, raw, field) ⇒对象
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
处理刚刚从文档属性中读取的原始属性值。
105 106 107 108 109 |
# File 'lib/mongoid/attributes.rb', line 105 def process_raw_attribute(名称, 原始, 字段) 值 = 字段 ? 字段.demogoize(原始) : 原始 attribute_will_change!(名称) if 值.可调整大小? 值 end |
# read_attribute (name) ⇒ Object也称为: []
从文档属性中读取值。 如果该值不存在,则返回 nil。
88 89 90 91 92 |
# File 'lib/mongoid/attributes.rb', line 88 def read_attribute(名称) 字段 = 字段[名称.to_s] 原始 = read_raw_attribute(名称) process_raw_attribute(名称.to_s, 原始, 字段) end |
# read_attribute_before_type_cast (name) ⇒对象
在类型转换之前从属性中读取值。 如果尚未分配该值,则将使用 read_raw_attribute 返回该属性的现有值。
122 123 124 125 126 127 128 129 |
# File 'lib/mongoid/attributes.rb', line 122 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
属性中删除一个值。 如果该值不存在,则会正常失败。
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/mongoid/attributes.rb', line 141 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 ⇒对象
返回类型转换的属性。
259 260 261 |
# File 'lib/mongoid/attributes.rb', line 259 def typed_attributes attribute_names.map { |名称| [名称, 发送(名称)] }.to_h end |
# write_attribute (name, value) ⇒ Object也称为: []=
将单个属性写入文档属性哈希。 这还将触发更新前后回调,并执行任何必要的类型转换。
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 200 201 202 203 |
# File 'lib/mongoid/attributes.rb', line 164 def write_attribute(名称, 值) validate_writable_field_name!(名称.to_s) field_name = database_field_name(名称) if attribute_missing?(field_name) 提高 Mongoid::Errors::AttributeNotLoaded.new(self.class, field_name) end if attribute_writable?(field_name) _assing do 本地化 = 字段[field_name].try(:localized?) properties_before_type_cast[名称.to_s] = 值 typed_value = typed_value_for(field_name, 值) 除非 属性[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
中的现有属性,所有其他属性将被保留。
235 236 237 |
# File 'lib/mongoid/attributes.rb', line 235 def write_attributes(attrs = nil) assign_attributes(attrs) end |