Class: BinaryCodec::PathSet

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

Constant Summary collapse

PATH_STEP_END_MARKER =
0xFF
PATH_END_MARKER =
0xFE
TYPE_ACCOUNT =
0x01
TYPE_CURRENCY =
0x10
TYPE_ISSUER =
0x20

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) ⇒ PathSet



12
13
14
# File 'lib/binary-codec/types/path_set.rb', line 12

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

Class Method Details

.from(value) ⇒ Object

Raises:

  • (StandardError)


16
17
18
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/path_set.rb', line 16

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

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

  if value.is_a?(Array)
    bytes = []
    value.each_with_index do |path, index|
      path.each do |step|
        type = 0
        step_bytes = []

        if step['account']
          type |= 
          step_bytes.concat(AccountId.from(step['account']).to_bytes)
        end
        if step['currency']
          type |= TYPE_CURRENCY
          step_bytes.concat(Currency.from(step['currency']).to_bytes)
        end
        if step['issuer']
          type |= TYPE_ISSUER
          step_bytes.concat(AccountId.from(step['issuer']).to_bytes)
        end

        bytes << type
        bytes.concat(step_bytes)
      end
      bytes << (index == value.length - 1 ? PATH_END_MARKER : PATH_STEP_END_MARKER)
    end
    return PathSet.new(bytes)
  end

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

.from_parser(parser, _hint = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/binary-codec/types/path_set.rb', line 54

def self.from_parser(parser, _hint = nil)
  bytes = []
  loop do
    type = parser.read_uint8
    bytes << type
    break if type == PATH_END_MARKER

    if type != PATH_STEP_END_MARKER
      bytes.concat(parser.read(20)) if (type & ) != 0
      bytes.concat(parser.read(20)) if (type & TYPE_CURRENCY) != 0
      bytes.concat(parser.read(20)) if (type & TYPE_ISSUER) != 0
    end
  end
  PathSet.new(bytes)
end

Instance Method Details

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/binary-codec/types/path_set.rb', line 70

def to_json(_definitions = nil, _field_name = nil)
  parser = BinaryParser.new(to_hex)
  paths = []
  current_path = []

  until parser.end?
    type = parser.read_uint8
    if type == PATH_END_MARKER || type == PATH_STEP_END_MARKER
      paths << current_path
      current_path = []
      break if type == PATH_END_MARKER
      next
    end

    step = {}
    step['account'] = AccountId.from_parser(parser).to_json if (type & ) != 0
    step['currency'] = Currency.from_parser(parser).to_json if (type & TYPE_CURRENCY) != 0
    step['issuer'] = AccountId.from_parser(parser).to_json if (type & TYPE_ISSUER) != 0
    current_path << step
  end
  paths
end