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:
parent
315ca0c503
commit
19348e2aee
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user