0.0
No commit activity in last 3 years
No release in over 3 years
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, and by so inverting the logic of environment specific configuration into the ConfigReader.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.9
>= 2.11.0, ~> 2.11

Runtime

> 3.0.0, ~> 3.0
> 3.0.0
 Project Readme

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.

Usage

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

 defaults:
    id: default_id
    secret_key: default_secret_key
 development:
    id: 123
    secret_key: secret_key
 production:
    id: 456
    secret_key: secret_key
 test:
    id: 678
    secret_key: secret_key
 staging:
    id: 154
    secret_key: secret_key

In your config initializer, initialize an EnvConfigReader like so:

   FACEBOOK = ConfigReader::EnvConfigReader.new("facebook.yml")

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

   FACEBOOK.id
   FACEBOOK.secret_key

Since we are using an EnvConfigReader object, the FACEBOOK.id 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 = ConfigReader::FlatConfigReader.new("flat.yml")
   FLAT.key1

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:

config/initializers/configreader.rb

   ConfigReader.initialize do
      config.auto_create_config_objects = true
   end

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.

Requirements

Ruby 1.8.7+, Rails 3.0+.