Class: Checkoff::Internal::EnvFallbackConfigLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/checkoff/internal/config_loader.rb

Overview

Use the provided config from a YAML file, and fall back to env variable if it’s not populated for a key’

Instance Method Summary collapse

Constructor Details

#initialize(config, sym, yaml_filename) ⇒ EnvFallbackConfigLoader

Returns a new instance of EnvFallbackConfigLoader.

Parameters:

  • config (Hash<Symbol, Object>)
  • sym (Symbol)
  • yaml_filename (String)


14
15
16
17
18
# File 'lib/checkoff/internal/config_loader.rb', line 14

def initialize(config, sym, yaml_filename)
  @config = config
  @envvar_prefix = sym.upcase
  @yaml_filename = yaml_filename
end

Instance Method Details

#[](key) ⇒ Object

Parameters:

  • key (Symbol)

Returns:

  • (Object)


22
23
24
25
26
27
28
# File 'lib/checkoff/internal/config_loader.rb', line 22

def [](key)
  config_value = @config[key]
  return config_value unless config_value.nil?

  # @sg-ignore
  ENV.fetch(envvar_name(key), nil)
end

#fetch(key) ⇒ Object

Parameters:

  • key (Symbol)

Returns:

  • (Object)

Raises:

  • (KeyError)


32
33
34
35
36
37
38
# File 'lib/checkoff/internal/config_loader.rb', line 32

def fetch(key)
  out = self[key]
  return out unless out.nil?

  raise KeyError,
        "Please configure either the #{key} key in #{@yaml_filename} or set #{envvar_name(key)}"
end