Class: BinaryCodec::SerializedType

Inherits:
Object
  • Object
show all
Defined in:
lib/binary-codec/types/serialized_type.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bytes = nil) ⇒ SerializedType

Initializes a new SerializedType instance.



10
11
12
# File 'lib/binary-codec/types/serialized_type.rb', line 10

def initialize(bytes = nil)
  @bytes = bytes
end

Instance Attribute Details

#bytesObject (readonly)

Returns the value of attribute bytes.



6
7
8
# File 'lib/binary-codec/types/serialized_type.rb', line 6

def bytes
  @bytes
end

Class Method Details

.from(value) ⇒ SerializedType

Creates a new instance of the type from a value.

Raises:

  • (NotImplementedError)


17
18
19
# File 'lib/binary-codec/types/serialized_type.rb', line 17

def self.from(value)
  raise NotImplementedError, 'from not implemented'
end

.from_bytes(bytes) ⇒ Object

Check if this is needed



40
41
42
# File 'lib/binary-codec/types/serialized_type.rb', line 40

def self.from_bytes(bytes)
  new(bytes)
end

.from_hex(hex) ⇒ Object

Check if this is needed



35
36
37
# File 'lib/binary-codec/types/serialized_type.rb', line 35

def self.from_hex(hex)
  self.from_bytes(hex_to_bytes(hex))
end

.from_json(json) ⇒ Object

Check if this is needed

Raises:

  • (NotImplementedError)


30
31
32
# File 'lib/binary-codec/types/serialized_type.rb', line 30

def self.from_json(json)
  raise NotImplementedError, 'from_parser not implemented'
end

.from_parser(parser, size_hint = nil) ⇒ SerializedType

Creates an instance of the type from a parser.

Raises:

  • (NotImplementedError)


25
26
27
# File 'lib/binary-codec/types/serialized_type.rb', line 25

def self.from_parser(parser, size_hint = nil)
  raise NotImplementedError, 'from_parser not implemented'
end

.get_type_by_name(name) ⇒ Class

Returns the class for a given type name.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/binary-codec/types/serialized_type.rb', line 81

def self.get_type_by_name(name)
  case name
  when "AccountID" then AccountId
  when "Amount" then Amount
  when "Blob" then Blob
  when "Currency" then Currency
  when "Hash128" then Hash128
  when "Hash160" then Hash160
  when "Hash192" then Hash192
  when "Hash256" then Hash256
  when "STArray" then STArray
  when "STObject" then STObject
  when "UInt8" then Uint8
  when "UInt16" then Uint16
  when "UInt32" then Uint32
  when "UInt64" then Uint64
  when "UInt96" then Uint96
  when "UInt128" then Uint128
  when "UInt160" then Uint160
  when "UInt192" then Uint192
  when "UInt256" then Uint256
  when "UInt384" then Uint384
  when "UInt512" then Uint512
  when "Int32" then Int32
  when "Int64" then Int64
  when "PathSet" then PathSet
  when "Vector256" then Vector256
  when "XChainBridge" then XChainBridge
  when "Issue" then Issue
  when "Transaction" then Blob
  when "LedgerEntry" then Blob
  when "Validation" then Blob
  when "Metadata" then Blob
  else
    raise "unsupported type #{name}"
  end
end

Instance Method Details

#to_byte_sink(sink) ⇒ Object

Puts the serialized data into a byte sink.



46
47
48
# File 'lib/binary-codec/types/serialized_type.rb', line 46

def to_byte_sink(sink)
  sink.put(to_bytes)
end

#to_bytesArray<Integer>

Serialize the given value into bytes This method must be implemented in the subclasses



53
54
55
# File 'lib/binary-codec/types/serialized_type.rb', line 53

def to_bytes
  @bytes
end

#to_hexString

Convert to a hex string



59
60
61
# File 'lib/binary-codec/types/serialized_type.rb', line 59

def to_hex
  bytes_to_hex(to_bytes)
end

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

Deserialize instance data and convert it to JSON string



68
69
70
# File 'lib/binary-codec/types/serialized_type.rb', line 68

def to_json(_definitions = nil, _field_name = nil)
  to_hex
end

#value_ofObject

Returns the value of the serialized type



74
75
76
# File 'lib/binary-codec/types/serialized_type.rb', line 74

def value_of
  @bytes
end