class ULID::Rails::Type

Public Class Methods

new(formatter = Formatter) click to toggle source
Calls superclass method
# File lib/ulid/rails/type.rb, line 11
def initialize(formatter = Formatter)
  @formatter = formatter
  super()
end

Public Instance Methods

cast(value) click to toggle source
Calls superclass method
# File lib/ulid/rails/type.rb, line 16
def cast(value)
  return nil if value.nil?

  value = value.to_s if value.is_a?(Data)
  value = value.unpack("H*")[0] if value.encoding == Encoding::ASCII_8BIT
  value = value[2..-1] if value.start_with?("\\x")

  value.length == 32 ? @formatter.format(value) : super
end
serialize(value) click to toggle source
# File lib/ulid/rails/type.rb, line 26
def serialize(value)
  return if value.nil?

  case ActiveRecord::Base.connection_config[:adapter]
  when "mysql2", "sqlite3"
    Data.new(@formatter.unformat(value))
  when "postgresql"
    Data.new([@formatter.unformat(value)].pack("H*"))
  end
end