Sha256: 679a3a23f913ba3718f40925e750bcfc4721b76b10ae7e58d1393ee75bac0ae3

Contents?: true

Size: 1002 Bytes

Versions: 1

Compression:

Stored size: 1002 Bytes

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)
      end

      def load(payload)
        @factory.load(payload, freeze: @freeze)
      rescue MessagePack::UnpackError => error
        raise UnpackError, error.message
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
paquito-0.1.0 lib/paquito/codec_factory.rb