Class: BinaryCodec::SerializedType

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

Direct Known Subclasses

Amount, Blob, ComparableSerializedType, STObject

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bytes = nil) ⇒ SerializedType

Returns a new instance of SerializedType.

Raises:

  • (NotImplementedError)


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

def initialize(bytes = nil)
  raise NotImplementedError, "#{self.class} is an abstract class and cannot be instantiated"
end

Instance Attribute Details

#bytesObject (readonly)

Returns the value of attribute bytes.



8
9
10
# File 'lib/binary-codec/types/serialized_type.rb', line 8

def bytes
  @bytes
end

Class Method Details

.from(value) ⇒ Object

Raises:

  • (NotImplementedError)


14
15
16
# File 'lib/binary-codec/types/serialized_type.rb', line 14

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

.from_bytes(bytes) ⇒ Object

Check if this is needed



33
34
35
# File 'lib/binary-codec/types/serialized_type.rb', line 33

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

.from_hex(hex) ⇒ Object

Check if this is needed



28
29
30
# File 'lib/binary-codec/types/serialized_type.rb', line 28

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

.from_json(json) ⇒ Object

Check if this is needed

Raises:

  • (NotImplementedError)


23
24
25
# File 'lib/binary-codec/types/serialized_type.rb', line 23

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

.from_parser(parser, size_hint = nil) ⇒ Object

Raises:

  • (NotImplementedError)


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

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

.get_type_by_name(name) ⇒ Object



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

def self.get_type_by_name(name)
  type_map = {
    "AccountID" => AccountId,
    "Amount" => Amount,
    "Blob" => Blob,
    "Currency" => Currency,
    "Hash128" => Hash128,
    "Hash160" => Hash160,
    "Hash256" => Hash256,
    #"PathSet" => PathSet,
    #"STArray" => STArray,
    "STObject" => STObject,
    "UInt8" => Uint8,
    "UInt16" => Uint16,
    "UInt32" => Uint32,
    "UInt64" => Uint64,
    #"Vector256" => Vector256
  }

  unless type_map.key?(name)
    raise "unsupported type #{name}"
  end

  # Return class
  type_map[name]
end

Instance Method Details

#to_byte_sink(sink) ⇒ Object



37
38
39
# File 'lib/binary-codec/types/serialized_type.rb', line 37

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

Returns:

  • (Array<Integer>)
    • Byte array representing the serialized data



44
45
46
# File 'lib/binary-codec/types/serialized_type.rb', line 44

def to_bytes
  @bytes
end

#to_hexString

Convert to a hex string

Returns:

  • (String)
    • Hexadecimal representation of the serialized data



50
51
52
# File 'lib/binary-codec/types/serialized_type.rb', line 50

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

Parameters:

  • _definitions (Hash) (defaults to: nil)
    • Definitions for serialization

  • _field_name (String) (defaults to: nil)
    • Field name for serialization

Returns:

  • (String)
    • JSON representation of the serialized data



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

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

#to_sObject

Represent object as a string (hexadecimal form)



64
65
66
# File 'lib/binary-codec/types/serialized_type.rb', line 64

def to_s
  to_hex
end