跳至内容 跳至搜索
命名空间
方法
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 键下注册。

非字符串值将使用其 `to_s` 方法强制转换为字符串。

布尔值 = ActiveModel::Type::Boolean
 

Active Model 布尔值类型

一个表现得像布尔值类型的类,包括用户输入强制转换规则。

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

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

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

十进制 = ActiveModel::Type::Decimal
 

Active Model 十进制类型

十进制属性类型,高精度浮点数值表示。它在 `: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
浮点数 = ActiveModel::Type::Float
 

Active Model 浮点数类型

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

class BagOfCoffee
  include ActiveModel::Attributes

  attribute :weight, :float
end

值将使用其 `to_f` 方法进行强制转换,但以下字符串除外

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

  • `"Infinity"` 将被强制转换为 `Float::INFINITY`。

  • `"-Infinity"` 将被强制转换为 `-Float::INFINITY`。

  • `"NaN"` 将被强制转换为 `Float::NAN`。

    bag = BagOfCoffee.new

    bag.weight = “0.25” bag.weight # => 0.25

    bag.weight = “” bag.weight # => nil

    bag.weight = “NaN” bag.weight # => Float::NAN

不可变字符串 = ActiveModel::Type::ImmutableString
 

Active Model 不可变字符串类型

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

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"
整数 = ActiveModel::Type::Integer
 

Active Model 整数类型

用于表示整数的属性类型。此类型在 :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)

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

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

class Person
  include ActiveModel::Attributes

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

Active Model 字符串类型

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

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

= ActiveModel::Type::Value
 

Active Model 值类型

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

类公共方法

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