Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

85 lines (56 sloc) 2.193 kb

AppConfig

Authors

Mannie Tagarira (modified from AppConfig by Christopher J. Bottaro)

Version

2.0.0

Contact

mannie@mygrid.org.uk

Summary

Application level configuration for Rails 2 and Ruby apps.

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

ApplicationConfiguration.setup { |c|
  c.config_name = "MySettings"
  c.add_file("config.yml")
}

MySettings.aws.access_key  # => "123ABC"
MySettings.aws.secret_key  # => "ABC123"
MySettings.now             # => Tue May 05 21:55:15 -0500 2009
MySettings.servers[0].name # => "example1.com"

MySetting.reload! # use this anytime to reload/refresh your config object

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

ApplicationConfiguration.setup { |c|
  c.config_name = "MyAppConfig"

  c.add_file("base.yml")
  c.add_file("production.yml")
}

MyAppConfig.app_name # => "MyCoolApp"
MyAppConfig.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'

ApplicationConfiguration.setup do |conf|
  conf.config_name = "Settings"

  conf.add_file("#{Rails.root}/config/app_config.yml")
  conf.add_file("#{Rails.root}/config/environments/#{Rails.env}.yml")

  # conf.add_file("#{Rails.root}/config/custom.yml")
end
Jump to Line
Something went wrong with that request. Please try again.