Sha256: 66d2fc7a42ed69e53e2eadb6db4336cc772ba695ef92001e4f7c11be6e78fade

Contents?: true

Size: 988 Bytes

Versions: 25

Compression:

Stored size: 988 Bytes

Contents

module Etherlite::Types
  class Integer < Base
    def initialize(_signed, _size)
      unless 0 < _size && _size <= 256 && _size % 8 == 0
        raise ArgumentError, "invalid integer size #{_size}"
      end

      @signed = _signed
      @size = _size
    end

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

    def size
      32
    end

    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

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

    private

    def maximum
      2**(@signed ? @size - 1 : @size)
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
etherlite-0.6.0 lib/etherlite/types/integer.rb
etherlite-0.5.3 lib/etherlite/types/integer.rb
etherlite-0.5.2 lib/etherlite/types/integer.rb
etherlite-0.5.1 lib/etherlite/types/integer.rb
etherlite-0.5.0 lib/etherlite/types/integer.rb
etherlite-0.4.2 lib/etherlite/types/integer.rb
etherlite-0.4.1 lib/etherlite/types/integer.rb
etherlite-0.4.0 lib/etherlite/types/integer.rb
etherlite-0.3.0 lib/etherlite/types/integer.rb
etherlite-0.2.6 lib/etherlite/types/integer.rb
etherlite-0.2.5 lib/etherlite/types/integer.rb
etherlite-0.2.4 lib/etherlite/types/integer.rb
etherlite-0.2.3 lib/etherlite/types/integer.rb
etherlite-0.2.2 lib/etherlite/types/integer.rb
etherlite-0.2.1 lib/etherlite/types/integer.rb
etherlite-0.2.0 lib/etherlite/types/integer.rb
etherlite-0.1.8 lib/etherlite/types/integer.rb
etherlite-0.1.7 lib/etherlite/types/integer.rb
etherlite-0.1.6 lib/etherlite/types/integer.rb
etherlite-0.1.5 lib/etherlite/types/integer.rb