Class: BinaryCodec::SerializedType
- Inherits:
-
Object
- Object
- BinaryCodec::SerializedType
- Defined in:
- lib/binary-codec/types/serialized_type.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#bytes ⇒ Object
readonly
Returns the value of attribute bytes.
Class Method Summary collapse
- .from(value) ⇒ Object
-
.from_bytes(bytes) ⇒ Object
Check if this is needed.
-
.from_hex(hex) ⇒ Object
Check if this is needed.
-
.from_json(json) ⇒ Object
Check if this is needed.
- .from_parser(parser, size_hint = nil) ⇒ Object
- .get_type_by_name(name) ⇒ Object
Instance Method Summary collapse
-
#initialize(bytes = nil) ⇒ SerializedType
constructor
A new instance of SerializedType.
- #to_byte_sink(sink) ⇒ Object
-
#to_bytes ⇒ Array<Integer>
Serialize the given value into bytes This method must be implemented in the subclasses.
-
#to_hex ⇒ String
Convert to a hex string.
-
#to_json(_definitions = nil, _field_name = nil) ⇒ String
Deserialize instance data and convert it to JSON string.
-
#to_s ⇒ Object
Represent object as a string (hexadecimal form).
Constructor Details
#initialize(bytes = nil) ⇒ SerializedType
Returns a new instance of SerializedType.
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
#bytes ⇒ Object (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
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
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
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_bytes ⇒ Array<Integer>
Serialize the given value into bytes This method must be implemented in the subclasses
44 45 46 |
# File 'lib/binary-codec/types/serialized_type.rb', line 44 def to_bytes @bytes end |
#to_hex ⇒ String
Convert to a hex string
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
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_s ⇒ Object
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 |