Class: Web3::Eth::Abi::Tuple

Inherits:
Type
  • Object
show all
Defined in:
lib/web3/eth/abi/type.rb

Instance Attribute Summary collapse

Attributes inherited from Type

#base, #dims, #sub

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Type

#dynamic?, size_type

Constructor Details

#initialize(types, dims) ⇒ Tuple

Returns a new instance of Tuple.



159
160
161
162
163
# File 'lib/web3/eth/abi/type.rb', line 159

def initialize types, dims
  super('tuple', '', dims.map {|x| x[1...-1].to_i})
  @types = types
  @parsed_types = types.map{|t| Type.parse t}
end

Instance Attribute Details

#parsed_typesObject (readonly)

Returns the value of attribute parsed_types.



158
159
160
# File 'lib/web3/eth/abi/type.rb', line 158

def parsed_types
  @parsed_types
end

#typesObject (readonly)

Returns the value of attribute types.



158
159
160
# File 'lib/web3/eth/abi/type.rb', line 158

def types
  @types
end

Class Method Details

.parse(types, dims) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/web3/eth/abi/type.rb', line 126

def self.parse types, dims

  depth = 0
  collected = []
  current = ''

  types.split('').each do |c|
    case c
      when ',' then
        if depth==0
          collected << current
          current = ''
        else
          current += c
        end
      when '(' then
        depth += 1
        current += c
      when ')' then
        depth -= 1
        current += c
      else
        current += c
    end

  end
  collected << current unless current.empty?

  Tuple.new collected, dims

end

Instance Method Details

#==(another_type) ⇒ Object



165
166
167
168
169
# File 'lib/web3/eth/abi/type.rb', line 165

def ==(another_type)
  another_type.kind_of?(Tuple) &&
      another_type.types == types &&
      another_type.dims == dims
end

#calculate_sizeObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/web3/eth/abi/type.rb', line 175

def calculate_size
  if dims.empty?
    s = 0
    parsed_types.each do |type|
      ts = type.size
      return nil if ts.nil?
      s += ts
    end
    s
  else
    if dims.last == 0 # 0 for dynamic array []
      nil
    else
      subtype.dynamic? ? nil : dims.last * subtype.size
    end
  end


end

#sizeObject



171
172
173
# File 'lib/web3/eth/abi/type.rb', line 171

def size
  @size ||= calculate_size
end

#subtypeObject



195
196
197
# File 'lib/web3/eth/abi/type.rb', line 195

def subtype
  @subtype ||= Tuple.new(types, dims[0...-1])
end