Module: Purtea::Config
- Defined in:
- lib/purtea/config.rb
Overview
Contains methods to load the Purtea config.
Constant Summary collapse
- DEFAULT_CONFIG_PATH =
'config.toml'
- DEFAULT_ENV_PREFIX =
'PURTEA_'
- ENV_NESTED_SEPARATOR =
'__'
Class Method Summary collapse
- .load(path = DEFAULT_CONFIG_PATH, env_prefix: DEFAULT_ENV_PREFIX) ⇒ Object
- .resolve_directory(create: false) ⇒ Object
- .resolve_file(file, create_dir: false) ⇒ Object
Class Method Details
.load(path = DEFAULT_CONFIG_PATH, env_prefix: DEFAULT_ENV_PREFIX) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/purtea/config.rb', line 15 def load(path = DEFAULT_CONFIG_PATH, env_prefix: DEFAULT_ENV_PREFIX) path = resolve_file path, create_dir: true config = if File.exist? path Tomlrb.load_file path else {} end ENV.select { |k, _| k.start_with? env_prefix }.each do |key, value| key_path = key .delete_prefix(env_prefix) .split(ENV_NESTED_SEPARATOR) .map(&:downcase) set_hash_by_path config, key_path, value unless key_path.empty? end config end |
.resolve_directory(create: false) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/purtea/config.rb', line 34 def resolve_directory(create: false) base = ENV['XDG_CONFIG_HOME'] || File.join(Dir.home, '.config') File.join(base, 'purtea').tap do |path| if create && !Dir.exist?(path) Purtea.logger.debug( "Config directory doesn't exist, creating #{path}" ) FileUtils.mkpath(path) end end end |
.resolve_file(file, create_dir: false) ⇒ Object
46 47 48 49 |
# File 'lib/purtea/config.rb', line 46 def resolve_file(file, create_dir: false) directory = resolve_directory create: create_dir File.join directory, file end |