Class: ConfigurationLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/configuration_loader.rb

Overview

Configuration Files Loader can be used as a gem or a Rails plugin to load config files.

It finds config file fragments in a rails config directory and in config directories of plugins and dependent gems. It then tries to merge them in order gem<-plugin<-application thus allowing overrides of global (gem/plugin) configs by individual applications. The supported types for merging are String, Hash, and Array.

It caches the content of files by default.

ConfigurationLoader is RoR environment aware and provides a short-cut ‘load_section’ to load a section of a config file corresponding to RAILS_ENV. It is being used in another method provided by ConfigurationLoader - load_db_config. It loads a section from ‘config/database.yml’ providing a convenience method for getting secondary DB entries in a code like this:

establish_connection ConfigurationLoader.load_db_config

See the ‘DRYing Up Configuration Files’ post in our team blog revolutiononrails.blogspot.com/2007/03/drying-up-configuration-files.html for additional details.

Usage

require ‘configuration_loader’

ConfigurationLoader.load_db_config # gets the RAILS_ENV section from database.yml

ConfigurationLoader.load(‘cfg.yml’) # loads a YAML-processed config file

ConfigurationLoader.load(‘cfg.yml’, :erb => true) # loads a ERB+YAML-processed config file

ConfigurationLoader.load(‘cfg.yml’, :section => RAILS_ENV) # loads a current RAILS_ENV section from a YAML-processed config file

ConfigLoader.load(‘cfg.yml’, :cache => false) # loads an uncached copy YAML-processed config file

Constant Summary collapse

@@cached =
{}

Class Method Summary collapse

Class Method Details

.load(file, supplied_options = {}) ⇒ Object

Finds files with a provided file name, loads and processes them. The options control the processing:

:erb => true -- runs the file through ERB before YAML-processing (DEFAULT: false)
:section => 'section_name' -- loads a specific section of the file (like 'development' for 'database.yml') (DEFAULT: nil)
:merge => false -- returns an arrary of processed resulted without trying to merge them into a single entry (DEFAULT: true)
:cache => false -- returns a non-cached copy (DEFAULT: true)


48
49
50
51
52
53
54
# File 'lib/configuration_loader.rb', line 48

def load(file, supplied_options = {})
  options = default_options.merge(supplied_options)
  res = load_file(file, options)
  res = sections(res, options[:section]) if options[:section]
  res = merge(res) if options[:merge]
  res
end

.load_db_config(section = default_section) ⇒ Object

A convenience method for loading the current RAILS_ENV section of the Rails DB config file



57
58
59
# File 'lib/configuration_loader.rb', line 57

def load_db_config(section = default_section)
  load('database.yml', :section => section)
end

.refresh!(key = :everything) ⇒ Object

Empties the cached entry thus forcing to reload from a file system next time



62
63
64
65
66
67
68
69
70
# File 'lib/configuration_loader.rb', line 62

def refresh!(key = :everything)
  case key
    when :everything then
      @@cached = {}
    else
      @@cached.delete(:__configs__)
      @@cached.delete(key)
  end
end