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
252 253 254 255 256 257 258 259 260 261 |
# File 'lib/coradoc/configurable.rb', line 252 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.
232 233 234 |
# File 'lib/coradoc/configurable.rb', line 232 def cache @cache end |
#custom ⇒ Hash (readonly)
Returns Custom configuration values.
247 248 249 |
# File 'lib/coradoc/configurable.rb', line 247 def custom @custom end |
#environment ⇒ String
Returns Current environment.
229 230 231 |
# File 'lib/coradoc/configurable.rb', line 229 def environment @environment end |
#logging ⇒ LoggingConfig (readonly)
Returns Logging configuration.
244 245 246 |
# File 'lib/coradoc/configurable.rb', line 244 def logging @logging end |
#output ⇒ OutputConfig (readonly)
Returns Output configuration.
241 242 243 |
# File 'lib/coradoc/configurable.rb', line 241 def output @output end |
#parser ⇒ ParserConfig (readonly)
Returns Parser configuration.
235 236 237 |
# File 'lib/coradoc/configurable.rb', line 235 def parser @parser end |
#transformer ⇒ TransformerConfig (readonly)
Returns Transformer configuration.
238 239 240 |
# File 'lib/coradoc/configurable.rb', line 238 def transformer @transformer end |
Class Method Details
.load_environment(prefix = 'CORADOC') ⇒ Configuration
Load configuration from environment variables
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/coradoc/configurable.rb', line 363 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
346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/coradoc/configurable.rb', line 346 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
288 289 290 |
# File 'lib/coradoc/configurable.rb', line 288 def [](key) @custom[key.to_sym] end |
#[]=(key, value) ⇒ Object
Set a custom configuration value
296 297 298 |
# File 'lib/coradoc/configurable.rb', line 296 def []=(key, value) @custom[key.to_sym] = value end |
#development? ⇒ Boolean
Check if running in development environment
266 267 268 |
# File 'lib/coradoc/configurable.rb', line 266 def development? @environment == 'development' end |
#dup ⇒ Configuration
Create a copy of this configuration
322 323 324 |
# File 'lib/coradoc/configurable.rb', line 322 def dup self.class.new(to_h) end |
#merge!(other) ⇒ void
This method returns an undefined value.
Merge another configuration into this one
304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/coradoc/configurable.rb', line 304 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
273 274 275 |
# File 'lib/coradoc/configurable.rb', line 273 def production? @environment == 'production' end |
#reset! ⇒ void
This method returns an undefined value.
Reset configuration to defaults
390 391 392 393 394 395 396 397 398 |
# File 'lib/coradoc/configurable.rb', line 390 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
280 281 282 |
# File 'lib/coradoc/configurable.rb', line 280 def test? @environment == 'test' end |
#to_h ⇒ Hash
Convert configuration to hash
329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/coradoc/configurable.rb', line 329 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
423 424 425 |
# File 'lib/coradoc/configurable.rb', line 423 def valid? validate.empty? end |
#validate ⇒ Array<String>
Validate configuration
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/coradoc/configurable.rb', line 403 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 |