Class: Rubino::Config::Loader
- Inherits:
-
Object
- Object
- Rubino::Config::Loader
- Defined in:
- lib/rubino/config/loader.rb
Overview
Responsible for loading configuration from YAML files and environment. Searches in order: project-local, user home, defaults.
Constant Summary collapse
- CONFIG_FILENAME =
"config.yml"- ENV_FILENAME =
".env"- ENV_VAR_PATTERN =
/\$\{([A-Z_][A-Z0-9_]*)\}/
Instance Attribute Summary collapse
-
#config_path ⇒ Object
readonly
Returns the value of attribute config_path.
-
#env_path ⇒ Object
readonly
Returns the value of attribute env_path.
-
#home_path ⇒ Object
readonly
Returns the value of attribute home_path.
Class Method Summary collapse
-
.default_home_path ⇒ Object
Single source of truth for the home directory: RUBINO_HOME when set, else ~/.rubino.
Instance Method Summary collapse
-
#config_exists? ⇒ Boolean
Returns true if a config file exists.
-
#create_default_config! ⇒ Object
Creates the initial config file with defaults.
-
#initialize(home_path: nil) ⇒ Loader
constructor
A new instance of Loader.
-
#load ⇒ Object
Loads configuration from file, merging with defaults.
-
#raw_config ⇒ Object
The RAW user config.yml as a Hash (NOT merged with defaults), or {} when no file exists.
Constructor Details
#initialize(home_path: nil) ⇒ Loader
Returns a new instance of Loader.
31 32 33 34 35 |
# File 'lib/rubino/config/loader.rb', line 31 def initialize(home_path: nil) @home_path = home_path || self.class.default_home_path @config_path = File.join(@home_path, CONFIG_FILENAME) @env_path = File.join(@home_path, ENV_FILENAME) end |
Instance Attribute Details
#config_path ⇒ Object (readonly)
Returns the value of attribute config_path.
17 18 19 |
# File 'lib/rubino/config/loader.rb', line 17 def config_path @config_path end |
#env_path ⇒ Object (readonly)
Returns the value of attribute env_path.
17 18 19 |
# File 'lib/rubino/config/loader.rb', line 17 def env_path @env_path end |
#home_path ⇒ Object (readonly)
Returns the value of attribute home_path.
17 18 19 |
# File 'lib/rubino/config/loader.rb', line 17 def home_path @home_path end |
Class Method Details
.default_home_path ⇒ Object
Single source of truth for the home directory: RUBINO_HOME when set, else ~/.rubino. Rubino.home_path delegates here so the server (which loads config via the Loader) and the CLI commands (config/setup/doctor) resolve the SAME directory — previously the server honoured $RUBINO_HOME while the CLI recomputed File.join(Rubino.home_path, “config.yml”) off the YAML ‘paths.home` default (~/.rubino), a split brain at first boot.
26 27 28 29 |
# File 'lib/rubino/config/loader.rb', line 26 def self.default_home_path env = ENV["RUBINO_HOME"].to_s.strip env.empty? ? File.("~/.rubino") : File.(env) end |
Instance Method Details
#config_exists? ⇒ Boolean
Returns true if a config file exists
66 67 68 |
# File 'lib/rubino/config/loader.rb', line 66 def config_exists? File.exist?(@config_path) end |
#create_default_config! ⇒ Object
Creates the initial config file with defaults
71 72 73 74 75 |
# File 'lib/rubino/config/loader.rb', line 71 def create_default_config! FileUtils.mkdir_p(@home_path) File.write(@config_path, Defaults.to_yaml) @config_path end |
#load ⇒ Object
Loads configuration from file, merging with defaults.
Every way a config.yml can be unusable is normalized into a single ConfigError with a clean, actionable message here — at the source —so no raw Ruby/Psych backtrace can escape to the boot path (CFG-1 / CFG-R2). Covers: a YAML syntax error, an alias/anchor reference (safe_load rejects aliases by design), a config path that is a directory or a symlink to one (Errno::EISDIR), a non-Hash top-level (a bare scalar like ‘foo`, or a sequence), and any other read/parse failure.
47 48 49 50 51 52 53 |
# File 'lib/rubino/config/loader.rb', line 47 def load raw = load_raw_config load_env_file if File.exist?(@env_path) deep_merge(Defaults.to_hash, (raw)) end |
#raw_config ⇒ Object
The RAW user config.yml as a Hash (NOT merged with defaults), or {} when no file exists. Public so the boot guard / doctor can run LOAD-time schema validation (F8) over exactly what the user hand-edited — defaults are valid by construction, so merging them in would hide the user’s mistakes. Same normalization as #load (a malformed shape raises ConfigError), so callers get a clean error, never a raw Psych backtrace.
61 62 63 |
# File 'lib/rubino/config/loader.rb', line 61 def raw_config load_raw_config end |