Class: Etherlite::Types::Integer

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

Instance Method Summary collapse

Methods inherited from Base

#dynamic?, #fixed?

Constructor Details

#initialize(_signed, _size) ⇒ Integer

Returns a new instance of Integer.



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

def initialize(_signed, _size)
  unless 0 < _size && _size <= 256 && _size % 8 == 0
    raise ArgumentError, "invalid integer size #{_size}"
  end

  @signed = _signed
  @size = _size
end

Instance Method Details

#decode(_connection, _value) ⇒ Object



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

def decode(_connection, _value)
  @signed ? Etherlite::Utils.hex_to_int(_value) : Etherlite::Utils.hex_to_uint(_value)
end

#encode(_value) ⇒ Object

Raises:

  • (ArgumentError)


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

def encode(_value)
  raise ArgumentError, "expected a number for #{signature}" unless _value.is_a? ::Integer
  raise ArgumentError, "expected a positive number for #{signature}" if !@signed && _value < 0
  raise ArgumentError, "value out of bounds #{_value}" if _value.abs > maximum

  @signed ? Etherlite::Utils.int_to_hex(_value) : Etherlite::Utils.uint_to_hex(_value)
end

#signatureObject



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

def signature
  "#{@signed ? 'int' : 'uint'}#{@size}"
end

#sizeObject



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

def size
  32
end