Class: Checkoff::Internal::EnvFallbackConfigLoader

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

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

@param config

@param sym

@param yaml_filename

Parameters:

  • config (::Hash[Symbol, untyped])
  • sym (Symbol)
  • yaml_filename (String)


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

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

Instance Method Details

#[](key) ⇒ Object

@sg-ignore

@param key

Parameters:

  • key (Symbol)

Returns:

  • (Object)


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

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

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

#envvar_name(key) ⇒ String

@param key

Parameters:

  • key (Symbol)

Returns:

  • (String)


46
47
48
# File 'lib/checkoff/internal/config_loader.rb', line 46

def envvar_name(key)
  "#{@envvar_prefix}__#{key.upcase}"
end

#fetch(key) ⇒ Object

@param key

Parameters:

  • key (Symbol)

Returns:

  • (Object)


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

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