Module: Denv

Defined in:
lib/denv.rb,
lib/denv/rails.rb,
lib/denv/version.rb

Defined Under Namespace

Classes: Error, InvalidFormatError, InvalidKeyNameError, NoSuchFileError, Parser, Railtie

Constant Summary collapse

DEFAULT_ENV_FILENAME =
'.env'.freeze
SPECIAL_KEY =
'__denv_previous_keys__'.freeze
DELIMITER =
'='.freeze
VERSION =
'0.2.2'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.callbackObject

Returns the value of attribute callback.



30
31
32
# File 'lib/denv.rb', line 30

def callback
  @callback
end

Class Method Details

.build_env(filename) ⇒ Hash

Read from .env file and build env Hash.

Parameters:

  • filename (String)

Returns:

  • (Hash)

    loaded environment variables



49
50
51
# 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) ⇒ Hash

Read from .env file and load vars into ‘ENV`. Default is over-write.

Parameters:

  • filename (String) (defaults to: DEFAULT_ENV_FILENAME)

Returns:

  • (Hash)

    env



36
37
38
39
40
41
42
43
44
# 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