Class: Coradoc::Configurable::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/coradoc/configurable.rb

Overview

Main configuration class

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Configuration

Create a new configuration

Parameters:

  • options (Hash) (defaults to: {})

    Configuration options



252
253
254
255
256
257
258
259
260
261
# File 'lib/coradoc/configurable.rb', line 252

def initialize(options = {})
  options = symbolize_keys(options)
  @environment = options.fetch(:environment, detect_environment)
  @cache = CacheConfig.new(options[:cache] || {})
  @parser = ParserConfig.new(options[:parser] || {})
  @transformer = TransformerConfig.new(options[:transformer] || {})
  @output = OutputConfig.new(options[:output] || {})
  @logging = LoggingConfig.new(options[:logging] || {})
  @custom = options.fetch(:custom, {})
end

Instance Attribute Details

#cacheCacheConfig (readonly)

Returns Cache configuration.

Returns:



232
233
234
# File 'lib/coradoc/configurable.rb', line 232

def cache
  @cache
end

#customHash (readonly)

Returns Custom configuration values.

Returns:

  • (Hash)

    Custom configuration values



247
248
249
# File 'lib/coradoc/configurable.rb', line 247

def custom
  @custom
end

#environmentString

Returns Current environment.

Returns:

  • (String)

    Current environment



229
230
231
# File 'lib/coradoc/configurable.rb', line 229

def environment
  @environment
end

#loggingLoggingConfig (readonly)

Returns Logging configuration.

Returns:



244
245
246
# File 'lib/coradoc/configurable.rb', line 244

def logging
  @logging
end

#outputOutputConfig (readonly)

Returns Output configuration.

Returns:



241
242
243
# File 'lib/coradoc/configurable.rb', line 241

def output
  @output
end

#parserParserConfig (readonly)

Returns Parser configuration.

Returns:



235
236
237
# File 'lib/coradoc/configurable.rb', line 235

def parser
  @parser
end

#transformerTransformerConfig (readonly)

Returns Transformer configuration.

Returns:



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

Parameters:

  • prefix (String) (defaults to: 'CORADOC')

    Environment variable prefix

Returns:



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')
  options = {}

  # 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

    options[section] ||= {}
    options[section][setting] = parse_env_value(value)
  end

  # Special handling for CORADOC_ENV
  options[:environment] = ENV["#{prefix}_ENV"] if ENV["#{prefix}_ENV"]

  new(options)
end

.load_file(path) ⇒ Configuration

Load configuration from a YAML file

Parameters:

  • path (String)

    Path to configuration file

Returns:

Raises:



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.message}"
  end
end

Instance Method Details

#[](key) ⇒ Object

Get a custom configuration value

Parameters:

  • key (Symbol)

    Configuration key

Returns:

  • (Object)

    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

Parameters:

  • key (Symbol)

    Configuration key

  • value (Object)

    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

Returns:

  • (Boolean)


266
267
268
# File 'lib/coradoc/configurable.rb', line 266

def development?
  @environment == 'development'
end

#dupConfiguration

Create a copy of this configuration

Returns:



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

Parameters:



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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


280
281
282
# File 'lib/coradoc/configurable.rb', line 280

def test?
  @environment == 'test'
end

#to_hHash

Convert configuration to hash

Returns:

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

Returns:

  • (Boolean)


423
424
425
# File 'lib/coradoc/configurable.rb', line 423

def valid?
  validate.empty?
end

#validateArray<String>

Validate configuration

Returns:

  • (Array<String>)

    List of validation errors



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