Sha256: 2ca1e3b513a85d292954eb80f9b386c4dc141e70a12a411220010ab6a8aab034

Contents?: true

Size: 1.17 KB

Versions: 4

Compression:

Stored size: 1.17 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)
      factory =
        if types.empty? && !serializable_type
          MessagePack
        else
          MessagePack::Factory.new
        end
      Types.register(factory, types) unless types.empty?
      Types.register_serializable_type(factory) if serializable_type
      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)
        @factory.load(payload, freeze: @freeze)
      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.3.1 lib/paquito/codec_factory.rb
paquito-0.3.0 lib/paquito/codec_factory.rb
paquito-0.2.1 lib/paquito/codec_factory.rb
paquito-0.2.0 lib/paquito/codec_factory.rb