Sha256: c28a21c0001634790d4bc8178f24e1a9c9954b782b5121e79bef9ec24fc9daf5

Contents?: true

Size: 1.96 KB

Versions: 153

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

require 'yaml'
require 'active_support/core_ext/hash'

module Checkoff
  module Internal
    # Use the provided config from a YAML file, and fall back to env
    # variable if it's not populated for a key'
    class EnvFallbackConfigLoader
      # @param config [Hash<Symbol, Object>]
      # @param sym [Symbol]
      # @param yaml_filename [String]
      def initialize(config, sym, yaml_filename)
        @config = config
        @envvar_prefix = sym.upcase
        @yaml_filename = yaml_filename
      end

      # @param key [Symbol]
      # @return [Object]
      def [](key)
        config_value = @config[key]
        return config_value unless config_value.nil?

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

      # @param key [Symbol]
      # @return [Object]
      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

      private

      # @param key [Symbol]
      # @return [String]
      def envvar_name(key)
        "#{@envvar_prefix}__#{key.upcase}"
      end
    end

    # Load configuration file
    class ConfigLoader
      class << self
        # @sg-ignore
        # @return [Hash<Symbol, Object>]
        def load(sym)
          yaml_result = load_yaml_file(sym)
          EnvFallbackConfigLoader.new(yaml_result, sym, yaml_filename(sym))
        end

        private

        # @param sym [Symbol]
        # @return [Hash<[String, Symbol], Object>]
        def load_yaml_file(sym)
          filename = yaml_filename(sym)
          return {} unless File.exist?(filename)

          # @sg-ignore
          YAML.load_file(filename).with_indifferent_access
        end

        # @param sym [Symbol]
        # @return [String]
        def yaml_filename(sym)
          file = "#{sym}.yml"
          File.expand_path("~/.#{file}")
        end
      end
    end
  end
end

Version data entries

153 entries across 153 versions & 1 rubygems

Version Path
checkoff-0.161.0 lib/checkoff/internal/config_loader.rb
checkoff-0.160.0 lib/checkoff/internal/config_loader.rb
checkoff-0.159.0 lib/checkoff/internal/config_loader.rb
checkoff-0.158.0 lib/checkoff/internal/config_loader.rb
checkoff-0.157.0 lib/checkoff/internal/config_loader.rb
checkoff-0.156.0 lib/checkoff/internal/config_loader.rb
checkoff-0.155.0 lib/checkoff/internal/config_loader.rb
checkoff-0.154.0 lib/checkoff/internal/config_loader.rb
checkoff-0.153.0 lib/checkoff/internal/config_loader.rb
checkoff-0.152.0 lib/checkoff/internal/config_loader.rb
checkoff-0.151.0 lib/checkoff/internal/config_loader.rb
checkoff-0.150.0 lib/checkoff/internal/config_loader.rb
checkoff-0.149.0 lib/checkoff/internal/config_loader.rb
checkoff-0.148.0 lib/checkoff/internal/config_loader.rb
checkoff-0.147.0 lib/checkoff/internal/config_loader.rb
checkoff-0.145.0 lib/checkoff/internal/config_loader.rb
checkoff-0.139.0 lib/checkoff/internal/config_loader.rb
checkoff-0.138.0 lib/checkoff/internal/config_loader.rb
checkoff-0.137.0 lib/checkoff/internal/config_loader.rb
checkoff-0.136.0 lib/checkoff/internal/config_loader.rb