Class: Appydave::Tools::Configuration::Config
- Inherits:
-
Object
- Object
- Appydave::Tools::Configuration::Config
- Extended by:
- KLog::Logging
- Defined in:
- lib/appydave/tools/configuration/config.rb
Overview
Configuration instances are created once on first registration and reused for all subsequent accesses. This prevents unnecessary file I/O and ensures consistent state across the application.
Central configuration management for appydave-tools
Thread-safe singleton pattern with memoization for registered configurations. Calling ‘Config.configure` multiple times is safe and idempotent.
Class Attribute Summary collapse
-
.config_path ⇒ Object
Returns the value of attribute config_path.
-
.configurations ⇒ Object
readonly
Returns the value of attribute configurations.
-
.default_block ⇒ Object
readonly
Returns the value of attribute default_block.
Class Method Summary collapse
-
.configure {|Config| ... } ⇒ void
Load configuration using either provided block or default configuration.
-
.debug ⇒ void
Debug output for all configurations.
-
.edit ⇒ void
Open configuration directory in VS Code.
-
.load ⇒ void
Load all registered configurations from their respective files.
-
.method_missing(method_name, *_args) ⇒ Object
Dynamic accessor for registered configurations.
-
.print(*keys) ⇒ void
Print specific configurations or all if no keys provided.
-
.register(key, klass) ⇒ Object
Register a configuration class with memoization.
-
.reset ⇒ void
Reset all configurations (primarily for testing).
- .respond_to_missing?(method_name, include_private = false) ⇒ Boolean
-
.save ⇒ void
Save all registered configurations to their respective files.
-
.set_default {|Config| ... } ⇒ Proc
Set default configuration block used when configure called without block.
Class Attribute Details
.config_path ⇒ Object
Returns the value of attribute config_path.
37 38 39 |
# File 'lib/appydave/tools/configuration/config.rb', line 37 def config_path @config_path end |
.configurations ⇒ Object (readonly)
Returns the value of attribute configurations.
38 39 40 |
# File 'lib/appydave/tools/configuration/config.rb', line 38 def configurations @configurations end |
.default_block ⇒ Object (readonly)
Returns the value of attribute default_block.
39 40 41 |
# File 'lib/appydave/tools/configuration/config.rb', line 39 def default_block @default_block end |
Class Method Details
.configure {|Config| ... } ⇒ void
This method returns an undefined value.
Load configuration using either provided block or default configuration
This method is idempotent and thread-safe. Calling it multiple times has no negative side effects - configurations are memoized on first call.
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/appydave/tools/configuration/config.rb', line 60 def configure if block_given? yield self elsif default_block default_block.call(self) else raise Appydave::Tools::Error, 'No configuration block provided' end ensure_config_directory end |
.debug ⇒ void
This method returns an undefined value.
Debug output for all configurations
156 157 158 159 |
# File 'lib/appydave/tools/configuration/config.rb', line 156 def debug log.kv 'Configuration Path', config_path configurations.each_value(&:debug) end |
.edit ⇒ void
This method returns an undefined value.
Open configuration directory in VS Code
147 148 149 150 151 152 |
# File 'lib/appydave/tools/configuration/config.rb', line 147 def edit ensure_config_directory puts "Edit configuration: #{config_path}" open_vscode = "code --folder-uri '#{config_path}'" # --new-window Open3.capture3(open_vscode) end |
.load ⇒ void
This method returns an undefined value.
Load all registered configurations from their respective files
141 142 143 |
# File 'lib/appydave/tools/configuration/config.rb', line 141 def load configurations.each_value(&:load) end |
.method_missing(method_name, *_args) ⇒ Object
Dynamic accessor for registered configurations
Provides method-style access to registered configuration instances. Called when accessing Config.settings, Config.brands, etc.
115 116 117 118 119 120 |
# File 'lib/appydave/tools/configuration/config.rb', line 115 def method_missing(method_name, *_args) raise Appydave::Tools::Error, 'Configuration has never been registered' if @configurations.nil? raise Appydave::Tools::Error, "Configuration not available: #{method_name}" unless @configurations.key?(method_name) @configurations[method_name] end |
.print(*keys) ⇒ void
This method returns an undefined value.
Print specific configurations or all if no keys provided
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/appydave/tools/configuration/config.rb', line 169 def print(*keys) if keys.empty? keys = configurations.keys else keys.map!(&:to_sym) end keys.each do |key| if configurations[key] configurations[key].print else log.error "Configuration not available: #{key}" end end end |
.register(key, klass) ⇒ Object
This method implements lazy initialization - the configuration instance is only created when first accessed, not at registration time.
Register a configuration class with memoization
Creates a single instance of the configuration class on first call. Subsequent calls return the same instance (memoized). This prevents unnecessary file I/O and ensures consistent configuration state.
88 89 90 91 92 |
# File 'lib/appydave/tools/configuration/config.rb', line 88 def register(key, klass) @configurations ||= {} # Only create new instance if not already registered (prevents multiple reloads) @configurations[key] ||= klass.new end |
.reset ⇒ void
This method returns an undefined value.
Reset all configurations (primarily for testing)
Clears all memoized configuration instances. Use this in test teardown to ensure each test starts with a clean configuration state.
103 104 105 |
# File 'lib/appydave/tools/configuration/config.rb', line 103 def reset @configurations = nil end |
.respond_to_missing?(method_name, include_private = false) ⇒ Boolean
122 123 124 |
# File 'lib/appydave/tools/configuration/config.rb', line 122 def respond_to_missing?(method_name, include_private = false) @configurations.key?(method_name) || super end |
.save ⇒ void
This method returns an undefined value.
Save all registered configurations to their respective files
128 129 130 |
# File 'lib/appydave/tools/configuration/config.rb', line 128 def save configurations.each_value(&:save) end |
.set_default {|Config| ... } ⇒ Proc
Set default configuration block used when configure called without block
135 136 137 |
# File 'lib/appydave/tools/configuration/config.rb', line 135 def set_default(&block) @default_block = block end |