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
This commit is contained in:
VetheonGames 2023-10-14 00:54:03 -06:00
parent 315ca0c503
commit 19348e2aee
4 changed files with 30 additions and 8 deletions

View File

@ -137,11 +137,10 @@ module ConfigMan
def self.load def self.load
config_file_path = File.join(Dir.pwd, '.config') config_file_path = File.join(Dir.pwd, '.config')
parsed_config = send_to_parser(config_file_path) 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| parsed_config.each do |module_name, config|
define_singleton_method(key) do define_singleton_method(module_name) do
value config
end end
end end
end end

View File

@ -5,14 +5,19 @@
module ConfigMan module ConfigMan
module Utils module Utils
def self.sort_into_sections(config_hash, expected_keys, loaded_modules) 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] = {} } sorted_config = Hash.new { |hash, key| hash[key] = {} }
config_hash.each do |key, value| config_hash.each do |key, value|
section_found = false section_found = false
expected_keys.each do |section, keys| 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) next unless keys.include?(key)
sorted_config[section][key] = value sorted_config[section][key] = value
@ -20,6 +25,7 @@ module ConfigMan
break break
end 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 sorted_config['General'][key] = value unless section_found
end end

View File

@ -34,13 +34,16 @@ module ConfigMan
# Access the loaded modules and expected keys from the main class # Access the loaded modules and expected keys from the main class
loaded_modules = ConfigMan.used_modules loaded_modules = ConfigMan.used_modules
expected_keys = ConfigMan.expected_keys 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 # Sort the keys into their respective sections
sorted_config = Utils.sort_into_sections(config_hash, expected_keys, loaded_modules) sorted_config = Utils.sort_into_sections(config_hash, expected_keys, loaded_modules)
# Debug statement to output the sorted_config # Debug statement to output the sorted_config
puts 'Debug: About to write the following sorted_config to INI file:' #puts 'Debug: About to write the following sorted_config to INI file:'
p sorted_config #p sorted_config
# Write the sorted config to the INI file # Write the sorted config to the INI file
ini = IniFile.new(content: sorted_config) ini = IniFile.new(content: sorted_config)

View File

@ -9,6 +9,7 @@ module ConfigMan
module XML module XML
CONFIG_FILE_PATH = File.join(Dir.pwd, '.config').freeze 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 # Parse the .config file and return a hash of the configuration values
def self.parse(file_path) def self.parse(file_path)
raise ArgumentError, "File not found: #{file_path}" unless File.exist?(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) document = REXML::Document.new(xml_file)
parsed_config = {} 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| document.elements.each('config/*') do |element|
parsed_config[element.name] = element.text parsed_config[element.name] = parse_element.call(element)
end end
raise ArgumentError, "Invalid XML format in #{file_path}" unless parsed_config.is_a?(Hash) raise ArgumentError, "Invalid XML format in #{file_path}" unless parsed_config.is_a?(Hash)