跳至内容 跳至搜索
命名空间
方法
R

常量

BigInteger = ActiveModel::Type::BigInteger
 

Active Model BigInteger 类型

可以序列化为任意字节数的整数的属性类型。此类型在 :big_integer 键下注册。

class Person
  include ActiveModel::Attributes

  attribute :id, :big_integer
end

person = Person.new
person.id = "18_000_000_000"

person.id # => 18000000000

所有转换和序列化都与标准的 ActiveModel::Type::Integer 类型执行方式相同。

Binary = ActiveModel::Type::Binary
 

Active Model Binary 类型

用于表示二进制数据的属性类型。此类型在 :binary 键下注册。

非字符串值使用其 to_s 方法进行强制转换。

Boolean = ActiveModel::Type::Boolean
 

Active Model Boolean 类型

一个行为类似于布尔类型的类,包括用户输入强制转换规则。

  • "false""f""0"0FALSE_VALUES 中的任何其他值将被强制转换为 false

  • 空字符串将被强制转换为 nil

  • 所有其他值将被强制转换为 true

Decimal = ActiveModel::Type::Decimal
 

Active Model Decimal 类型

用于十进制、高精度浮点数表示的属性类型。它在 :decimal 键下注册。

class BagOfCoffee
  include ActiveModel::Attributes

  attribute :weight, :decimal
end

Numeric 实例将被转换为 BigDecimal 实例。任何其他对象都将使用其 to_d 方法进行转换,空字符串除外,空字符串将转换为 nil。如果未定义 to_d 方法,则使用 to_s 方法将对象转换为字符串,然后使用 to_d 进行转换。

bag = BagOfCoffee.new

bag.weight = 0.01
bag.weight # => 0.1e-1

bag.weight = "0.01"
bag.weight # => 0.1e-1

bag.weight = ""
bag.weight # => nil

bag.weight = :arbitrary
bag.weight # => nil (the result of `.to_s.to_d`)

Decimal 的精度默认为 18,在声明属性时可以自定义。

class BagOfCoffee
  include ActiveModel::Attributes

  attribute :weight, :decimal, precision: 24
end
Float = ActiveModel::Type::Float
 

Active Model Float 类型

用于浮点数值的属性类型。它在 :float 键下注册。

class BagOfCoffee
  include ActiveModel::Attributes

  attribute :weight, :float
end

bag = BagOfCoffee.new

bag.weight = "0.25"
bag.weight # => 0.25

bag.weight = ""
bag.weight # => nil

bag.weight = "NaN"
bag.weight # => Float::NAN

除了以下字符串外,值都将使用其 to_f 方法进行转换:

  • 空字符串将被转换为 nil

  • "Infinity" 将被转换为 Float::INFINITY

  • "-Infinity" 将被转换为 -Float::INFINITY

  • "NaN" 将被转换为 Float::NAN

ImmutableString = ActiveModel::Type::ImmutableString
 

Active Model ImmutableString 类型

用于表示不可变字符串的属性类型。它会将传入的值转换为冻结字符串。

class Person
  include ActiveModel::Attributes

  attribute :name, :immutable_string
end

person = Person.new
person.name = 1

person.name # => "1"
person.name.frozen? # => true

值将使用其 to_s 方法强制转换为字符串。但是,Boolean 值会有所不同:true 将转换为 "t"false 将转换为 "f"。这些字符串可以在声明属性时自定义。

class Person
  include ActiveModel::Attributes

  attribute :active, :immutable_string, true: "aye", false: "nay"
end

person = Person.new
person.active = true

person.active # => "aye"
Integer = ActiveModel::Type::Integer
 

Active Model Integer 类型

用于整数表示的属性类型。此类型在 :integer 键下注册。

class Person
  include ActiveModel::Attributes

  attribute :age, :integer
end

值将使用其 to_i 方法进行转换,空字符串除外,空字符串将转换为 nil。如果未定义 to_i 方法或引发错误,则该值将转换为 nil

person = Person.new

person.age = "18"
person.age # => 18

person.age = ""
person.age # => nil

person.age = :not_an_integer
person.age # => nil (because Symbol does not define #to_i)

Serialization 也遵循相同的原理。非数字字符串将序列化为 nil,例如。

Serialization 还会验证整数是否可以使用有限数量的字节存储。如果不能,将引发 ActiveModel::RangeError。默认限制为 4 字节,在声明属性时可以自定义。

class Person
  include ActiveModel::Attributes

  attribute :age, :integer, limit: 6
end
字符串 = ActiveModel::Type::String
 

Active Model String 类型

用于字符串的属性类型。它在 :string 键下注册。

此类是 ActiveModel::Type::ImmutableString 的特化。它的转换方式相同,并且可以以相同的方式进行配置。但是,它考虑了可变字符串,因此脏跟踪可以正确检查字符串是否已更改。

Value = ActiveModel::Type::Value
 

Active Model Value 类型

所有属性类型的基类。此类也作为未指定类型的属性的默认类型。

类公共方法

register(type_name, klass = nil, **options, &block)

向注册表添加一个新类型,允许通过 ActiveRecord::Base.attribute 将其引用为符号。如果您的类型仅用于特定的数据库适配器,则可以通过传递 adapter: :postgresql 来实现。如果您的类型与当前适配器的本机类型同名,则会引发异常,除非您指定 :override 选项。override: true 将导致使用您的类型而不是本机类型。override: false 将导致在存在本机类型的情况下,本机类型优先于您的类型。

# File activerecord/lib/active_record/type.rb, line 37
def register(type_name, klass = nil, **options, &block)
  registry.register(type_name, klass, **options, &block)
end