class KSUID::ActiveRecord::Type
A string-serialized KSUID
for storage within an ActiveRecord
database
@api private
@example Set an attribute as a KSUID
using the verbose syntax
class Event < ActiveRecord::Base attribute :ksuid, KSUID::ActiveRecord::Type.new, default: -> { KSUID.new } end
@example Set an attribute as a KSUID
using the pre-registered type
class Event < ActiveRecord::Base attribute :ksuid, :ksuid, default: -> { KSUID.new } end
Public Instance Methods
Casts a value from user input into a KSUID
Type
casting happens via the attribute setter and can take input from many places, including:
1. The Rails form builder 2. Directly from the attribute setter 3. From the model initializer
@param value [String, Array<Integer>, KSUID::Type] the value to cast into a KSUID
@return [KSUID::Type] the type-casted value
# File lib/ksuid/activerecord/type.rb, line 30 def cast(value) KSUID.call(value) end
Casts the value from a KSUID
into a database-understandable format
@param value [KSUID::Type, nil] the KSUID
in Ruby format @return [String, nil] the base 62-encoded KSUID
for storage in the database
# File lib/ksuid/activerecord/type.rb, line 48 def serialize(value) return unless value KSUID.call(value).to_s end
The identifier to use within ActiveRecord's type registry
@api private @return [Symbol]
# File lib/ksuid/activerecord/type.rb, line 58 def type :ksuid end