Class: Etherlite::Types::ArrayFixed

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#dynamic?, #fixed?

Constructor Details

#initialize(_subtype, _length) ⇒ ArrayFixed

Returns a new instance of ArrayFixed.

Raises:

  • (ArgumentError)


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

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

  @subtype = _subtype
  @length = _length
end

Instance Attribute Details

#lengthObject (readonly)

Returns the value of attribute length.



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

def length
  @length
end

#subtypeObject (readonly)

Returns the value of attribute subtype.



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

def subtype
  @subtype
end

Instance Method Details

#decode(_connection, _data) ⇒ Object



28
29
30
# File 'lib/etherlite/types/array_fixed.rb', line 28

def decode(_connection, _data)
  Etherlite::Support::Array.decode(_connection, [@subtype] * @length, _data)
end

#encode(_values) ⇒ Object

Raises:

  • (ArgumentError)


21
22
23
24
25
26
# File 'lib/etherlite/types/array_fixed.rb', line 21

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

  Etherlite::Support::Array.encode([@subtype] * @length, _values)
end

#signatureObject



12
13
14
# File 'lib/etherlite/types/array_fixed.rb', line 12

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

#sizeObject



16
17
18
19
# File 'lib/etherlite/types/array_fixed.rb', line 16

def size
  return nil if @subtype.dynamic?
  @subtype.size * @length
end