Sha256: 03a58f17de45c96317416e69a7455d35dd2d5325cfbcfeb9257f9d7343c8fc1f

Contents?: true

Size: 1.51 KB

Versions: 7

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

require "dry/core/constants"

module Hanami
  class Application
    class Settings
      # Default application settings store.
      #
      # Uses [dotenv](https://github.com/bkeepers/dotenv) (if available) to load
      # .env files and then loads settings from ENV. For a given `HANAMI_ENV`
      # environment, the following `.env` files are looked up in the following order:
      #
      # - .env.{environment}.local
      # - .env.local (except if the environment is `test`)
      # - .env.{environment}
      # - .env
      #
      # @since 2.0.0
      # @api private
      class DotenvStore
        Undefined = Dry::Core::Constants::Undefined

        attr_reader :store,
                    :hanami_env

        def initialize(store: ENV, hanami_env: Hanami.env)
          @store = store
          @hanami_env = hanami_env
        end

        def fetch(name, default_value = Undefined, &block)
          name = name.to_s.upcase
          args = (default_value == Undefined) ? [name] : [name, default_value]

          store.fetch(*args, &block)
        end

        def with_dotenv_loaded
          require "dotenv"
          Dotenv.load(*dotenv_files) if defined?(Dotenv)
          self
        rescue LoadError
          self
        end

        private

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
hanami-2.0.0.alpha8 lib/hanami/application/settings/dotenv_store.rb
hanami-2.0.0.alpha7.1 lib/hanami/application/settings/dotenv_store.rb
hanami-2.0.0.alpha7 lib/hanami/application/settings/dotenv_store.rb
hanami-2.0.0.alpha6 lib/hanami/application/settings/dotenv_store.rb
hanami-2.0.0.alpha5 lib/hanami/application/settings/dotenv_store.rb
hanami-2.0.0.alpha4 lib/hanami/application/settings/dotenv_store.rb
hanami-2.0.0.alpha3 lib/hanami/application/settings/dotenv_store.rb