Class: Mammoth::Configuration
- Inherits:
-
Object
- Object
- Mammoth::Configuration
- 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 =
File.("../../config/mammoth.schema.json", __dir__)
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#schema_path ⇒ Object
readonly
Returns the value of attribute schema_path.
Class Method Summary collapse
-
.load(path, schema_path: DEFAULT_SCHEMA_PATH) ⇒ Mammoth::Configuration
Load and validate a configuration file.
Instance Method Summary collapse
-
#dig(*keys) ⇒ Object?
Fetch a nested value from the loaded configuration.
-
#initialize(path, schema_path: DEFAULT_SCHEMA_PATH) ⇒ Configuration
constructor
A new instance of Configuration.
-
#load ⇒ Mammoth::Configuration
Load and validate the configuration file.
Constructor Details
#initialize(path, schema_path: DEFAULT_SCHEMA_PATH) ⇒ Configuration
Returns a new instance of Configuration.
29 30 31 32 33 |
# File 'lib/mammoth/configuration.rb', line 29 def initialize(path, schema_path: DEFAULT_SCHEMA_PATH) @path = path @schema_path = schema_path @data = nil end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
15 16 17 |
# File 'lib/mammoth/configuration.rb', line 15 def data @data end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
15 16 17 |
# File 'lib/mammoth/configuration.rb', line 15 def path @path end |
#schema_path ⇒ Object (readonly)
Returns the value of attribute schema_path.
15 16 17 |
# File 'lib/mammoth/configuration.rb', line 15 def schema_path @schema_path end |
Class Method Details
.load(path, schema_path: DEFAULT_SCHEMA_PATH) ⇒ Mammoth::Configuration
Load and validate a configuration file.
23 24 25 |
# File 'lib/mammoth/configuration.rb', line 23 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.
55 56 57 |
# File 'lib/mammoth/configuration.rb', line 55 def dig(*keys) data&.dig(*keys) end |
#load ⇒ Mammoth::Configuration
Load and validate the configuration file.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/mammoth/configuration.rb', line 39 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.}" end |