Class: Paquito::SingleBytePrefixVersion

Inherits:
Object
  • Object
show all
Defined in:
lib/paquito/single_byte_prefix_version.rb

Direct Known Subclasses

SingleBytePrefixVersionWithStringBypass

Instance Method Summary collapse

Constructor Details

#initialize(current_version, coders) ⇒ SingleBytePrefixVersion

Returns a new instance of SingleBytePrefixVersion.



5
6
7
8
9
# File 'lib/paquito/single_byte_prefix_version.rb', line 5

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

Instance Method Details

#dump(object) ⇒ Object



11
12
13
# File 'lib/paquito/single_byte_prefix_version.rb', line 11

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

#load(payload) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/paquito/single_byte_prefix_version.rb', line 15

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