Class: Appydave::Tools::Configuration::Config

Inherits:
Object
  • Object
show all
Extended by:
KLog::Logging
Defined in:
lib/appydave/tools/configuration/config.rb

Overview

Note:

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.

Examples:

Basic usage

Config.configure  # Load default configuration (idempotent)
Config.settings.video_projects_root  # Access settings
Config.brands.get_brand('appydave')  # Access brands

DAM module usage pattern

# Config.configure called once at module load time
# All subsequent calls within DAM classes are no-ops (memoized)
def some_method
  Config.configure  # Safe to call - returns immediately if already configured
  brand = Config.brands.get_brand('appydave')
end

Registered configurations

Config.settings         # => SettingsConfig instance
Config.brands           # => BrandsConfig instance
Config.channels         # => ChannelsConfig instance
Config.youtube_automation  # => YoutubeAutomationConfig instance

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.config_pathObject

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

.configurationsObject (readonly)

Returns the value of attribute configurations.



38
39
40
# File 'lib/appydave/tools/configuration/config.rb', line 38

def configurations
  @configurations
end

.default_blockObject (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.

Examples:

With block (manual configuration)

Config.configure do |config|
  config.config_path = '/custom/path'
  config.register(:settings, SettingsConfig)
end

Without block (uses default_block)

Config.set_default { |config| config.register(:settings, SettingsConfig) }
Config.configure  # Uses default_block
Config.configure  # Safe to call again - no-op due to memoization

Yields:

  • (Config)

    configuration object for manual setup

Raises:

  • (Error)

    if no block provided and no default_block set



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

.debugvoid

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

.editvoid

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

.loadvoid

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.

Parameters:

  • method_name (Symbol)

    configuration key

Returns:

  • (Object)

    configuration instance

Raises:

  • (Error)

    if configurations not registered or key not found



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

This method returns an undefined value.

Print specific configurations or all if no keys provided

Parameters:

  • keys (Array<String, Symbol>)

    configuration keys to print



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

Note:

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.

Examples:

Config.register(:settings, SettingsConfig)
Config.settings  # => SettingsConfig instance (created on first access)
Config.settings  # => Same instance (memoized)

Parameters:

  • key (Symbol)

    configuration identifier (e.g., :settings, :brands)

  • klass (Class)

    configuration class to instantiate

Returns:

  • (Object)

    configuration instance



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

.resetvoid

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.

Examples:

RSpec usage

after { Config.reset }


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

Returns:

  • (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

.savevoid

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

Yields:

  • (Config)

    configuration block to execute by default

Returns:

  • (Proc)

    the stored block



135
136
137
# File 'lib/appydave/tools/configuration/config.rb', line 135

def set_default(&block)
  @default_block = block
end