DotEnv

DotEnv is for managing different environment setups based on a simple textfile.

Installation

Add this line to your application's Gemfile:

gem 'dot_env'

And then execute:

$ bundle

Or simply:

$ gem install dot_env

Usage

Basic usage:

require 'dot_env'

current_environment = DotEnv.get_environment  #=> 'development'

The DotEnv.get_environment method reads a file (named .env as default). It reads, parses and sets all the environment variables found in this file. After that you can access these variables in your application simply using ENV['VARIABLE_NAME'].

If it doesn't find an .env file in your app's root directory, then sets the name of the environment -- as default -- to development.

I made assumption only about naming one variable, and this is 'APP_ENV'. DotEnv tries to detect the current environment's name from this setting.

You can use any other name for your settings file, and you can add that name as a parameter to the function: DotEnv.get_environment('path/to/settings_file')

DotEnv handles the variables as below:

  • accepts shell-compatible variable names
  • values could be quoted or unquoted
  • supports basic shell variable expansion ($VARIABLE and ${VARIABLE})
  • handles simple arrays
  • handles boolean values (true, yes, 1 will be true and false, no, 0 will be false, case insensitively)

After parsing every variable's type will be string, except booleans and arrays.

Environment settings file example:

# example .env file

APP_ENV = "development"
APP_ROOT = $PWD
AN_ARRAY = ['one', '$HOME', True, 42]

There could be empty, and comment rows (prepended with a # sign at the beginning of the row) in the file.

After setting up the environment, you could access these variables from your code:

ENV['APP_ENV']    #=> 'development'
ENV['APP_ROOT']   #=> '/path/to/here'
ENV['AN_ARRAY']   #=> ['one', '/home/username', true, '42']

Contributing

  1. Fork it ( https://github.com/kaze/dot_env/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request