Class: ActiveRecord::KSUID::Type Private

Inherits:
Type::String
  • Object
show all
Defined in:
lib/active_record/ksuid/type.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A string-serialized KSUID for storage within an ActiveRecord database

Examples:

Set an attribute as a KSUID using the verbose syntax

class EventWithBareType < ActiveRecord::Base
  attribute :ksuid, ActiveRecord::KSUID::Type.new, default: -> { KSUID.new }
end

Set an attribute as a KSUID using the pre-registered type

class EventWithRegisteredType < ActiveRecord::Base
  attribute :ksuid, :ksuid, default: -> { KSUID.new }
end

Since:

  • 0.5.0

Instance Method Summary collapse

Instance Method Details

#cast(value) ⇒ KSUID::Type

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

  • value (String, Array<Integer>, KSUID::Type)

    the value to cast into a KSUID

Returns:

Since:

  • 0.5.0



30
31
32
# File 'lib/active_record/ksuid/type.rb', line 30

def cast(value)
  ::KSUID.call(value)
end

#deserialize(value) ⇒ KSUID::Type

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a value from database input to a KSUID

Parameters:

  • value (String, nil)

    the database-serialized KSUID to convert

Returns:

Since:

  • 0.5.0



38
39
40
41
42
# File 'lib/active_record/ksuid/type.rb', line 38

def deserialize(value)
  return unless value

  ::KSUID.from_base62(value)
end

#serialize(value) ⇒ String?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Casts the value from a KSUID into a database-understandable format

Parameters:

  • value (KSUID::Type, nil)

    the KSUID in Ruby format

Returns:

  • (String, nil)

    the base 62-encoded KSUID for storage in the database

Since:

  • 0.5.0



48
49
50
51
52
# File 'lib/active_record/ksuid/type.rb', line 48

def serialize(value)
  return unless value

  ::KSUID.call(value).to_s
end