Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Rails plugin that provides environment specific application configuration.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 CHANGELOG
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 Rakefile
Octocat-spinner-32 VERSION.yml
Octocat-spinner-32 app_config.gemspec
Octocat-spinner-32 install.rb
README.rdoc

Summary

Application level configuration. If you are using Rails 3, please use RailsConfig (github.com/railsjedi/rails_config) instead.

Features

* simple YAML config files
* config files support ERB
* config files support inheritance
* access config information via convenient object member notation

Basic Usage

You simply write a configuration file in YAML. Notice you can use ERB.

config.yml

aws:
  access_key: 123ABC
  secret_key: ABC123
now: <%= Time.now %>
servers: [ {name: example1.com}, {name: example2.com} ]

Then somewhere in your code, you create a global constant from the config file. Then access the config data via object member notation.

code

::AppConfig = ApplicationConfiguration.new("config.yml")
AppConfig.aws.access_key  # => "123ABC"
AppConfig.aws.secret_key  # => "ABC123"
AppConfig.now             # => Tue May 05 21:55:15 -0500 2009
AppConfig.servers[0].name # => "example1.com"

Inheritance

You can have a second config file that is recursively merged with the first config file.

base.yml

app_name:  MyCoolApp
domain:  dev.mycoolapp.com

production.yml

domain:  www.mycoolapp.com

code

::AppConfig = ApplicationConfiguration.new("base.yml", "production.yml")
AppConfig.app_name # => "MyCoolApp"
AppConfig.domain   # => "www.mycoolapp.com"

Using in a Rails app

By default, installing via `script/plugin install`/`rails plugin install` will create an initializer file containing the following content:

code

require 'app_config'

::AppConfig = ApplicationConfiguration.new("#{Rails.root}/config/app_config.yml",
                                           "#{Rails.root}/config/environments/#{Rails.env}.yml")

Environments

Alternatively to splitting out your environments into separate files, you can just have a single file which defines the application configuration for all environments (much like how databases.yml works). Note if you do this, nested configurations will not be recursively merged. See example below.

app_config.yml

defaults: &defaults
  one: won
  two: too
  nested:
    foo: foo
    bar: bar

development:
  <<: *defaults
  two: new
  nested:
    foo: bar

code

RAILS_ENV # => "development"
::AppConfig = ApplicationConfiguration.new("app_config.yml")
AppConfig.use_environment!(RAILS_ENV)
AppConfig.one # => "won"
AppConfig.two # => "new"
AppConfig.nested.foo # => "bar"
AppConfig.nested.bar # raises NoMethodError because nested configurations are not recursively merged

Author

Christopher J. Bottaro

Something went wrong with that request. Please try again.