Class: ConfigurationLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/config_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 config_loader.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 ‘config_loader’

ConfigLoader = ConfigurationLoader.new

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

ConfigLoader.load_file(‘cfg.yml’) # loads a YAML-processed config file

ConfigLoader.load_dynamic_file(‘cfg.yml’) # loads a ERB+YAML-processed config file

ConfigLoader.load_section(‘cfg.yml’) # loads a current RAILS_ENV section from a YAML-processed config file

ConfigLoader.load_dynamic_section(‘cfg.yml’, ‘test’) # loads a ‘test’ section from a ERB+YAML-processed config file

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache_option = :use_cache) ⇒ ConfigurationLoader

Creats a configuration loader instance Any vaule but :use_cache would trigger having no caching



41
42
43
# File 'lib/config_loader.rb', line 41

def initialize(cache_option = :use_cache)
  @use_cache = (cache_option == :use_cache)
end

Class Method Details

.refresh!Object

Empties the cached entries thus forcing it to reload them from a file system



85
86
87
# File 'lib/config_loader.rb', line 85

def self.refresh!
  @@cached = {}
end

Instance Method Details

#load_assembled_dynamic_file(file) ⇒ Object

Finds files with a provided file name, loads them, ERB-processes, YML-processes, and returns an array of results



61
62
63
# File 'lib/config_loader.rb', line 61

def load_assembled_dynamic_file(file)
 cache[file] ||= yaml_load_file(file, :erb)
end

#load_assembled_file(file) ⇒ Object

Finds files with a provided file name, loads them, YML-processes, and returns an array of results



56
57
58
# File 'lib/config_loader.rb', line 56

def load_assembled_file(file)
  cache[file] ||= yaml_load_file(file)
end

#load_db_config(section = default_section) ⇒ Object

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



80
81
82
# File 'lib/config_loader.rb', line 80

def load_db_config(section = default_section)
  load_section('database.yml', section)
end

#load_dynamic_file(file) ⇒ Object

Works as load_assembled_dynamic_file but returns the merged results as a single entry



51
52
53
# File 'lib/config_loader.rb', line 51

def load_dynamic_file(file)
  merge(load_assembled_dynamic_file(file))
end

#load_dynamic_section(file, section = default_section, follow_links = false) ⇒ Object

Works as load_assembled_dynamic_file but in addition returns only a section (assuming that the entry is a hash) based on the supplied section. If no section is provided, the current RAILS_ENV value is being used



74
75
76
77
# File 'lib/config_loader.rb', line 74

def load_dynamic_section(file, section = default_section, follow_links = false)
  loaded = load_assembled_dynamic_file(file)
  section(loaded, section, follow_links)
end

#load_file(file) ⇒ Object

Works as load_assembled_file but returns the merged results as a single entry



46
47
48
# File 'lib/config_loader.rb', line 46

def load_file(file)
  merge(load_assembled_file(file))
end

#load_section(file, section = default_section, follow_links = false) ⇒ Object

Works as load_file but in addition returns only a section (assuming that the entry is a hash) based on the supplied section. If no section is provided, the current RAILS_ENV value is being used



67
68
69
70
# File 'lib/config_loader.rb', line 67

def load_section(file, section = default_section, follow_links = false)
  loaded = load_assembled_file(file)
  section(loaded, section, follow_links)
end