Class: RLP::Sedes::List
- Inherits:
-
Array
- Object
- Array
- RLP::Sedes::List
- Defined in:
- lib/rlp/sedes/list.rb
Overview
A sedes for lists of fixed length
Instance Method Summary collapse
- #deserialize(serial) ⇒ Object
-
#initialize(elements: [], strict: true) ⇒ List
constructor
A new instance of List.
- #serialize(obj) ⇒ Object
Methods included from Utils
#big_endian_to_int, #bytes_to_str, #encode_hex, #int_to_big_endian, #list?, #primitive?, #str_to_bytes
Constructor Details
#initialize(elements: [], strict: true) ⇒ List
Returns a new instance of List.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/rlp/sedes/list.rb', line 10 def initialize(elements: [], strict: true) super() @strict = strict elements.each do |e| if Sedes.sedes?(e) push e elsif list?(e) push List.new(elements: e) else raise TypeError, "Instances of List must only contain sedes objects or nested sequences thereof." end end end |
Instance Method Details
#deserialize(serial) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rlp/sedes/list.rb', line 42 def deserialize(serial) raise ListDeserializationError.new(message: 'Can only deserialize sequences', serial: serial) unless list?(serial) raise ListDeserializationError.new(message: 'List has wrong length', serial: serial) if @strict && serial.size != self.size result = [] len = [serial.size, self.size].min len.times do |i| begin sedes = self[i] element = serial[i] result.push sedes.deserialize(element) rescue DeserializationError => e raise ListDeserializationError.new(serial: serial, element_exception: e, index: i) end end result.freeze end |
#serialize(obj) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/rlp/sedes/list.rb', line 26 def serialize(obj) raise ListSerializationError.new(message: "Can only serialize sequences", obj: obj) unless list?(obj) raise ListSerializationError.new(message: "List has wrong length", obj: obj) if (@strict && self.size != obj.size) || self.size < obj.size result = [] obj.zip(self).each_with_index do |(element, sedes), i| begin result.push sedes.serialize(element) rescue SerializationError => e raise ListSerializationError.new(obj: obj, element_exception: e, index: i) end end result end |