Skip to content
An easy way to load up configuration files in your Rails app
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

ConfigReader 0.0.4 Build Status

ConfigReader provides an easy way to load up your configuration YAML files into Ruby objects, providing a more concise API to access your configuration data, by accessing methods instead of Hash keys. It also allows you to configure environment aware configuration objects, keeping your code DRY.

You can use it to access your FACEBOOK / ANALYTICS configuration YAML's for instance.


Note: The examples are for a Rails 3 app.

  • Add our gem to your Gemfile:

gem 'configreader'

  • The most simple way to use ConfigReader is to change your existing config initializer files to use it like so:

Let's say you have a YAML config like config/facebook.yml

    id: default_id
    secret_key: default_secret_key
    id: 123
    secret_key: secret_key
    id: 456
    secret_key: secret_key
    id: 678
    secret_key: secret_key
    id: 154
    secret_key: secret_key

In your config initializer, initialize an EnvConfigReader like so:

   FACEBOOK ="facebook.yml")

Then you could access FACEBOOK config from anywhere in your Rails app:

Since we are using an EnvConfigReader object, the key we asked for is loaded up from the current RAILS_ENVIRONMENT. By default, ConfigReader will assume your config folder is your Rails.root/config, and will build the full path using it. If the current environment is not specified in the YAML file (for example the demo environment) then EnvConfigReader will use the settings nested under the 'defaults' hash. If no 'defaults' hash exist then the EnvConfigReader will raise an exception.

There's also a FlatConfigReader, which is the more basic version. it's useful for flat config files like so:

   key1: 1
   key2: 2
   FLAT ="flat.yml")

Automatically creating configuration objects

A more advanced feature ConfigReader offers, is automatically loading all your configuration files during the initialization of your Rails app.

It's turned off by default, so to turn it on, we'll need to create an initializer for ConfigReader:


   ConfigReader.initialize do
      config.auto_create_config_objects = true

You're almost done, you'll need to move your YAML configuration files to the default configreader directory: #{Rails.root}/config/configreader

The YAML files in the root configreader folder will be created with a FlatConfigReader, and the ones in the #{Rails.root}/config/configreader/env will be created with an EnvConfigReader. The configuration objects will be set as consts on Object, so you'll be able to access them from anywhere in your Rails app, just like we already did in the initializers.

auto_create_class - You can also override the default auto_create behavior to set the consts instead on Object, to something else like ConfigReader, so then you'll be able to access them using that object: ConfigReader::EXAMPLE

auto_create_config_folder - The default value to the config_folder is #{Rails.root}/config/configreader. Use this config item to override it to another path.


Ruby 1.8.7+, Rails 3.0+.

Something went wrong with that request. Please try again.