Class: Paquito::CommentPrefixVersion

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

Constant Summary collapse

PREFIX =
"#\u2620"
SUFFIX =
"\u2622\n"
VERSION_POSITION =
PREFIX.bytesize
HEADER_SLICE =
(0..(PREFIX.bytesize + SUFFIX.bytesize))
PAYLOAD_SLICE =
(PREFIX.bytesize + 1 + SUFFIX.bytesize)..-1
DEFAULT_VERSION =
0

Instance Method Summary collapse

Constructor Details

#initialize(current_version, coders) ⇒ CommentPrefixVersion

Returns a new instance of CommentPrefixVersion.



13
14
15
16
17
18
19
20
21
# File 'lib/paquito/comment_prefix_version.rb', line 13

def initialize(current_version, coders)
  unless (0..9).cover?(current_version) && coders.keys.all? { |version| (0..9).cover?(version) }
    raise ArgumentError, "CommentPrefixVersion versions must be between 0 and 9"
  end

  @current_version = current_version
  @coders = coders.transform_values { |c| Paquito.cast(c) }.freeze
  @current_coder = coders.fetch(current_version)
end

Instance Method Details

#dump(object) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/paquito/comment_prefix_version.rb', line 23

def dump(object)
  prefix = +"#{PREFIX}#{@current_version}#{SUFFIX}"
  payload = @current_coder.dump(object)
  if payload.encoding == Encoding::BINARY
    prefix.b << payload
  else
    prefix << payload
  end
end

#load(payload) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/paquito/comment_prefix_version.rb', line 33

def load(payload)
  payload_version, serial = extract_version(payload)

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