Class: Etheruby::Encoders::StaticArray

Inherits:
Base
  • Object
show all
Defined in:
lib/etheruby/encoders/arrays.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#data

Instance Method Summary collapse

Constructor Details

#initialize(_type, _size, _data) ⇒ StaticArray

Returns a new instance of StaticArray.



11
12
13
14
15
# File 'lib/etheruby/encoders/arrays.rb', line 11

def initialize(_type, _size, _data)
  super(_data)
  @type = _type
  @size = _size
end

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



9
10
11
# File 'lib/etheruby/encoders/arrays.rb', line 9

def size
  @size
end

#typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/etheruby/encoders/arrays.rb', line 9

def type
  @type
end

Instance Method Details

#decodeObject



40
41
42
43
44
45
46
# File 'lib/etheruby/encoders/arrays.rb', line 40

def decode
  if Etheruby::is_static_type?(type)
    sub_decode(0, data)
  else
    sub_decode(size*32, data[size*2*32..data.length])
  end
end

#encodeObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/etheruby/encoders/arrays.rb', line 21

def encode
  if Etheruby::is_static_type? type
    data.map{ |d| Etheruby::treat_variable(type, d, :encode) }.join
  else
    head_x = ''
    enc_x = ''
    c_pos = size * 32
    data.map { |d|
      treated = Etheruby::treat_variable(type, d, :encode)
      {size: treated.length/2, value: treated}
    }.each { |item|
      head_x += Uint.new(c_pos).encode
      c_pos += item[:size]
      enc_x += item[:value]
    }
    head_x + enc_x
  end
end

#to_sObject



17
18
19
# File 'lib/etheruby/encoders/arrays.rb', line 17

def to_s
  encode
end