module Denv

Constants

DEFAULT_ENV_FILENAME
DELIMITER
SPECIAL_KEY
VERSION

Attributes

callback[RW]

Public Class Methods

build_env(filename) click to toggle source

Read from .env file and build env Hash. @param [String] filename @return [Hash] loaded environment variables

# File lib/denv.rb, line 49
def build_env(filename)
  open_file(filename) {|f| Parser.new(f, filename).parse }
end
load(filename = DEFAULT_ENV_FILENAME) click to toggle source

Read from .env file and load vars into `ENV`. Default is over-write. @param [String] filename @return [Hash] env

# File lib/denv.rb, line 36
def load(filename = DEFAULT_ENV_FILENAME)
  filename = File.expand_path(filename.to_s)
  run_callback(filename) do
    remove_previous_denv_envs
    env = build_env(filename)
    ENV.update(env)
    ENV[SPECIAL_KEY] = env.keys.join(DELIMITER)
  end
end

Private Class Methods

open_file(filename) { |f| ... } click to toggle source
# File lib/denv.rb, line 64
def open_file(filename)
  File.open(filename) {|f| yield f }
rescue Errno::ENOENT
  raise NoSuchFileError.new(filename)
end
remove_previous_denv_envs() click to toggle source
# File lib/denv.rb, line 55
def remove_previous_denv_envs
  return unless ENV[SPECIAL_KEY]

  previous_keys = ENV[SPECIAL_KEY].split(DELIMITER)
  unless previous_keys.empty?
    ENV.delete_if {|k, _| previous_keys.include?(k) }
  end
end
run_callback(filename) { || ... } click to toggle source
# File lib/denv.rb, line 70
def run_callback(filename)
  callback.call(filename) if callback
  yield
end