Class: RLP::Sedes::Binary

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/rlp/sedes/binary.rb

Constant Summary collapse

Infinity =
1.0 / 0.0

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#big_endian_to_int, #bytes_to_str, #encode_hex, #int_to_big_endian, #list?, make_immutable!, #primitive?, #str_to_bytes

Constructor Details

#initialize(min_length: 0, max_length: Infinity, allow_empty: false) ⇒ Binary

Returns a new instance of Binary.


18
19
20
21
22
# File 'lib/rlp/sedes/binary.rb', line 18

def initialize(min_length: 0, max_length: Infinity, allow_empty: false)
  @min_length = min_length
  @max_length = max_length
  @allow_empty = allow_empty
end

Class Method Details

.fixed_length(l, allow_empty: false) ⇒ Object


9
10
11
# File 'lib/rlp/sedes/binary.rb', line 9

def fixed_length(l, allow_empty: false)
  new(min_length: l, max_length: l, allow_empty: allow_empty)
end

.valid_type?(obj) ⇒ Boolean

Returns:

  • (Boolean)

13
14
15
# File 'lib/rlp/sedes/binary.rb', line 13

def valid_type?(obj)
  obj.instance_of?(String)
end

Instance Method Details

#deserialize(serial) ⇒ Object


33
34
35
36
37
38
# File 'lib/rlp/sedes/binary.rb', line 33

def deserialize(serial)
  raise DeserializationError.new("Objects of type #{serial.class} cannot be deserialized", serial) unless primitive?(serial)
  raise DeserializationError.new("#{serial.class} has invalid length", serial) unless valid_length?(serial.size)

  serial
end

#serialize(obj) ⇒ Object

Raises:


24
25
26
27
28
29
30
31
# File 'lib/rlp/sedes/binary.rb', line 24

def serialize(obj)
  raise SerializationError.new("Object is not a serializable (#{obj.class})", obj) unless self.class.valid_type?(obj)

  serial = str_to_bytes obj
  raise SerializationError.new("Object has invalid length", serial) unless valid_length?(serial.size)

  serial
end