class ActiveModel::Type::ImmutableString
person.active # => “aye”
person.active = true
person = Person.new
end
attribute :active, :immutable_string, true: “aye”, false: “nay”
include ActiveModel::Attributes
class Person
declaring an attribute:false
will be cast to "f"
. These strings can be customized when
are treated differently, however: true
will be cast to "t"
and
Values are coerced to strings using their to_s
method. Boolean values
person.name.frozen? # => true
person.name # => “1”
person.name = 1
person = Person.new
end
attribute :name, :immutable_string
include ActiveModel::Attributes
class Person
frozen strings.
Attribute type to represent immutable strings. It casts incoming values to
= Active Model ImmutableString Type
def cast_value(value)
def cast_value(value) case value when true then @true when false then @false else value.to_s.freeze end end
def initialize(**args)
def initialize(**args) @true = -(args.delete(:true)&.to_s || "t") @false = -(args.delete(:false)&.to_s || "f") super end
def serialize(value)
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
def serialize_cast_value(value) # :nodoc:
def serialize_cast_value(value) # :nodoc: value end
def type
def type :string end