Sha256: 1fcfd52a25787f2171e8339ba78bd6c34ee0c273bd20d8667ab60031338648ab

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

require "dry/core/constants"

module Hanami
  class Settings
    # Default app 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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hanami-2.0.0.beta1.1 lib/hanami/settings/dotenv_store.rb
hanami-2.0.0.beta1 lib/hanami/settings/dotenv_store.rb