module Paquito::Types

Constants

DATE_FORMAT
DATE_TIME_FORMAT
DESERIALIZE_METHOD
SERIALIZE_METHOD
SERIALIZE_PROC
TIME_FORMAT

Do not change those formats, this would break current codecs.

TIME_WITH_ZONE_FORMAT
TYPES

Do not change any code, this would break current codecs. New types can be added as long as they have unique code.

Public Class Methods

define_custom_type(klass, packer: nil, unpacker:) click to toggle source
# File lib/paquito/types.rb, line 249
def define_custom_type(klass, packer: nil, unpacker:)
  CustomTypesRegistry.register(klass, packer: packer, unpacker: unpacker)
end
register(factory, types) click to toggle source
# File lib/paquito/types.rb, line 207
def register(factory, types)
  types.each do |type|
    name = type.name

    # Up to Rails 7 ActiveSupport::TimeWithZone#name returns "Time"
    if name == "Time" && defined?(ActiveSupport::TimeWithZone)
      name = "ActiveSupport::TimeWithZone" if type == ActiveSupport::TimeWithZone
    end

    type_attributes = TYPES.fetch(name)
    factory.register_type(
      type_attributes.fetch(:code),
      type,
      type_attributes
    )
  end
end
register_serializable_type(factory) click to toggle source
# File lib/paquito/types.rb, line 225
def register_serializable_type(factory)
  factory.register_type(
    0x7f,
    Object,
    packer: ->(value) do
      packer = CustomTypesRegistry.packer(value)
      class_name = value.class.to_s
      factory.dump([packer.call(value), class_name])
    end,
    unpacker: ->(value) do
      payload, class_name = factory.load(value)

      begin
        klass = Object.const_get(class_name)
      rescue NameError
        raise ClassMissingError, "missing #{class_name} class"
      end

      unpacker = CustomTypesRegistry.unpacker(klass)
      unpacker.call(payload)
    end
  )
end