Skip to content
forked from varvet/econfig

Flexible configuration for Rails applications

License

Notifications You must be signed in to change notification settings

PaulMcAdam/econfig

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Econfig

Econfig is a gem which allows you to easily configure your Rails applications in a multitude of ways.

Installation

Add this to your gemfile:

gem "econfig", :require => "econfig/rails"

Accessing config options

Econfig extends the Rails configuration, you can use it like this:

MyApp::Application.config.app.aws_access_key

Where MyApp is the name of your application and aws_access_key is whatever property you want to access. We recommend you add the shortcut module so you can access config options directly:

module MyApp
  extend Econfig::Shortcut

  
end

In config/application.rb. This will give you:

MyApp.aws_access_key_id

which is obviously way more convenient. The rest of this README is going to assume that you added this shortcut.

Forcing an option to exist

Sometimes you want the application to crash early when a given config option is not set. Just add a bang (!) after the option name, and an error will be thrown if it is not set to a value which is present.

MyApp.aws_access_key_id!

Configuring options.

You can specify configuration through:

  1. ENV variables
  2. Redis
  3. Relational database
  4. YAML files

This allows you to set up econfig on most kinds of hosting solutions (EngineYard, Heroku, etc) without any additional effort, and to switch between them easily. The options are listed in descending order of preference.

ENV variables

Just set an environment variable whose name is the name of the option being accessed uppercased.

For example:

AWS_ACCESS_KEY_ID=xyz rails server

This is especially convenient for Heroku.

Relational database

This needs to be explicitly enabled. In config/application.rb add this line:

Econfig.use_database

You will also need to create a migration to create the necessary database tables:

rails generate econfig:migration
rake db:migrate

You can now set options by assigning them:

MyApp.aws_access_key_id = "xyz"

You may not use both Redis and relational database storage at the same time.

Redis

This needs to be explicitly enabled. In config/application.rb add this line:

Econfig.use_redis Redis.new(:host => "myredis.com")

You can now set options by assigning them:

MyApp.aws_access_key_id = "xyz"

You may not use both Redis and relational database storage at the same time.

YAML

Add a yaml file to config/app.yml. This should have a similar layout to config/database.yml:

development:
  aws_access_key_id: "xyz"
  aws_secret_access_key: "xyz"
production:
  aws_access_key_id: "xyz"
  aws_secret_access_key: "xyz"

License

MIT, see separate LICENSE.txt file

About

Flexible configuration for Rails applications

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%