Sha256: ff4023868f0a797dc600e8827f53b907f88cd2c329cb7eb2c5ddad948eae2c02

Contents?: true

Size: 1.02 KB

Versions: 8

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module Paquito
  class SingleBytePrefixVersion
    def initialize(current_version, coders)
      @current_version = validate_version(current_version)
      @coders = coders.transform_keys { |v| validate_version(v) }.transform_values { |c| Paquito.cast(c) }
      @current_coder = coders.fetch(current_version)
    end

    def dump(object)
      @current_version.chr(Encoding::BINARY) << @current_coder.dump(object)
    end

    def load(payload)
      payload_version = payload.getbyte(0)
      unless payload_version
        raise UnsupportedCodec, "Missing version byte."
      end

      coder = @coders.fetch(payload_version) do
        raise UnsupportedCodec, "Unsupported packer version #{payload_version}"
      end
      coder.load(payload.byteslice(1..-1))
    end

    private

    def validate_version(version)
      unless (0..255).cover?(version)
        raise ArgumentError, "Invalid version #{version.inspect}, versions must be an integer between 0 and 255"
      end

      version
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
paquito-0.11.2 lib/paquito/single_byte_prefix_version.rb
paquito-0.11.1 lib/paquito/single_byte_prefix_version.rb
paquito-0.11.0 lib/paquito/single_byte_prefix_version.rb
paquito-0.10.0 lib/paquito/single_byte_prefix_version.rb
paquito-0.9.2 lib/paquito/single_byte_prefix_version.rb
paquito-0.9.1 lib/paquito/single_byte_prefix_version.rb
paquito-0.9.0 lib/paquito/single_byte_prefix_version.rb
paquito-0.8.0 lib/paquito/single_byte_prefix_version.rb