模块: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动态嵌入式嵌套处理只读类:投影器

实例属性摘要折叠

实例方法摘要折叠

Readonly中包含的方法

#attribute_writable?

Processing中包含的方法

#process_attributes

实例属性详细信息

#属性对象(只读)也称为: raw_attributes

返回属性(attribute)的值。



22
23
24
# File 'lib/mongoid/attributes.rb', line 22

def 属性
  @attributes
end

实例方法详细信息

# assign_attributes (attrs = nil) ⇒对象

允许您使用 :as 选项传入其键与属性名称(同样与列名称匹配)和角色名称匹配的属性哈希,从而为特定批量分配安全角色设置所有属性。 要绕过批量分配安全性,可以使用 :Without_Protection => true 选项。

例子:

分配属性。

person.assign_attributes(:title => "Mr.")

分配属性(使用角色)。

person.assign_attributes({ :title => "Mr." }, :as => :admin)

参数:

  • attrs 哈希 (默认为: nil

    要设立的新属性。



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

确定文档中是否缺少该属性,因为从缺少字段的数据库加载文档。

例子:

是否缺少属性?

document.attribute_missing?("test")

参数:

  • 名称 ( string )

    属性的名称。

返回:

  • ( 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

确定属性是否存在。

例子:

该属性是否存在?

person.attribute_present?("title")

参数:

  • 名称 ( string | Symbol )

    属性的名称。

返回:

  • ( true | false )

    如果存在则为 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哈希

获取尚未转换的属性。

例子:

在类型转换之前获取属性。

document.attributes_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

文档是否具有提供的属性?

例子:

文档是否具有该属性?

model.has_attribute?(:name)

参数:

  • 名称 ( string | Symbol )

    属性的名称。

返回:

  • ( 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

文档在分配和类型转换之前是否具有提供的属性?

例子:

文档在分配之前是否具有该属性?

model.has_attribute_before_type_cast?(:name)

参数:

  • 名称 ( string | Symbol )

    属性的名称。

返回:

  • ( true | false )

    如果该键存在于 attributes_before_type_cast 中。



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 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

处理刚刚从文档属性中读取的原始属性值。

参数:

  • 名称 ( string )

    要获取的属性的名称。

  • 原始 ( Object )

    原始属性值。

  • 字段 ( Field | nil )

    用于去对象化的字段,或为 nil。

返回:

  • ( Object )

    属性的值。



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。

例子:

读取属性。

person.read_attribute(:title)

读取属性(备用语法)。

person[:title]

参数:

  • 名称 ( string | Symbol )

    要获取的属性的名称。

返回:

  • ( Object )

    属性的值。



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 返回该属性的现有值。

例子:

在类型转换之前读取属性。

person.read_attribute_before_type_cast(:price)

参数:

  • 名称 ( string | Symbol )

    要获取的属性的名称。

返回:

  • ( Object )

    类型转换之前的属性值(如果可用)。 否则,为属性的值。



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属性中删除一个值。 如果该值不存在,则会正常失败。

例子:

删除该属性。

person.remove_attribute(:title)

参数:

  • 名称 ( string | Symbol )

    要删除的属性的名称。

引发:



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对象

返回类型转换的属性。

例子:

类型转换的属性。

document.typed_attributes

返回:

  • ( Object )

    包含类型转换属性的键和值的哈希。



259
260
261
# File 'lib/mongoid/attributes.rb', line 259

def typed_attributes
  attribute_names.map { |名称| [名称, 发送(名称)] }.to_h
end

# write_attribute (name, value) ⇒ Object也称为: []=

将单个属性写入文档属性哈希。 这还将触发更新前后回调,并执行任何必要的类型转换。

例子:

写入属性。

person.write_attribute(:title, "Mr.")

写入属性(备用语法)。

person[:title] = "Mr."

参数:

  • 名称 ( string | Symbol )

    要更新的属性的名称。

  • ( 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中的现有属性,所有其他属性将被保留。

例子:

写入属性。

person.write_attributes(:title => "Mr.")

写入属性(备用语法)。

person.attributes = { :title => "Mr." }

参数:

  • attrs 哈希 (默认为: nil

    要设立的新属性。



235
236
237
# File 'lib/mongoid/attributes.rb', line 235

def write_attributes(attrs = nil)
  assign_attributes(attrs)
end