Class: Rubino::Config::Loader

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

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.expand_path("~/.rubino") : File.expand_path(env)
end

Instance Method Details

#config_exists?Boolean

Returns true if a config file exists

Returns:

  • (Boolean)


57
58
59
# File 'lib/rubino/config/loader.rb', line 57

def config_exists?
  File.exist?(@config_path)
end

#create_default_config!Object

Creates the initial config file with defaults



62
63
64
65
66
# File 'lib/rubino/config/loader.rb', line 62

def create_default_config!
  FileUtils.mkdir_p(@home_path)
  File.write(@config_path, Defaults.to_yaml)
  @config_path
end

#loadObject

Loads configuration from file, merging with defaults



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rubino/config/loader.rb', line 38

def load
  raw =
    if File.exist?(@config_path)
      begin
        YAML.safe_load_file(@config_path, permitted_classes: [Symbol]) || {}
      rescue Psych::SyntaxError => e
        raise ConfigError,
              "Invalid YAML in #{@config_path} at line #{e.line}, column #{e.column}: #{e.problem}"
      end
    else
      {}
    end

  load_env_file if File.exist?(@env_path)

  deep_merge(Defaults.to_hash, expand_env_vars(raw))
end