Class: Etherlite::Types::ArrayDynamic

Inherits:
Base
  • Object
show all
Defined in:
lib/etherlite/types/array_dynamic.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#dynamic?, #fixed?, #size

Constructor Details

#initialize(_subtype) ⇒ ArrayDynamic

Returns a new instance of ArrayDynamic.

Raises:

  • (ArgumentError)


5
6
7
8
9
# File 'lib/etherlite/types/array_dynamic.rb', line 5

def initialize(_subtype)
  raise ArgumentError, 'An array can not contain a dynamic type' if _subtype.dynamic?

  @subtype = _subtype
end

Instance Attribute Details

#subtypeObject (readonly)

Returns the value of attribute subtype.



3
4
5
# File 'lib/etherlite/types/array_dynamic.rb', line 3

def subtype
  @subtype
end

Instance Method Details

#decode(_connection, _data) ⇒ Object



22
23
24
25
# File 'lib/etherlite/types/array_dynamic.rb', line 22

def decode(_connection, _data)
  length = Etherlite::Utils.hex_to_uint(_data[0..63])
  Etherlite::Support::Array.decode(_connection, [@subtype] * length, _data[64..-1])
end

#encode(_values) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
20
# File 'lib/etherlite/types/array_dynamic.rb', line 15

def encode(_values)
  raise ArgumentError, "expected an array for #{signature}" unless _values.is_a? Array

  encoded_array = Etherlite::Support::Array.encode([@subtype] * _values.length, _values)
  Etherlite::Utils.uint_to_hex(_values.length) + encoded_array
end

#signatureObject



11
12
13
# File 'lib/etherlite/types/array_dynamic.rb', line 11

def signature
  "#{@subtype.signature}[]"
end