Class: BinaryCodec::Definitions
- Inherits:
-
Object
- Object
- BinaryCodec::Definitions
- Defined in:
- lib/binary-codec/enums/definitions.rb
Constant Summary collapse
- @@instance =
nil
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 |
# 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'] ) field_header = FieldHeader.new(type: @type_ordinals[field_info.type], nth: field_info.nth) @field_info_map[field_name] = field_info @field_id_name_map[Digest::MD5.hexdigest(Marshal.dump(field_header))] = 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.message}" end |
Class Method Details
.instance ⇒ Definitions
Returns the singleton instance of the Definitions class.
50 51 52 |
# File 'lib/binary-codec/enums/definitions.rb', line 50 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.
57 58 59 |
# File 'lib/binary-codec/enums/definitions.rb', line 57 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.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/binary-codec/enums/definitions.rb', line 71 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.
64 65 66 |
# File 'lib/binary-codec/enums/definitions.rb', line 64 def get_field_name_from_header(field_header) @field_id_name_map[Digest::MD5.hexdigest(Marshal.dump(field_header))] end |