Sha256: 3c34a7de70f059ab850e3a01aeadbebb29469b691c7bda3ce8db1a302d373d33

Contents?: true

Size: 1.48 KB

Versions: 4

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

require "paquito/types"
require "paquito/coder_chain"

module Paquito
  class CodecFactory
    def self.build(types = [], freeze: false, serializable_type: false, pool: 1, format_version: Paquito.format_version)
      factory = if types.empty? && !serializable_type
        MessagePack::DefaultFactory
      else
        MessagePack::Factory.new
      end

      Types.register(factory, types, format_version: format_version) unless types.empty?
      Types.register_serializable_type(factory) if serializable_type

      if pool && pool > 0 && factory.respond_to?(:pool)
        factory = factory.freeze.pool(pool, freeze: freeze)
        freeze = false
      end

      MessagePackCodec.new(factory, freeze: freeze)
    end

    class MessagePackCodec
      def initialize(factory, freeze: false)
        @factory = factory
        @freeze = freeze
      end

      def dump(object)
        @factory.dump(object)
      rescue NoMethodError => error
        raise PackError.new(error.message, error.receiver)
      rescue RangeError => error
        raise PackError, "#{error.class.name}, #{error.message}"
      end

      def load(payload)
        if @freeze
          @factory.load(payload, freeze: @freeze)
        else
          @factory.load(payload)
        end
      rescue MessagePack::UnpackError => error
        raise UnpackError, error.message
      rescue IOError => error
        raise UnpackError, "#{error.class.name}, #{error.message}"
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
paquito-0.11.2 lib/paquito/codec_factory.rb
paquito-0.11.1 lib/paquito/codec_factory.rb
paquito-0.11.0 lib/paquito/codec_factory.rb
paquito-0.10.0 lib/paquito/codec_factory.rb