跳至內容 跳至搜尋
命名空間
方法
R

常數

BigInteger (大整數) = ActiveModel::Type::BigInteger
 

Active Model 大整數類型

用於可以序列化為無限位元組的整數的屬性類型。此類型註冊在 `: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` 方法強制轉換為字串。

Boolean (布林值) = ActiveModel::Type::Boolean
 

Active Model 布林值類型

一個行為類似布林值類型的類別,包括使用者輸入強制轉換的規則。

  • `"false"`、`"f"`、`"0"`、`0` 或 `FALSE_VALUES` 中的任何其他值將被強制轉換為 `false`。

  • 空字串被強制轉換為 `nil`。

  • 所有其他值將被強制轉換為 `true`。

Decimal (十進位) = 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
Float (浮點數) = 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

ImmutableString (不可變字串) = 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"
Integer (整數) = 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
String (字串) = ActiveModel::Type::String
 

Active Model 字串類型

用於字串的屬性類型。它註冊在 `:string` 鍵下。

此類別是 `ActiveModel::Type::ImmutableString` 的特化。它的強制轉換方式相同,並且可以以相同的方式進行配置。但是,它考慮了可變字串,因此髒污追蹤可以正確檢查字串是否已更改。

Value (值) = ActiveModel::Type::Value
 

Active Model 值類型

所有屬性類型的基類。此類別也作為未指定類型的屬性的預設類型。

類別公共方法

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

將新的類型添加到註冊表,允許 ActiveRecord::Base.attribute 將其作為符號引用。如果您的類型僅 meant to be used with a specific database adapter,您可以通過傳遞 `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