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.201.0 lib/checkoff/internal/config_loader.rb
checkoff-0.200.0 lib/checkoff/internal/config_loader.rb
checkoff-0.199.0 lib/checkoff/internal/config_loader.rb
checkoff-0.198.0 lib/checkoff/internal/config_loader.rb
checkoff-0.197.0 lib/checkoff/internal/config_loader.rb
checkoff-0.196.0 lib/checkoff/internal/config_loader.rb
checkoff-0.195.0 lib/checkoff/internal/config_loader.rb
checkoff-0.194.0 lib/checkoff/internal/config_loader.rb
checkoff-0.193.0 lib/checkoff/internal/config_loader.rb
checkoff-0.192.0 lib/checkoff/internal/config_loader.rb
checkoff-0.191.0 lib/checkoff/internal/config_loader.rb
checkoff-0.190.0 lib/checkoff/internal/config_loader.rb
checkoff-0.189.0 lib/checkoff/internal/config_loader.rb
checkoff-0.188.0 lib/checkoff/internal/config_loader.rb
checkoff-0.187.0 lib/checkoff/internal/config_loader.rb
checkoff-0.186.0 lib/checkoff/internal/config_loader.rb
checkoff-0.185.0 lib/checkoff/internal/config_loader.rb
checkoff-0.184.0 lib/checkoff/internal/config_loader.rb
checkoff-0.183.0 lib/checkoff/internal/config_loader.rb
checkoff-0.182.0 lib/checkoff/internal/config_loader.rb