Module: Tinycode

Defined in:
lib/tinycode/module.rb,
lib/tinycode/version.rb,
lib/tinycode/fixed_integer.rb

Overview

An encoder and decoder for tinycode, which is a bencode like encoding that supports more data-types and slightly smaller encoded sizes.

Defined Under Namespace

Modules: FixedInteger Classes: ParseError, U16, U32, U64, U8

Constant Summary collapse

VERSION =
'0.3.0'

Class Method Summary collapse

Class Method Details

.dump(obj) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tinycode/module.rb', line 12

def dump(obj)
  case obj
  when Array then dump_array(obj)
  when Hash then dump_hash(obj)
  when String then dump_string(obj)
  when Symbol then dump_symbol(obj)
  when Integer then dump_integer(obj)
  when U8 then dump_fixed_integer(obj, code: '%', pack: 'aC')
  when U16 then dump_fixed_integer(obj, code: '$', pack: 'aS>')
  when U32 then dump_fixed_integer(obj, code: '!', pack: 'aL>')
  when U64 then dump_fixed_integer(obj, code: '#', pack: 'aQ>')
  when nil then "\0".b
  when true then '?T'.b
  when false then '?F'.b
  else raise ArgumentError, "Don't know how to dump #{obj.class}"
  end
end

.load(tinycode) ⇒ Object

Raises:



30
31
32
33
34
35
36
37
# File 'lib/tinycode/module.rb', line 30

def load(tinycode)
  tinycode = implicitly_convert_str(tinycode).b
  struct, length = load_with_length(tinycode)
  raise 'a load_ implementation claims it read past the end of stream!' if length > tinycode.length
  raise ParseError, 'Stream is longer than expected' if length < tinycode.length

  struct
end