Module: Rlp::Sedes

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

Defined Under Namespace

Classes: BigEndianInt, Binary, List

Class Method Summary collapse

Class Method Details

.big_endian_intRlp::Sedes::BigEndianInt

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

Returns:



43
44
45
# File 'lib/rlp-lite/sedes.rb', line 43

def self.big_endian_int
  @big_endian_int ||= BigEndianInt.new
end

.binaryRlp::Sedes::Binary

A utility to use a binary sedes type.

Returns:



50
51
52
# File 'lib/rlp-lite/sedes.rb', line 50

def self.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.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rlp-lite/sedes.rb', line 16

def self.infer( obj )
  if is_sedes?( obj.class )
    obj.class
  elsif obj.is_a?(Integer) && obj >= 0
    big_endian_int
  elsif Binary.valid_type?( obj )    ## note: same as obj.is_a?( String )
    binary
  elsif Util.is_list?( obj )
    List.new( elements: obj.map { |item| infer( item ) } )
  else
    raise TypeError, "Did not find sedes handling type #{obj.class.name}"
  end
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.



35
36
37
# File 'lib/rlp-lite/sedes.rb', line 35

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