Class: BinaryCodec::Definitions

Inherits:
Object
  • Object
show all
Defined in:
lib/binary-codec/enums/definitions.rb

Constant Summary collapse

@@instance =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDefinitions

Returns a new instance of Definitions.



10
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
# File 'lib/binary-codec/enums/definitions.rb', line 10

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

.instanceObject



47
48
49
# File 'lib/binary-codec/enums/definitions.rb', line 47

def self.instance
  @@instance ||= new
end

Instance Method Details

#get_field_header_from_name(field_name) ⇒ Object



51
52
53
# File 'lib/binary-codec/enums/definitions.rb', line 51

def get_field_header_from_name(field_name)
  @field_header_map[field_name]
end

#get_field_instance(field_name) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/binary-codec/enums/definitions.rb', line 59

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: (field_header.type << 16) | field_info.nth, # @type_ordinals[field_info.type],
    name: field_name,
    header: field_header,
    associated_type: field_info.type # SerializedType::getTypeByName($this->type)::class;
  )
end

#get_field_name_from_header(field_header) ⇒ Object



55
56
57
# File 'lib/binary-codec/enums/definitions.rb', line 55

def get_field_name_from_header(field_header)
  @field_id_name_map[Digest::MD5.hexdigest(Marshal.dump(field_header))]
end