config_loader 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +50 -0
- data/lib/config_loader.rb +210 -0
- metadata +47 -0
data/README
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
= Configuration Files Loader
|
2
|
+
|
3
|
+
Configuration Files Loader can be used as a gem or a Rails plugin to load various config files.
|
4
|
+
|
5
|
+
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 the following order: gem<-plugin<-application. This allows 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 shortcut ('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 convenient method for placing secondary DB entries in a code as seen here:
|
6
|
+
|
7
|
+
establish_connection config_loader.load_db_config['secondary_db']
|
8
|
+
|
9
|
+
|
10
|
+
See the 'DRYing Up Configuration Files' post in our team blog http://revolutiononrails.blogspot.com/2007/03/drying-up-configuration-files.html for additional details.
|
11
|
+
|
12
|
+
|
13
|
+
== Setup
|
14
|
+
|
15
|
+
=== Installation
|
16
|
+
|
17
|
+
To install Configuration Files Loader as a gem:
|
18
|
+
|
19
|
+
Get the gem file from the project page (http://rubyforge.org/projects/config-loader) and install it
|
20
|
+
|
21
|
+
To install as a Rails plugin:
|
22
|
+
|
23
|
+
ruby script/plugin install svn://rubyforge.org/var/svn/config-loader/trunk/vendor/plugins/config_loader
|
24
|
+
|
25
|
+
|
26
|
+
=== Usage
|
27
|
+
|
28
|
+
require 'config_loader'
|
29
|
+
|
30
|
+
ConfigLoader = ConfigurationLoader.new
|
31
|
+
|
32
|
+
ConfigLoader.load_db_config # gets the RAILS_ENV section from database.yml
|
33
|
+
|
34
|
+
ConfigLoader.load_file('cfg.yml') # loads a YAML-processed config file
|
35
|
+
|
36
|
+
ConfigLoader.load_dynamic_file('cfg.yml') # loads a ERB+YAML-processed config file
|
37
|
+
|
38
|
+
ConfigLoader.load_section('cfg.yml') # loads a current RAILS_ENV section from a YAML-processed config file
|
39
|
+
|
40
|
+
ConfigLoader.load_dynamic_section('cfg.yml', 'test') # loads a 'test' section from a ERB+YAML-processed config file
|
41
|
+
|
42
|
+
|
43
|
+
== License
|
44
|
+
|
45
|
+
Configuration Files Loader is released under the MIT license.
|
46
|
+
|
47
|
+
|
48
|
+
== Support
|
49
|
+
|
50
|
+
The RubyForge home page is http://rubyforge.org/projects/config-loader
|
@@ -0,0 +1,210 @@
|
|
1
|
+
# Configuration Files Loader can be used as a gem or a Rails plugin to load config files.
|
2
|
+
#
|
3
|
+
# It finds config file fragments in a rails config directory and in config directories of plugins and dependent gems.
|
4
|
+
# It then tries to merge them in order gem<-plugin<-application thus allowing overrides of global (gem/plugin) configs by individual applications.
|
5
|
+
# The supported types for merging are String, Hash, and Array.
|
6
|
+
#
|
7
|
+
# It caches the content of files by default.
|
8
|
+
#
|
9
|
+
# ConfigurationLoader is RoR environment aware and provides a short-cut 'load_section' to load a section of a config file corresponding to RAILS_ENV.
|
10
|
+
# It is being used in another method provided by ConfigurationLoader - load_db_config.
|
11
|
+
# It loads a section from 'config/database.yml' providing a convenience method for getting secondary DB entries in a code like this:
|
12
|
+
#
|
13
|
+
# establish_connection config_loader.load_db_config['secondary_db']
|
14
|
+
#
|
15
|
+
# See the 'DRYing Up Configuration Files' post in our team blog http://revolutiononrails.blogspot.com/2007/03/drying-up-configuration-files.html
|
16
|
+
# for additional details.
|
17
|
+
#
|
18
|
+
#
|
19
|
+
# == Usage
|
20
|
+
#
|
21
|
+
# require 'config_loader'
|
22
|
+
#
|
23
|
+
# ConfigLoader = ConfigurationLoader.new
|
24
|
+
#
|
25
|
+
# ConfigLoader.load_db_config # gets the RAILS_ENV section from database.yml
|
26
|
+
#
|
27
|
+
# ConfigLoader.load_file('cfg.yml') # loads a YAML-processed config file
|
28
|
+
#
|
29
|
+
# ConfigLoader.load_dynamic_file('cfg.yml') # loads a ERB+YAML-processed config file
|
30
|
+
#
|
31
|
+
# ConfigLoader.load_section('cfg.yml') # loads a current RAILS_ENV section from a YAML-processed config file
|
32
|
+
#
|
33
|
+
# ConfigLoader.load_dynamic_section('cfg.yml', 'test') # loads a 'test' section from a ERB+YAML-processed config file
|
34
|
+
#
|
35
|
+
class ConfigurationLoader
|
36
|
+
|
37
|
+
require 'pp'
|
38
|
+
|
39
|
+
# Creats a configuration loader instance
|
40
|
+
# Any vaule but +:use_cache+ would trigger having no caching
|
41
|
+
def initialize(cache_option = :use_cache)
|
42
|
+
@use_cache = (cache_option == :use_cache)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Works as <tt>load_assembled_file</tt> but returns the merged results as a single entry
|
46
|
+
def load_file(file)
|
47
|
+
merge(load_assembled_file(file))
|
48
|
+
end
|
49
|
+
|
50
|
+
# Works as <tt>load_assembled_dynamic_file</tt> but returns the merged results as a single entry
|
51
|
+
def load_dynamic_file(file)
|
52
|
+
merge(load_assembled_dynamic_file(file))
|
53
|
+
end
|
54
|
+
|
55
|
+
# Finds files with a provided file name, loads them, YML-processes, and returns an array of results
|
56
|
+
def load_assembled_file(file)
|
57
|
+
cache[file] ||= yaml_load_file(file)
|
58
|
+
end
|
59
|
+
|
60
|
+
# Finds files with a provided file name, loads them, ERB-processes, YML-processes, and returns an array of results
|
61
|
+
def load_assembled_dynamic_file(file)
|
62
|
+
cache[file] ||= yaml_load_file(file, :erb)
|
63
|
+
end
|
64
|
+
|
65
|
+
# Works as <tt>load_file</tt> but in addition returns only a section (assuming that the entry is a hash) based on the supplied +section+.
|
66
|
+
# If no +section+ is provided, the current +RAILS_ENV+ value is being used
|
67
|
+
def load_section(file, section = default_section, follow_links = false)
|
68
|
+
loaded = load_assembled_file(file)
|
69
|
+
section(loaded, section, follow_links)
|
70
|
+
end
|
71
|
+
|
72
|
+
# Works as <tt>load_assembled_dynamic_file</tt> but in addition returns only a section (assuming that the entry is a hash) based on the supplied +section+.
|
73
|
+
# If no +section+ is provided, the current +RAILS_ENV+ value is being used
|
74
|
+
def load_dynamic_section(file, section = default_section, follow_links = false)
|
75
|
+
loaded = load_assembled_dynamic_file(file)
|
76
|
+
section(loaded, section, follow_links)
|
77
|
+
end
|
78
|
+
|
79
|
+
# A convenience method for loading the current RAILS_ENV section of the Rails DB config file
|
80
|
+
def load_db_config(section = default_section)
|
81
|
+
load_section('database.yml', section)
|
82
|
+
end
|
83
|
+
|
84
|
+
# Empties the cached entries thus forcing it to reload them from a file system
|
85
|
+
def self.refresh!
|
86
|
+
@@cached = {}
|
87
|
+
end
|
88
|
+
|
89
|
+
private
|
90
|
+
|
91
|
+
def section(loaded_configs, section, follow_links)
|
92
|
+
loaded_configs.empty? ? nil : merge(collect_sections(loaded_configs, section, follow_links))
|
93
|
+
end
|
94
|
+
|
95
|
+
def collect_sections(loaded_configs, section, follow_links)
|
96
|
+
loaded_configs.inject([]) do |combined_section, config|
|
97
|
+
s = select_section(config, section, follow_links)
|
98
|
+
combined_section << s if s
|
99
|
+
combined_section
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def select_section(loaded_config, section, follow_links)
|
104
|
+
|
105
|
+
section = loaded_config[section]
|
106
|
+
if follow_links
|
107
|
+
seen_sections = [ ]
|
108
|
+
while section.class == String
|
109
|
+
if seen_sections.include? section
|
110
|
+
raise ArgumentError("Circular references in config file")
|
111
|
+
end
|
112
|
+
seen_sections << section
|
113
|
+
section = loaded_config[section]
|
114
|
+
end
|
115
|
+
end
|
116
|
+
section
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
def default_section
|
121
|
+
(defined?(RAILS_ENV) && RAILS_ENV) || 'development'
|
122
|
+
end
|
123
|
+
|
124
|
+
def file_path(path, file)
|
125
|
+
File.join(path, file)
|
126
|
+
end
|
127
|
+
|
128
|
+
def cache
|
129
|
+
cache? ? (@@cached ||= {}) : {}
|
130
|
+
end
|
131
|
+
|
132
|
+
def configs
|
133
|
+
cache[:__configs__] ||= begin
|
134
|
+
(load_pathes("/gems/") + load_pathes("vendor/plugins/") + app_root).uniq.inject([]) do |configs, path|
|
135
|
+
debug("Component: #{ path }")
|
136
|
+
config = File.join(path, 'config')
|
137
|
+
if File.exist?(config)
|
138
|
+
configs << config
|
139
|
+
debug('Has config')
|
140
|
+
else
|
141
|
+
debug('No config')
|
142
|
+
end
|
143
|
+
configs
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
def app_root
|
149
|
+
path = defined?(RAILS_ROOT) ? RAILS_ROOT : '.'
|
150
|
+
[ File.expand_path(path) ]
|
151
|
+
end
|
152
|
+
|
153
|
+
def load_pathes(pattern)
|
154
|
+
$:.grep(%r{#{ pattern }}).collect { |path| File.expand_path(path[%r{.*#{ pattern }[^/]+}]) }
|
155
|
+
end
|
156
|
+
|
157
|
+
def yaml_load_file(file_name, file_type = :none)
|
158
|
+
|
159
|
+
configs.inject([]) do |loaded, config_path|
|
160
|
+
file = file_path(config_path, file_name)
|
161
|
+
debug("Probing #{ file }")
|
162
|
+
if File.exist?(file)
|
163
|
+
loaded << YAML.load(file_loader(file, file_type))
|
164
|
+
debug('Found')
|
165
|
+
else
|
166
|
+
debug('Not found')
|
167
|
+
end
|
168
|
+
loaded
|
169
|
+
end
|
170
|
+
|
171
|
+
end
|
172
|
+
|
173
|
+
def file_loader(file, file_type)
|
174
|
+
case file_type
|
175
|
+
when :erb then ERB.new(IO.read(file)).result
|
176
|
+
else IO.read(file)
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
def cache?
|
181
|
+
@use_cache
|
182
|
+
end
|
183
|
+
|
184
|
+
def merge(values)
|
185
|
+
|
186
|
+
debug('Merging configs:') { pp values }
|
187
|
+
|
188
|
+
case
|
189
|
+
when all_are?(values, Hash) then
|
190
|
+
values.inject({}) { |c, v| c.merge(v) }
|
191
|
+
when all_are?(values, Array) then
|
192
|
+
values.inject([]) { |c, v| c + v }
|
193
|
+
else
|
194
|
+
values.last
|
195
|
+
end
|
196
|
+
|
197
|
+
end
|
198
|
+
|
199
|
+
def all_are?(values, type)
|
200
|
+
values.all? { |v| v.is_a?(type) }
|
201
|
+
end
|
202
|
+
|
203
|
+
def debug(msg)
|
204
|
+
if ENV['CONFIG_LOADER_DEBUG']
|
205
|
+
puts "CONFIG_LOADER: #{ msg }"
|
206
|
+
yield if block_given?
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.2
|
3
|
+
specification_version: 1
|
4
|
+
name: config_loader
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 1.0.0
|
7
|
+
date: 2007-03-15 00:00:00 -04:00
|
8
|
+
summary: Configuration files loader
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: rails-trunk@revolution.com
|
12
|
+
homepage:
|
13
|
+
rubyforge_project:
|
14
|
+
description:
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: false
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- RHG Team
|
31
|
+
files:
|
32
|
+
- lib/config_loader.rb
|
33
|
+
- README
|
34
|
+
test_files: []
|
35
|
+
|
36
|
+
rdoc_options: []
|
37
|
+
|
38
|
+
extra_rdoc_files:
|
39
|
+
- README
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
requirements: []
|
45
|
+
|
46
|
+
dependencies: []
|
47
|
+
|