Class: BinaryCodec::Definitions
- Inherits:
-
Object
- Object
- BinaryCodec::Definitions
- Defined in:
- lib/binary-codec/enums/definitions.rb
Constant Summary collapse
- @@instance =
nil
Instance Attribute Summary collapse
-
#definitions ⇒ Hash
(also: #raw)
readonly
The parsed definitions.json, for the sections this class does not index itself - TRANSACTION_FORMATS and TRANSACTION_FLAGS, which the transaction models are built from.
Class Method Summary collapse
-
.instance ⇒ Definitions
Returns the singleton instance of the Definitions class.
Instance Method Summary collapse
-
#get_field_header_from_name(field_name) ⇒ FieldHeader
Returns the field header for a given field name.
-
#get_field_instance(field_name) ⇒ FieldInstance
Returns a FieldInstance for a given field name.
-
#get_field_name_from_header(field_header) ⇒ String
Returns the field name for a given field header.
-
#initialize ⇒ Definitions
constructor
A new instance of Definitions.
Constructor Details
#initialize ⇒ Definitions
Returns a new instance of Definitions.
11 12 13 14 15 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 |
# File 'lib/binary-codec/enums/definitions.rb', line 11 def initialize file_path = File.join(__dir__, 'definitions.json') # contents = File.read(file_path) @definitions = JSON.parse(contents) @type_ordinals = @definitions['TYPES'] @ledger_entry_types = @definitions['LEDGER_ENTRY_TYPES'] @transaction_results = @definitions['TRANSACTION_RESULTS'] @transaction_types = @definitions['TRANSACTION_TYPES'] @field_info_map = {} @field_id_name_map = {} @field_header_map = {} @definitions['FIELDS'].each do |field| field_name = field[0] field_info = FieldInfo.new( nth: field[1]['nth'], is_vl_encoded: field[1]['isVLEncoded'], is_serialized: field[1]['isSerialized'], is_signing_field: field[1]['isSigningField'], type: field[1]['type'] ) type_ordinal = @type_ordinals[field_info.type] field_header = FieldHeader.new(type: type_ordinal, nth: field_info.nth) @field_info_map[field_name] = field_info key = (type_ordinal << 16) | field_info.nth @field_id_name_map[key] = field_name @field_header_map[field_name] = field_header end rescue Errno::ENOENT raise "Error: The file '#{file_path}' was not found. Please ensure the file exists." rescue JSON::ParserError => e raise "Error: The file '#{file_path}' contains invalid JSON: #{e.}" end |
Instance Attribute Details
#definitions ⇒ Hash (readonly) Also known as: raw
The parsed definitions.json, for the sections this class does not index itself - TRANSACTION_FORMATS and TRANSACTION_FLAGS, which the transaction models are built from.
55 56 57 |
# File 'lib/binary-codec/enums/definitions.rb', line 55 def definitions @definitions end |
Class Method Details
.instance ⇒ Definitions
Returns the singleton instance of the Definitions class.
60 61 62 |
# File 'lib/binary-codec/enums/definitions.rb', line 60 def self.instance @@instance ||= new end |
Instance Method Details
#get_field_header_from_name(field_name) ⇒ FieldHeader
Returns the field header for a given field name.
67 68 69 |
# File 'lib/binary-codec/enums/definitions.rb', line 67 def get_field_header_from_name(field_name) @field_header_map[field_name] end |
#get_field_instance(field_name) ⇒ FieldInstance
Returns a FieldInstance for a given field name.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/binary-codec/enums/definitions.rb', line 81 def get_field_instance(field_name) field_info = @field_info_map[field_name] field_header = get_field_header_from_name(field_name) FieldInstance.new( nth: field_info.nth, is_variable_length_encoded: field_info.is_vl_encoded, is_serialized: field_info.is_serialized, is_signing_field: field_info.is_signing_field, type: field_info.type, ordinal: (@type_ordinals[field_info.type] << 16) | field_info.nth, name: field_name, header: field_header, associated_type: SerializedType.get_type_by_name(field_info.type) ) end |
#get_field_name_from_header(field_header) ⇒ String
Returns the field name for a given field header.
74 75 76 |
# File 'lib/binary-codec/enums/definitions.rb', line 74 def get_field_name_from_header(field_header) @field_id_name_map[(field_header.type << 16) | field_header.nth] end |