Sha256: f2c8c01855dcb205befc7dbbdd786829d677592a54dfd783a2ee3e4ad3256855

Contents?: true

Size: 1.28 KB

Versions: 17

Compression:

Stored size: 1.28 KB

Contents

begin
  require 'dotenv/parser'
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::Parser)

      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

17 entries across 17 versions & 1 rubygems

Version Path
hanami-1.3.5 lib/hanami/env.rb
hanami-1.3.4 lib/hanami/env.rb
hanami-1.3.3 lib/hanami/env.rb
hanami-1.3.2 lib/hanami/env.rb
hanami-1.3.1 lib/hanami/env.rb
hanami-1.3.0 lib/hanami/env.rb
hanami-1.3.0.beta1 lib/hanami/env.rb
hanami-1.2.0 lib/hanami/env.rb
hanami-1.2.0.rc2 lib/hanami/env.rb
hanami-1.2.0.rc1 lib/hanami/env.rb
hanami-1.2.0.beta2 lib/hanami/env.rb
hanami-1.2.0.beta1 lib/hanami/env.rb
hanami-1.1.1 lib/hanami/env.rb
hanami-1.1.0 lib/hanami/env.rb
hanami-1.1.0.rc1 lib/hanami/env.rb
hanami-1.1.0.beta3 lib/hanami/env.rb
hanami-1.1.0.beta2 lib/hanami/env.rb