Sha256: 93ce185e41231a157f4eed61446b3bcc4d9486e47eeb9a874d978561c80fb913

Contents?: true

Size: 1.26 KB

Versions: 6

Compression:

Stored size: 1.26 KB

Contents

begin
  require 'dotenv'
rescue LoadError # rubocop:disable Lint/HandleExceptions
end

module Hanami
  # Encapsulate access to ENV
  #
  # @since 0.9.0
  # @api private
  class Env
    # Create a new instance
    #
    # @param env [#[],#[]=] a Hash like object. It defaults to ENV
    #
    # @return [Hanami::Env]
    #
    # @since 0.9.0
    # @api private
    def initialize(env: ENV)
      @env = env
    end

    # Return a value, if found
    #
    # @param key [String] the key
    #
    # @return [String,NilClass] the value, if found
    #
    # @since 0.9.0
    # @api private
    def [](key)
      @env[key]
    end

    # Sets a value
    #
    # @param key [String] the key
    # @param value [String] the value
    #
    # @since 0.9.0
    # @api private
    def []=(key, value)
      @env[key] = value
    end

    # Loads a dotenv file and updates self
    #
    # @param path [String, Pathname] the path to the dotenv file
    #
    # @return void
    #
    # @since 0.9.0
    # @api private
    def load!(path)
      return unless defined?(Dotenv)

      contents = ::File.open(path, "rb:bom|utf-8", &:read)
      parsed   = Dotenv::Parser.call(contents)

      parsed.each do |k, v|
        next if @env.has_key?(k)

        @env[k] = v
      end
      nil
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
hanami-1.1.0.beta1 lib/hanami/env.rb
hanami-1.0.0 lib/hanami/env.rb
hanami-1.0.0.rc1 lib/hanami/env.rb
hanami-1.0.0.beta3 lib/hanami/env.rb
hanami-1.0.0.beta2 lib/hanami/env.rb
hanami-1.0.0.beta1 lib/hanami/env.rb