Module: ConfigMan

Defined in:
lib/configman.rb,
lib/configman/version.rb,
lib/configman/modules/api.rb,
lib/configman/parsers/ini.rb,
lib/configman/parsers/xml.rb,
lib/configman/parsers/json.rb,
lib/configman/parsers/yaml.rb,
lib/configman/modules/cache.rb,
lib/configman/modules/email.rb,
lib/configman/modules/utils.rb,
lib/configman/modules/logging.rb,
lib/configman/modules/database.rb,
lib/configman/modules/filestorage.rb,
lib/configman/modules/localization.rb

Overview

config_util.rb

Defined Under Namespace

Modules: Modules, Parsers, Utils Classes: Error

Constant Summary collapse

EXPECTED_KEYS =
{
  'API' => %w[api_endpoint api_key rate_limit],
  'Cache' => %w[cache_type host port],
  'Database' => %w[db_host db_port db_user db_password db_name],
  'Email' => %w[smtp_server smtp_port smtp_user smtp_password smtp_protocol],
  'FileStorage' => %w[storage_type cloud_provider local_path],
  'Localization' => %w[language time_zone encoding],
  'Logging' => %w[log_level log_file log_rotation]
}.freeze
VERSION =
'1.0.0'

Class Method Summary collapse

Class Method Details

.cleanup_unused_modulesObject

Cleanup unused modules



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/configman.rb', line 124

def self.cleanup_unused_modules
  # List of all available modules
  all_modules = %w[database logging api ini yaml]

  # Calculate modules to remove
  remove_modules = all_modules - @used_modules

  # Remove unnecessary modules
  remove_modules.each do |mod|
    module_path = File.join(__dir__, 'modules', "#{mod}.rb")
    File.delete(module_path) if File.exist?(module_path)
  end
end

.expected_keysObject



27
28
29
# File 'lib/configman.rb', line 27

def self.expected_keys
  EXPECTED_KEYS
end

.fetch(key) ⇒ Object



162
163
164
# File 'lib/configman.rb', line 162

def self.fetch(key)
  @config_values[key] || raise(%(Configuration key "#{key}" not found.))
end

.generate_config_file(final_config) ⇒ Object

Generate a .config file in the working directory



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/configman.rb', line 83

def self.generate_config_file(final_config)
  File.open('.config', 'w') do |file|
    case @loaded_parser
    when 'json'
      file.write(JSON.pretty_generate(final_config))
    when 'yaml'
      require 'yaml'
      file.write(final_config.to_yaml)
    when 'xml'
      require 'rexml/document'
      xml = REXML::Document.new
      xml.add_element('config', final_config)
      formatter = REXML::Formatters::Pretty.new
      formatter.write(xml, file)
    when 'ini'
      require 'inifile'
      ini = IniFile.new
      ini['default'] = final_config
      file.write(ini.to_s)
    else
      file.write(JSON.pretty_generate(final_config)) # Default to JSON
    end
  end
end

.loadObject

Load configurations from the .config file



139
140
141
142
143
144
145
146
147
148
# File 'lib/configman.rb', line 139

def self.load
  config_file_path = File.join(Dir.pwd, '.config')
  parsed_config = send_to_parser(config_file_path)

  parsed_config.each do |module_name, config|
    define_singleton_method(module_name) do
      config
    end
  end
end

.load_modules(module_names) ⇒ Object

Lazy loading of modules and parsers



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/configman.rb', line 109

def self.load_modules(module_names)
  module_names.each do |module_name|
    if %w[json ini xml yaml].include?(module_name.downcase)
      # It's a parser
      require_relative "configman/parsers/#{module_name.downcase}"
      @loaded_parser = module_name.downcase
    else
      # It's a regular module
      require_relative "configman/modules/#{module_name.downcase}"
    end
    @used_modules << module_name.downcase
  end
end

.method_missing(method_name, *args, &block) ⇒ Object

Handle undefined methods to fetch config values



167
168
169
170
171
172
173
# File 'lib/configman.rb', line 167

def self.method_missing(method_name, *args, &block)
  if @config_values.key?(method_name)
    @config_values[method_name]
  else
    super
  end
end

.register_module(file_path) ⇒ Object

register any custom modules the user provides

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/configman.rb', line 32

def self.register_module(file_path)
  raise ArgumentError, "File not found: #{file_path}" unless File.exist?(file_path)

  require file_path

  module_name = File.basename(file_path, '.rb').capitalize
  mod_class = Object.const_get(module_name)

  unless mod_class.respond_to?(:populate_defaults)
    raise ArgumentError, "Custom module must implement a 'populate_defaults' method"
  end

  @custom_modules ||= []
  @custom_modules << mod_class
  puts "Custom modules: #{@custom_modules.inspect}"  # Debug line
end

.respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Respond to missing methods

Returns:

  • (Boolean)


176
177
178
# File 'lib/configman.rb', line 176

def self.respond_to_missing?(method_name, include_private = false)
  @config_values.key?(method_name) || super
end

.send_to_parser(file_path) ⇒ Object

Delegate to the appropriate parser to parse the file

Raises:

  • (ArgumentError)


151
152
153
154
155
156
157
158
159
160
# File 'lib/configman.rb', line 151

def self.send_to_parser(file_path)
  raise ArgumentError, 'No parser loaded' unless @loaded_parser

  parser_module = Object.const_get("ConfigMan::Parsers::#{@loaded_parser.upcase}")

  raise ArgumentError, "Invalid parser: #{@loaded_parser}" unless parser_module.respond_to?(:parse)

  parsed_config = parser_module.parse(file_path)
  @config_values.merge!(parsed_config)
end

.setup(default_modules, custom_options = {}) ⇒ Object

Setup ConfigMan with presets



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/configman.rb', line 54

def self.setup(default_modules, custom_options = {})
  # Check if a parser is loaded
  raise 'No parser module loaded. Please load a parser module before calling setup.' if @loaded_parser.nil?

  final_config = {}

  # Remove parser names from default_modules
  default_modules = default_modules.reject { |mod| %w[json ini xml yaml].include?(mod.downcase) }

  # Populate defaults from built-in modules
  default_modules.each do |mod|
    mod_class = Object.const_get("ConfigMan::Modules::#{mod}")
    final_config.merge!(mod_class.populate_defaults)
  end

  # Populate defaults from custom modules
  @custom_modules.each do |mod_class|
    final_config.merge!(mod_class.populate_defaults)
  end

  # Add custom options
  final_config.merge!(custom_options) unless custom_options.empty?

  # Write to the config file using the appropriate parser
  parser_module = Object.const_get("ConfigMan::Parsers::#{@loaded_parser.upcase}")
  parser_module.write(final_config)
end

.used_modulesObject



49
50
51
# File 'lib/configman.rb', line 49

def self.used_modules
  @used_modules
end