Class: Coradoc::Configurable::Configuration
- Inherits:
-
Object
- Object
- Coradoc::Configurable::Configuration
- Defined in:
- lib/coradoc/configurable.rb
Overview
Main configuration class
Instance Attribute Summary collapse
-
#cache ⇒ CacheConfig
readonly
Cache configuration.
-
#custom ⇒ Hash
readonly
Custom configuration values.
-
#environment ⇒ String
Current environment.
-
#logging ⇒ LoggingConfig
readonly
Logging configuration.
-
#output ⇒ OutputConfig
readonly
Output configuration.
-
#parser ⇒ ParserConfig
readonly
Parser configuration.
-
#transformer ⇒ TransformerConfig
readonly
Transformer configuration.
Class Method Summary collapse
-
.load_environment(prefix = 'CORADOC') ⇒ Configuration
Load configuration from environment variables.
-
.load_file(path) ⇒ Configuration
Load configuration from a YAML file.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Get a custom configuration value.
-
#[]=(key, value) ⇒ Object
Set a custom configuration value.
-
#development? ⇒ Boolean
Check if running in development environment.
-
#dup ⇒ Configuration
Create a copy of this configuration.
-
#initialize(options = {}) ⇒ Configuration
constructor
Create a new configuration.
-
#merge!(other) ⇒ void
Merge another configuration into this one.
-
#production? ⇒ Boolean
Check if running in production environment.
-
#reset! ⇒ void
Reset configuration to defaults.
-
#test? ⇒ Boolean
Check if running in test environment.
-
#to_h ⇒ Hash
Convert configuration to hash.
-
#valid? ⇒ Boolean
Check if configuration is valid.
-
#validate ⇒ Array<String>
Validate configuration.
Constructor Details
#initialize(options = {}) ⇒ Configuration
Create a new configuration
256 257 258 259 260 261 262 263 264 265 |
# File 'lib/coradoc/configurable.rb', line 256 def initialize( = {}) = symbolize_keys() @environment = .fetch(:environment, detect_environment) @cache = CacheConfig.new([:cache] || {}) @parser = ParserConfig.new([:parser] || {}) @transformer = TransformerConfig.new([:transformer] || {}) @output = OutputConfig.new([:output] || {}) @logging = LoggingConfig.new([:logging] || {}) @custom = .fetch(:custom, {}) end |
Instance Attribute Details
#cache ⇒ CacheConfig (readonly)
Returns Cache configuration.
236 237 238 |
# File 'lib/coradoc/configurable.rb', line 236 def cache @cache end |
#custom ⇒ Hash (readonly)
Returns Custom configuration values.
251 252 253 |
# File 'lib/coradoc/configurable.rb', line 251 def custom @custom end |
#environment ⇒ String
Returns Current environment.
233 234 235 |
# File 'lib/coradoc/configurable.rb', line 233 def environment @environment end |
#logging ⇒ LoggingConfig (readonly)
Returns Logging configuration.
248 249 250 |
# File 'lib/coradoc/configurable.rb', line 248 def logging @logging end |
#output ⇒ OutputConfig (readonly)
Returns Output configuration.
245 246 247 |
# File 'lib/coradoc/configurable.rb', line 245 def output @output end |
#parser ⇒ ParserConfig (readonly)
Returns Parser configuration.
239 240 241 |
# File 'lib/coradoc/configurable.rb', line 239 def parser @parser end |
#transformer ⇒ TransformerConfig (readonly)
Returns Transformer configuration.
242 243 244 |
# File 'lib/coradoc/configurable.rb', line 242 def transformer @transformer end |
Class Method Details
.load_environment(prefix = 'CORADOC') ⇒ Configuration
Load configuration from environment variables
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/coradoc/configurable.rb', line 367 def self.load_environment(prefix = 'CORADOC') = {} # Parse CORADOC_CACHE_ENABLED=true style variables ENV.each do |key, value| next unless key.start_with?("#{prefix}_") # Convert CORADOC_CACHE_ENABLED to [:cache, :enabled] parts = key.sub("#{prefix}_", '').downcase.split('_') next if parts.length < 2 section = parts.first.to_sym setting = parts[1..].join('_').to_sym [section] ||= {} [section][setting] = parse_env_value(value) end # Special handling for CORADOC_ENV [:environment] = ENV["#{prefix}_ENV"] if ENV["#{prefix}_ENV"] new() end |
.load_file(path) ⇒ Configuration
Load configuration from a YAML file
350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/coradoc/configurable.rb', line 350 def self.load_file(path) require 'yaml' raise ConfigurationError, "Configuration file not found: #{path}" unless File.exist?(path) begin yaml_content = YAML.load_file(path) new(yaml_content || {}) rescue Psych::SyntaxError => e raise ConfigurationError, "Invalid YAML in #{path}: #{e.}" end end |
Instance Method Details
#[](key) ⇒ Object
Get a custom configuration value
292 293 294 |
# File 'lib/coradoc/configurable.rb', line 292 def [](key) @custom[key.to_sym] end |
#[]=(key, value) ⇒ Object
Set a custom configuration value
300 301 302 |
# File 'lib/coradoc/configurable.rb', line 300 def []=(key, value) @custom[key.to_sym] = value end |
#development? ⇒ Boolean
Check if running in development environment
270 271 272 |
# File 'lib/coradoc/configurable.rb', line 270 def development? @environment == 'development' end |
#dup ⇒ Configuration
Create a copy of this configuration
326 327 328 |
# File 'lib/coradoc/configurable.rb', line 326 def dup self.class.new(to_h) end |
#merge!(other) ⇒ void
This method returns an undefined value.
Merge another configuration into this one
308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/coradoc/configurable.rb', line 308 def merge!(other) case other when Configuration @environment = other.environment if other.environment != detect_environment @cache.merge!(other.cache) @parser.merge!(other.parser) @transformer.merge!(other.transformer) @output.merge!(other.output) @logging.merge!(other.logging) @custom.merge!(other.custom) when Hash merge_hash(other) end end |
#production? ⇒ Boolean
Check if running in production environment
277 278 279 |
# File 'lib/coradoc/configurable.rb', line 277 def production? @environment == 'production' end |
#reset! ⇒ void
This method returns an undefined value.
Reset configuration to defaults
394 395 396 397 398 399 400 401 402 |
# File 'lib/coradoc/configurable.rb', line 394 def reset! @environment = detect_environment @cache = CacheConfig.new @parser = ParserConfig.new @transformer = TransformerConfig.new @output = OutputConfig.new @logging = LoggingConfig.new @custom = {} end |
#test? ⇒ Boolean
Check if running in test environment
284 285 286 |
# File 'lib/coradoc/configurable.rb', line 284 def test? @environment == 'test' end |
#to_h ⇒ Hash
Convert configuration to hash
333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/coradoc/configurable.rb', line 333 def to_h { environment: @environment, cache: @cache.to_h, parser: @parser.to_h, transformer: @transformer.to_h, output: @output.to_h, logging: @logging.to_h, custom: @custom.dup } end |
#valid? ⇒ Boolean
Check if configuration is valid
427 428 429 |
# File 'lib/coradoc/configurable.rb', line 427 def valid? validate.empty? end |
#validate ⇒ Array<String>
Validate configuration
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'lib/coradoc/configurable.rb', line 407 def validate errors = [] errors << 'cache.max_size must be positive' if @cache.max_size <= 0 errors << 'cache.ttl must be non-negative' if @cache.ttl.negative? errors << 'parser.max_nesting_depth must be positive' if @parser.max_nesting_depth <= 0 errors << 'output.line_width must be positive' if @output.line_width <= 0 errors << 'cache.backend must be :memory, :file, or :redis' unless %i[memory file redis].include?(@cache.backend) errors << 'logging.level must be :debug, :info, :warn, or :error' unless %i[debug info warn error].include?(@logging.level) errors end |