Sha256: 9de1365d3d74609996e3e729671bfe69d0447301b10e877de4afc248f61ce4ce

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module Dry
  module System
    module ProviderSources
      module Settings
        # @api private
        class Loader
          # @api private
          attr_reader :store

          # @api private
          def initialize(root:, env:, store: ENV)
            @store = store
            load_dotenv(root, env.to_sym)
          end

          # @api private
          def [](key)
            store[key]
          end

          private

          def load_dotenv(root, env)
            require "dotenv"
            Dotenv.load(*dotenv_files(root, env)) if defined?(Dotenv)
          rescue LoadError
            Dry::Core::Deprecations.announce(
              "Dry::System :settings provider now requires dotenv to to load settings from .env files`", # rubocop:disable Layout/LineLength
              "Add `gem \"dotenv\"` to your application's `Gemfile`",
              tag: "dry-system",
              uplevel: 3
            )
            # Do nothing if dotenv is unavailable
          end

          def dotenv_files(root, env)
            [
              File.join(root, ".env.#{env}.local"),
              (File.join(root, ".env.local") unless env == :test),
              File.join(root, ".env.#{env}"),
              File.join(root, ".env")
            ].compact
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dry-system-0.27.2 lib/dry/system/provider_sources/settings/loader.rb
dry-system-0.27.1 lib/dry/system/provider_sources/settings/loader.rb
dry-system-0.27.0 lib/dry/system/provider_sources/settings/loader.rb