From 19348e2aeee5b82ac9ca4a9417c03650cf75b739 Mon Sep 17 00:00:00 2001 From: VetheonGames Date: Sat, 14 Oct 2023 00:54:03 -0600 Subject: [PATCH] Parsers/Interpreters and Modules Finished! Made some changes to the way self.load determines how to load nested strings Add some comments make some changes to string interpretation for self.sort_into_sections in the utils module add some, remove some other debug statements to ini parser Add a Recursive lambda to parse nested elements in the XML parser --- lib/configman.rb | 7 +++---- lib/configman/modules/utils.rb | 8 +++++++- lib/configman/parsers/ini.rb | 7 +++++-- lib/configman/parsers/xml.rb | 16 +++++++++++++++- 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/lib/configman.rb b/lib/configman.rb index ed2ea8a..c4ba1c9 100644 --- a/lib/configman.rb +++ b/lib/configman.rb @@ -137,11 +137,10 @@ module ConfigMan def self.load config_file_path = File.join(Dir.pwd, '.config') parsed_config = send_to_parser(config_file_path) - # Assuming send_to_parser delegates to the correct parser and returns a hash - parsed_config.each do |key, value| - define_singleton_method(key) do - value + parsed_config.each do |module_name, config| + define_singleton_method(module_name) do + config end end end diff --git a/lib/configman/modules/utils.rb b/lib/configman/modules/utils.rb index c3a7795..ce39c1f 100644 --- a/lib/configman/modules/utils.rb +++ b/lib/configman/modules/utils.rb @@ -5,14 +5,19 @@ module ConfigMan module Utils def self.sort_into_sections(config_hash, expected_keys, loaded_modules) + # Convert loaded_modules to uppercase for comparison + loaded_modules_upcase = loaded_modules.map(&:upcase) + sorted_config = Hash.new { |hash, key| hash[key] = {} } config_hash.each do |key, value| section_found = false expected_keys.each do |section, keys| - next unless loaded_modules.include?(section) + # Skip the section if it's not in the loaded modules + next unless loaded_modules_upcase.include?(section.upcase) + # Skip the key if it's not in the expected keys for this section next unless keys.include?(key) sorted_config[section][key] = value @@ -20,6 +25,7 @@ module ConfigMan break end + # If the key doesn't match any of the expected keys, put it in the 'General' section sorted_config['General'][key] = value unless section_found end diff --git a/lib/configman/parsers/ini.rb b/lib/configman/parsers/ini.rb index 1960cda..c43d32f 100644 --- a/lib/configman/parsers/ini.rb +++ b/lib/configman/parsers/ini.rb @@ -34,13 +34,16 @@ module ConfigMan # Access the loaded modules and expected keys from the main class loaded_modules = ConfigMan.used_modules expected_keys = ConfigMan.expected_keys + puts "Debug: About to pass these #{expected_keys} to the utils module" + puts "and these #{loaded_modules}" + puts "and this #{config_hash}" # Sort the keys into their respective sections sorted_config = Utils.sort_into_sections(config_hash, expected_keys, loaded_modules) # Debug statement to output the sorted_config - puts 'Debug: About to write the following sorted_config to INI file:' - p sorted_config + #puts 'Debug: About to write the following sorted_config to INI file:' + #p sorted_config # Write the sorted config to the INI file ini = IniFile.new(content: sorted_config) diff --git a/lib/configman/parsers/xml.rb b/lib/configman/parsers/xml.rb index 5637892..eb98114 100644 --- a/lib/configman/parsers/xml.rb +++ b/lib/configman/parsers/xml.rb @@ -9,6 +9,7 @@ module ConfigMan module XML CONFIG_FILE_PATH = File.join(Dir.pwd, '.config').freeze + # Parse the .config file and return a hash of the configuration values # Parse the .config file and return a hash of the configuration values def self.parse(file_path) raise ArgumentError, "File not found: #{file_path}" unless File.exist?(file_path) @@ -17,8 +18,21 @@ module ConfigMan document = REXML::Document.new(xml_file) parsed_config = {} + # Recursive lambda to parse nested elements + parse_element = lambda do |element| + if element.has_elements? + element_hash = {} + element.each_element do |child| + element_hash[child.name] = parse_element.call(child) + end + element_hash + else + element.text + end + end + document.elements.each('config/*') do |element| - parsed_config[element.name] = element.text + parsed_config[element.name] = parse_element.call(element) end raise ArgumentError, "Invalid XML format in #{file_path}" unless parsed_config.is_a?(Hash)