Class: Mammoth::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/mammoth/configuration.rb

Overview

Loads and validates Mammoth YAML configuration.

Configuration is intentionally schema-backed so the same contract can power editor IntelliSense, preflight validation, and runtime startup checks.

Constant Summary collapse

DEFAULT_SCHEMA_PATH =

Default JSON Schema used to validate Mammoth YAML configuration files.

File.expand_path("../../config/mammoth.schema.json", __dir__.to_s)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, schema_path: DEFAULT_SCHEMA_PATH) ⇒ Configuration

Returns a new instance of Configuration.

Parameters:

  • path (String)

    YAML configuration path

  • schema_path (String) (defaults to: DEFAULT_SCHEMA_PATH)

    JSON Schema path



30
31
32
33
34
# File 'lib/mammoth/configuration.rb', line 30

def initialize(path, schema_path: DEFAULT_SCHEMA_PATH)
  @path = path
  @schema_path = schema_path
  @data = nil
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



16
17
18
# File 'lib/mammoth/configuration.rb', line 16

def data
  @data
end

#pathObject (readonly)

Returns the value of attribute path.



16
17
18
# File 'lib/mammoth/configuration.rb', line 16

def path
  @path
end

#schema_pathObject (readonly)

Returns the value of attribute schema_path.



16
17
18
# File 'lib/mammoth/configuration.rb', line 16

def schema_path
  @schema_path
end

Class Method Details

.load(path, schema_path: DEFAULT_SCHEMA_PATH) ⇒ Mammoth::Configuration

Load and validate a configuration file.

Parameters:

  • path (String)

    YAML configuration path

  • schema_path (String) (defaults to: DEFAULT_SCHEMA_PATH)

    JSON Schema path

Returns:

Raises:



24
25
26
# File 'lib/mammoth/configuration.rb', line 24

def self.load(path, schema_path: DEFAULT_SCHEMA_PATH)
  new(path, schema_path: schema_path).load
end

Instance Method Details

#dig(*keys) ⇒ Object?

Fetch a nested value from the loaded configuration.

Parameters:

  • keys (Array<String>)

    nested hash keys

Returns:

  • (Object, nil)

    value or nil



56
57
58
# File 'lib/mammoth/configuration.rb', line 56

def dig(*keys)
  data&.dig(*keys)
end

#loadMammoth::Configuration

Load and validate the configuration file.

Returns:

Raises:



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mammoth/configuration.rb', line 40

def load
  raise ConfigurationError, "configuration file not found: #{path}" unless File.file?(path)

  @data = YAML.safe_load_file(path, permitted_classes: [], aliases: false)
  raise ConfigurationError, "configuration must be a YAML mapping" unless data.is_a?(Hash)

  validate_schema!
  self
rescue Psych::SyntaxError => e
  raise ConfigurationError, "invalid YAML in #{path}: #{e.message}"
end