Sha256: 759e480910f6984d07e2ae359a60b897179a0c8340b73442a17aeeea8719c3cb

Contents?: true

Size: 1.23 KB

Versions: 3

Compression:

Stored size: 1.23 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|
        @env[k] = v
      end
      nil
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hanami-0.9.2 lib/hanami/env.rb
hanami-0.9.1 lib/hanami/env.rb
hanami-0.9.0 lib/hanami/env.rb