Module: Klay::Rlp::Sedes

Defined in:
lib/klay/rlp/sedes.rb,
lib/klay/rlp/sedes/list.rb,
lib/klay/rlp/sedes/binary.rb,
lib/klay/rlp/sedes/big_endian_int.rb

Overview

Provides serializable and deserializable types (SeDes).

Defined Under Namespace

Classes: BigEndianInt, Binary, List

Class Method Summary collapse

Class Method Details

.big_endian_intKlay::Rlp::Sedes::BigEndianInt

A utility to use a big-endian, unsigned integer sedes type with unspecified length.

Returns:



61
62
63
# File 'lib/klay/rlp/sedes.rb', line 61

def big_endian_int
  @big_endian_int ||= BigEndianInt.new
end

.binaryKlay::Rlp::Sedes::Binary

A utility to use a binary sedes type.

Returns:



68
69
70
# File 'lib/klay/rlp/sedes.rb', line 68

def binary
  @binary ||= Binary.new
end

.infer(obj) ⇒ Object

Tries to find a sedes objects suitable for a given Ruby object.

The sedes objects considered are obj's class, big_endian_int and binary. If obj is a list, an List will be constructed recursively.

Parameters:

  • obj (Object)

    the Ruby object for which to find a sedes object.

Raises:

  • (TypeError)

    if no appropriate sedes could be found.



41
42
43
44
45
46
47
# File 'lib/klay/rlp/sedes.rb', line 41

def infer(obj)
  return obj.class if is_sedes? obj.class
  return big_endian_int if obj.is_a?(Integer) && obj >= 0
  return binary if Binary.valid_type? obj
  return List.new(elements: obj.map { |item| infer item }) if Util.is_list? obj
  raise TypeError, "Did not find sedes handling type #{obj.class.name}"
end

.is_sedes?(obj) ⇒ Boolean

Determines if an object is a sedes object.

Parameters:

  • obj (Object)

    the object to check.

Returns:

  • (Boolean)

    true if it's serializable and deserializable.



53
54
55
# File 'lib/klay/rlp/sedes.rb', line 53

def is_sedes?(obj)
  obj.respond_to?(:serialize) && obj.respond_to?(:deserialize)
end