Class: BinaryCodec::Uint
- Inherits:
-
ComparableSerializedType
- Object
- SerializedType
- ComparableSerializedType
- BinaryCodec::Uint
- Defined in:
- lib/binary-codec/types/uint.rb
Direct Known Subclasses
Int32, Int64, Uint128, Uint16, Uint160, Uint192, Uint256, Uint32, Uint384, Uint512, Uint64, Uint8, Uint96
Instance Attribute Summary
Attributes inherited from SerializedType
Class Method Summary collapse
-
.from(value) ⇒ Uint
Creates a new Uint instance from a value.
-
.from_parser(parser, _hint = nil) ⇒ Uint
Creates a Uint instance from a parser.
-
.width ⇒ Integer
Returns the width of the Uint type in bytes.
Instance Method Summary collapse
-
#compare_to(other) ⇒ Integer
Comparison result (-1, 0, or 1).
-
#initialize(byte_buf = nil) ⇒ Uint
constructor
A new instance of Uint.
-
#to_json(_definitions = nil, _field_name = nil) ⇒ Integer, String
Returns the JSON representation of the Uint.
-
#value_of ⇒ Integer
Returns the numeric value of the Uint.
Methods inherited from ComparableSerializedType
Methods inherited from SerializedType
from_bytes, from_hex, from_json, get_type_by_name, #to_byte_sink, #to_bytes, #to_hex
Constructor Details
#initialize(byte_buf = nil) ⇒ Uint
Returns a new instance of Uint.
12 13 14 |
# File 'lib/binary-codec/types/uint.rb', line 12 def initialize(byte_buf = nil) super(byte_buf || Array.new(self.class.width, 0)) end |
Class Method Details
.from(value) ⇒ Uint
Creates a new Uint instance from a value.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/binary-codec/types/uint.rb', line 19 def self.from(value) return value if value.is_a?(self) if value.is_a?(String) # Special handling for TransactionType and LedgerEntryType if self == Uint16 transaction_types = Definitions.instance.instance_variable_get(:@transaction_types) if transaction_types&.key?(value) return new(int_to_bytes(transaction_types[value], width)) end ledger_entry_types = Definitions.instance.instance_variable_get(:@ledger_entry_types) if ledger_entry_types&.key?(value) return new(int_to_bytes(ledger_entry_types[value], width)) end elsif self == Uint8 transaction_results = Definitions.instance.instance_variable_get(:@transaction_results) if transaction_results&.key?(value) return new(int_to_bytes(transaction_results[value], width)) end end # Handle hex strings or numeric strings if valid_hex?(value) && value.length == self.width * 2 return new(hex_to_bytes(value)) end return new(int_to_bytes(value.to_i, width)) end if value.is_a?(Integer) return new(int_to_bytes(value, width)) end raise StandardError, "Cannot construct #{self} from the value given" end |
.from_parser(parser, _hint = nil) ⇒ Uint
Creates a Uint instance from a parser.
58 59 60 |
# File 'lib/binary-codec/types/uint.rb', line 58 def self.from_parser(parser, _hint = nil) new(parser.read(width)) end |
.width ⇒ Integer
Returns the width of the Uint type in bytes.
8 9 10 |
# File 'lib/binary-codec/types/uint.rb', line 8 def self.width @width end |
Instance Method Details
#compare_to(other) ⇒ Integer
Returns Comparison result (-1, 0, or 1).
104 105 106 |
# File 'lib/binary-codec/types/uint.rb', line 104 def compare_to(other) value_of <=> other.value_of end |
#to_json(_definitions = nil, _field_name = nil) ⇒ Integer, String
Returns the JSON representation of the Uint.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/binary-codec/types/uint.rb', line 70 def to_json(_definitions = nil, _field_name = nil) # Special handling for TransactionType, LedgerEntryType, and TransactionResult # ONLY when requested via a field name that matches. if _field_name == 'TransactionType' val = value_of transaction_types = Definitions.instance.instance_variable_get(:@transaction_types) if transaction_types name = transaction_types.key(val) return name if name end elsif _field_name == 'LedgerEntryType' val = value_of ledger_entry_types = Definitions.instance.instance_variable_get(:@ledger_entry_types) if ledger_entry_types name = ledger_entry_types.key(val) return name if name end elsif _field_name == 'TransactionResult' val = value_of transaction_results = Definitions.instance.instance_variable_get(:@transaction_results) if transaction_results name = transaction_results.key(val) return name if name end end # For Uint8/16/32/64 and Int32/64 we return padded hex, to satisfy existing Ruby tests. # We use unsigned value for hex representation of signed types. u_val = value_of u_val += (1 << (self.class.width * 8)) if u_val < 0 return u_val.to_s(16).upcase.rjust(self.class.width * 2, '0') end |
#value_of ⇒ Integer
Returns the numeric value of the Uint.
64 65 66 |
# File 'lib/binary-codec/types/uint.rb', line 64 def value_of @bytes.reduce(0) { |acc, byte| (acc << 8) + byte } end |