bkeepers/dotenv — pull request #34 by bkeepers (2013-05-01 21:35:51+00:00)
Title: Add `Dotenv.env` and `Rails.configuration.env`

This exposes a wrapper around ENV that allows you to AVOID_SCREAMING_SNAKE_CASE_ALL_OVER_THE_PLACE. This implementation feels like a sane balance between just using `ENV` and the bloated configuration frameworks.

From the updated readme:

## Usage

…

Whenever your application loads, these variables will be available in `ENV` and a `Doten.env` helper object:

```ruby
s3 = AWS::S3.new({
:access_key_id => ENV['S3_ACCESS_KEY'],
:secret_access_key => ENV['S3_SECRET_ACCESS_KEY']
})

# or

s3 = AWS::S3.new({
:access_key_id => Dotenv.env.s3_access_key,
:secret_access_key => Dotenv.env.s3_secret_access_key
})
```

### Rails

In Rails, `#env` is available on the `Rails.configuration` object, in controllers…

```ruby
class ApplicationController < ActionController::Base
before_filter :redirect_unknown_hosts

def redirect_unknown_hosts
if request.host != env.app_host
redirect_to "#{env.app_host}#{request.path}"
end
end
end
```

…and in views.

```erb

```

## Customization

Logic-less configuration is ideal, but unfortunately we don't live in an ideal world. You can add logic to your configuration by extending it with your own module:

```
# lib/my_app/config.rb
module MyApp
module Config
def redis_url
self['REDISTOGO_URL'] || self['GH_REDIS_URL'] || "redis://localhost:6379"
end
end
end

# config/application.rb
module MyApp
class Application < Rails::Application
# ...

require 'my_app/config'
config.env.extend MyApp::Config
end
end

# config/initializers/resque.rb
Resque.redis = Redis.connect :url => Rails.configuration.env.redis_url
```