class ActiveRecord::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"
Открытые методы класса
# File activemodel/lib/active_model/type/immutable_string.rb, line 40 def initialize(**args) @true = -(args.delete(:true)&.to_s || "t") @false = -(args.delete(:false)&.to_s || "f") super end
Вызывает метод суперкласса
Открытые методы экземпляра
# File activemodel/lib/active_model/type/immutable_string.rb, line 50 def serialize(value) case value when ::Numeric, ::Symbol, ActiveSupport::Duration then value.to_s when true then @true when false then @false else super end end
Вызывает метод суперкласса
# File activemodel/lib/active_model/type/immutable_string.rb, line 46 def type :string end
© 2004–2021 David Heinemeier Hansson
Licensed under the MIT License.