Class: BinaryCodec::Vector256

Inherits:
SerializedType show all
Defined in:
lib/binary-codec/types/vector256.rb

Instance Attribute Summary

Attributes inherited from SerializedType

#bytes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SerializedType

from_bytes, from_hex, from_json, get_type_by_name, #to_byte_sink, #to_bytes, #to_hex, #value_of

Constructor Details

#initialize(bytes = nil) ⇒ Vector256



5
6
7
# File 'lib/binary-codec/types/vector256.rb', line 5

def initialize(bytes = nil)
  super(bytes || [])
end

Class Method Details

.from(value) ⇒ Vector256

Creates a new Vector256 instance from a value.

Raises:

  • (StandardError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/binary-codec/types/vector256.rb', line 12

def self.from(value)
  return value if value.is_a?(Vector256)

  if value.is_a?(String)
    return Vector256.new(hex_to_bytes(value))
  end

  if value.is_a?(Array)
    bytes = []
    value.each do |item|
      hash = Hash256.from(item)
      bytes.concat(hash.to_bytes)
    end
    return Vector256.new(bytes)
  end

  raise StandardError, "Cannot construct Vector256 from #{value.class}"
end

.from_parser(parser, size_hint = nil) ⇒ Vector256

Creates a Vector256 instance from a parser.



35
36
37
38
39
40
41
42
# File 'lib/binary-codec/types/vector256.rb', line 35

def self.from_parser(parser, size_hint = nil)
  bytes = []
  num_hashes = size_hint / 32
  num_hashes.times do
    bytes.concat(parser.read(32))
  end
  Vector256.new(bytes)
end

Instance Method Details

#to_json(_definitions = nil, _field_name = nil) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/binary-codec/types/vector256.rb', line 44

def to_json(_definitions = nil, _field_name = nil)
  parser = BinaryParser.new(to_hex)
  result = []
  until parser.end?
    result << bytes_to_hex(parser.read(32))
  end
  result
end