revolutionhealth-config_loader 2.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/LICENSE +20 -0
- data/Manifest.txt +13 -0
- data/README +52 -0
- data/Rakefile +65 -0
- data/TODO +4 -0
- data/lib/config_loader.rb +249 -0
- data/lib/service_config.rb +118 -0
- data/test/config/service.yml +9 -0
- data/test/config/test_reload.yml +3 -0
- data/test/test_config_reload.rb +116 -0
- data/test/test_helper.rb +7 -0
- data/test/test_service_config.rb +22 -0
- metadata +69 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2007 Revolution Health
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest.txt
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
LICENSE
|
2
|
+
Manifest.txt
|
3
|
+
Rakefile
|
4
|
+
README
|
5
|
+
TODO
|
6
|
+
lib/config_loader.rb
|
7
|
+
lib/service_config.rb
|
8
|
+
test/test_config_reload.rb
|
9
|
+
test/test_helper.rb
|
10
|
+
test/test_service_config.rb
|
11
|
+
test/config/database.yml
|
12
|
+
test/config/service.yml
|
13
|
+
test/config/test_reload.yml
|
data/README
ADDED
@@ -0,0 +1,52 @@
|
|
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
|
+
Finds config file fragments in a Rails config directory and in the 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
|
+
See the 'DRYing Up Configuration Files' post in our blog http://revolutiononrails.blogspot.com/2007/03/drying-up-configuration-files.html for additional details.
|
10
|
+
|
11
|
+
|
12
|
+
= Setup
|
13
|
+
|
14
|
+
== Installation
|
15
|
+
|
16
|
+
To install Configuration Files Loader as a gem:
|
17
|
+
|
18
|
+
sudo gem install revolutionhealth-config_loader -s http://gems.github.com
|
19
|
+
|
20
|
+
== Source
|
21
|
+
|
22
|
+
http://github.com/revolutionhealth/config_loader
|
23
|
+
|
24
|
+
|
25
|
+
= Usage
|
26
|
+
|
27
|
+
require 'config_loader'
|
28
|
+
|
29
|
+
== ConfigLoader
|
30
|
+
|
31
|
+
ConfigLoader.load_db_config # gets the RAILS_ENV section from database.yml
|
32
|
+
|
33
|
+
ConfigLoader.load_file('cfg.yml') # loads a YAML-processed config file
|
34
|
+
|
35
|
+
ConfigLoader.load_dynamic_file('cfg.yml') # loads a ERB+YAML-processed config file
|
36
|
+
|
37
|
+
ConfigLoader.load_section('cfg.yml') # loads a current RAILS_ENV section from a YAML-processed config file
|
38
|
+
|
39
|
+
ConfigLoader.load_dynamic_section('cfg.yml', 'test') # loads a 'test' section from a ERB+YAML-processed config file
|
40
|
+
|
41
|
+
== ServiceConfig
|
42
|
+
|
43
|
+
Assuming you have a file 'config/service.yml' with a 'search' entry for your RAILS_ENV setting
|
44
|
+
|
45
|
+
ServiceConfig.endpoint('search') # gets the url of the service
|
46
|
+
|
47
|
+
ServiceConfig.timeout('timeout') # gets the timeout of the service
|
48
|
+
|
49
|
+
|
50
|
+
= Support
|
51
|
+
|
52
|
+
The RubyForge home page is http://github.com/revolutionhealth/config_loader
|
data/Rakefile
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake/gempackagetask'
|
3
|
+
require 'rubygems/specification'
|
4
|
+
require 'date'
|
5
|
+
|
6
|
+
GEM = "config_loader"
|
7
|
+
GEM_VERSION = "2.0.0"
|
8
|
+
AUTHOR = "Revolution Health"
|
9
|
+
EMAIL = "rails-trunk@revolutionhealth.com"
|
10
|
+
HOMEPAGE = %q{http://github.com/revolutionhealth/config_loader}
|
11
|
+
SUMMARY = "Provides convenience methods for the loading of configuration files"
|
12
|
+
DESCRIPTION = "Abstracts out the loading of common configuration files such as database.yml dependent on the Rails environment"
|
13
|
+
|
14
|
+
spec = Gem::Specification.new do |s|
|
15
|
+
s.name = GEM
|
16
|
+
s.version = GEM_VERSION
|
17
|
+
s.date = Time.now.strftime("%Y-%m-%d")
|
18
|
+
s.platform = Gem::Platform::RUBY
|
19
|
+
s.has_rdoc = true
|
20
|
+
s.extra_rdoc_files = ["Manifest.txt", "README"]
|
21
|
+
s.rdoc_options = ["--main", "README"]
|
22
|
+
|
23
|
+
s.summary = SUMMARY
|
24
|
+
s.description = DESCRIPTION
|
25
|
+
s.author = AUTHOR
|
26
|
+
s.email = EMAIL
|
27
|
+
s.homepage = HOMEPAGE
|
28
|
+
|
29
|
+
# Uncomment this to add a dependency
|
30
|
+
# s.add_dependency "foo"
|
31
|
+
|
32
|
+
s.require_path = 'lib'
|
33
|
+
s.autorequire = GEM
|
34
|
+
s.files = %w(Manifest.txt LICENSE README TODO Rakefile) + Dir.glob("{lib,test}/**/*")
|
35
|
+
s.test_files = Dir.glob('test/**/test_*.rb') - ['test/test_helper.rb']
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
41
|
+
pkg.gem_spec = spec
|
42
|
+
end
|
43
|
+
|
44
|
+
desc "install the gem locally"
|
45
|
+
task :install => [:package] do
|
46
|
+
sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}}
|
47
|
+
end
|
48
|
+
|
49
|
+
desc "create a gemspec file"
|
50
|
+
task :make_spec do
|
51
|
+
File.open("#{GEM}.gemspec", "w") do |file|
|
52
|
+
file.puts spec.to_ruby
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
require 'test/unit'
|
57
|
+
|
58
|
+
task :test do
|
59
|
+
runner = Test::Unit::AutoRunner.new(true)
|
60
|
+
runner.to_run << 'test'
|
61
|
+
runner.run
|
62
|
+
end
|
63
|
+
|
64
|
+
task :default => [:test, :package] do
|
65
|
+
end
|
@@ -0,0 +1,249 @@
|
|
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
|
+
# In your environment.rb
|
22
|
+
# config.gem 'revolutionhealth-config_loader', :lib => 'config_loader, :source => 'http://gems.github.com'
|
23
|
+
#
|
24
|
+
# Examples
|
25
|
+
# Gets the RAILS_ENV section from database.yml
|
26
|
+
# ConfigLoader.load_db_config
|
27
|
+
#
|
28
|
+
# Loads a YAML-processed config file
|
29
|
+
# ConfigLoader.load_file('cfg.yml')
|
30
|
+
#
|
31
|
+
# Loads a ERB+YAML-processed config file
|
32
|
+
# ConfigLoader.load_dynamic_file('cfg.yml')
|
33
|
+
#
|
34
|
+
# Loads a current RAILS_ENV section from a YAML-processed config file
|
35
|
+
# ConfigLoader.load_section('cfg.yml')
|
36
|
+
#
|
37
|
+
# Loads a 'test' section from a ERB+YAML-processed config file
|
38
|
+
# ConfigLoader.load_dynamic_section('cfg.yml', 'test')
|
39
|
+
#
|
40
|
+
|
41
|
+
require 'yaml'
|
42
|
+
require 'pp'
|
43
|
+
require 'erb'
|
44
|
+
require File.join(File.dirname(__FILE__), 'service_config')
|
45
|
+
|
46
|
+
class ConfigurationLoader
|
47
|
+
|
48
|
+
def initialize(cache_option = :force_cache, base_path = nil)
|
49
|
+
@caching = (cache_option == :force_cache)
|
50
|
+
@base_path = base_path
|
51
|
+
end
|
52
|
+
|
53
|
+
def load_file(file, use_env = false)
|
54
|
+
merge(load_assembled_file(file, use_env))
|
55
|
+
end
|
56
|
+
|
57
|
+
def load_dynamic_file(file, use_env = false)
|
58
|
+
merge(load_assembled_dynamic_file(file, use_env))
|
59
|
+
end
|
60
|
+
|
61
|
+
def load_assembled_file(file, use_env = false)
|
62
|
+
cache[key(file, use_env)] ||= yaml_load_file(file, use_env)
|
63
|
+
end
|
64
|
+
|
65
|
+
def load_assembled_dynamic_file(file, use_env = false)
|
66
|
+
cache[key(file, use_env)] ||= yaml_load_file(file, use_env, :erb)
|
67
|
+
end
|
68
|
+
|
69
|
+
def load_section(file, section = default_section, follow_links = false)
|
70
|
+
loaded = load_assembled_file(file)
|
71
|
+
section(loaded, section, follow_links)
|
72
|
+
end
|
73
|
+
|
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
|
+
def load_raw_file(file)
|
80
|
+
cache[key(file, false)] ||= yaml_load_file(file, false, :raw).last
|
81
|
+
end
|
82
|
+
|
83
|
+
def load_service_config
|
84
|
+
begin
|
85
|
+
load_dynamic_section('service.yml')
|
86
|
+
rescue => ex
|
87
|
+
puts "Warning: no or broken service.yml detected for this project - #{ ex }"
|
88
|
+
{}
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def load_db_config(section = default_section)
|
93
|
+
load_section('database.yml', section)
|
94
|
+
end
|
95
|
+
|
96
|
+
private
|
97
|
+
|
98
|
+
def section(loaded_configs, section, follow_links)
|
99
|
+
loaded_configs.empty? ? nil : merge(collect_sections(loaded_configs, section, follow_links))
|
100
|
+
end
|
101
|
+
|
102
|
+
def collect_sections(loaded_configs, section, follow_links)
|
103
|
+
loaded_configs.inject([]) do |combined_section, config|
|
104
|
+
s = select_section(config, section, follow_links)
|
105
|
+
combined_section << s if s
|
106
|
+
combined_section
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def select_section(loaded_config, section, follow_links)
|
111
|
+
|
112
|
+
section = loaded_config[section]
|
113
|
+
if follow_links
|
114
|
+
seen_sections = [ ]
|
115
|
+
while section.class == String
|
116
|
+
if seen_sections.include? section
|
117
|
+
raise ArgumentError("Circular references in config file")
|
118
|
+
end
|
119
|
+
seen_sections << section
|
120
|
+
section = loaded_config[section]
|
121
|
+
end
|
122
|
+
end
|
123
|
+
section
|
124
|
+
|
125
|
+
end
|
126
|
+
|
127
|
+
def default_section
|
128
|
+
rails_env
|
129
|
+
end
|
130
|
+
|
131
|
+
def rails_env
|
132
|
+
RAILS_ENV || 'development'
|
133
|
+
end
|
134
|
+
|
135
|
+
def file_path(path, file, use_env)
|
136
|
+
to_load = use_env ? "environments/#{ rails_env }-#{ file }" : file
|
137
|
+
File.join(path, to_load)
|
138
|
+
end
|
139
|
+
|
140
|
+
def cache
|
141
|
+
if cache?
|
142
|
+
if loaded_file_changed?
|
143
|
+
@@cached = nil
|
144
|
+
loaded_files.clear
|
145
|
+
end
|
146
|
+
(@@cached ||= {})
|
147
|
+
else
|
148
|
+
{}
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
def key(file, env)
|
153
|
+
"#{ file }_#{ env }"
|
154
|
+
end
|
155
|
+
|
156
|
+
def loaded_files
|
157
|
+
(@@loaded_files ||= {})
|
158
|
+
end
|
159
|
+
|
160
|
+
# frequency in secs to reload config files
|
161
|
+
RELOAD_FREQ_SECS = 10
|
162
|
+
|
163
|
+
def loaded_file_changed?
|
164
|
+
result = false
|
165
|
+
now = Time.now
|
166
|
+
# check every 10 sec
|
167
|
+
if (now > ((@@last_file_changed_check ||= now) + RELOAD_FREQ_SECS))
|
168
|
+
loaded_files.each do |file, time|
|
169
|
+
if (File.stat(file).mtime <=> time) != 0
|
170
|
+
result = true
|
171
|
+
break
|
172
|
+
end
|
173
|
+
end
|
174
|
+
@@last_file_changed_check = now
|
175
|
+
end
|
176
|
+
result
|
177
|
+
end
|
178
|
+
|
179
|
+
def configs
|
180
|
+
cache[:configs] ||= begin
|
181
|
+
(load_pathes("/gems/") + load_pathes("vendor/plugins/") + app_root).uniq.inject([]) do |configs, path|
|
182
|
+
puts "Component: #{ path }" if ENV['CONFIG_LOADER_DEBUG']
|
183
|
+
config = File.join(path, 'config')
|
184
|
+
configs << config if File.exist?(config)
|
185
|
+
configs
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
def app_root
|
191
|
+
raise "Must set base_path if RAILS_ROOT is nil" unless (defined?(RAILS_ROOT) or @base_path)
|
192
|
+
path_to_return = defined?(RAILS_ROOT) ? RAILS_ROOT : @base_path
|
193
|
+
return [ File.expand_path(path_to_return) ]
|
194
|
+
end
|
195
|
+
|
196
|
+
def load_pathes(pattern)
|
197
|
+
$:.grep(%r{#{ pattern }}).collect { |path| File.expand_path(path[%r{.*#{ pattern }[^/]+}]) }
|
198
|
+
end
|
199
|
+
|
200
|
+
def yaml_load_file(file_name, use_env, file_type = :none)
|
201
|
+
configs.inject([]) do |loaded, config_path|
|
202
|
+
file = file_path(config_path, file_name, use_env)
|
203
|
+
puts "Probing #{ file }" if ENV['CONFIG_LOADER_DEBUG']
|
204
|
+
if File.exist?(file)
|
205
|
+
f = file_loader(file, file_type)
|
206
|
+
res = (file_type == :raw) ? f : YAML.load(f)
|
207
|
+
loaded << res
|
208
|
+
end
|
209
|
+
loaded
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
def file_loader(file, file_type)
|
214
|
+
loaded_files[file.to_s] = File.stat(file).mtime
|
215
|
+
case file_type
|
216
|
+
when :erb then ERB.new(IO.read(file)).result
|
217
|
+
else IO.read(file)
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
def cache?
|
222
|
+
@caching
|
223
|
+
end
|
224
|
+
|
225
|
+
def merge(values)
|
226
|
+
|
227
|
+
if ENV['CONFIG_LOADER_DEBUG']
|
228
|
+
puts "Merging configs: "
|
229
|
+
pp values
|
230
|
+
end
|
231
|
+
|
232
|
+
case
|
233
|
+
when all_are?(values, Hash) then
|
234
|
+
values.inject({}) { |c, v| c.merge(v) }
|
235
|
+
when all_are?(values, Array) then
|
236
|
+
values.inject([]) { |c, v| c + v }
|
237
|
+
else
|
238
|
+
values.last
|
239
|
+
end
|
240
|
+
|
241
|
+
end
|
242
|
+
|
243
|
+
def all_are?(values, type)
|
244
|
+
values.all? { |v| v.is_a?(type) }
|
245
|
+
end
|
246
|
+
|
247
|
+
end
|
248
|
+
|
249
|
+
::ConfigLoader = ConfigurationLoader.new unless defined?(ConfigLoader)
|
@@ -0,0 +1,118 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
3
|
+
module ServiceConfig
|
4
|
+
extend self
|
5
|
+
|
6
|
+
ConfigLogger = defined?(RAILS_DEFAULT_LOGGER) ? RAILS_DEFAULT_LOGGER : Logger.new(STDOUT)
|
7
|
+
|
8
|
+
def endpoint(service)
|
9
|
+
begin
|
10
|
+
cfg[service.to_s]['url']
|
11
|
+
rescue
|
12
|
+
nil
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def config_timeout(service)
|
17
|
+
begin
|
18
|
+
cfg[service.to_s]['timeout']
|
19
|
+
rescue
|
20
|
+
log_not_found service
|
21
|
+
ConfigLogger.error "Configuring a default timeout of 5 seconds."
|
22
|
+
5
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def is_mocked?(service)
|
27
|
+
begin
|
28
|
+
cfg[service.to_s][:mocked.to_s]
|
29
|
+
rescue
|
30
|
+
log_not_found service
|
31
|
+
false
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def is_service_backed?(service)
|
36
|
+
begin
|
37
|
+
cfg[service.to_s][:service_backed.to_s]
|
38
|
+
rescue
|
39
|
+
log_not_found service
|
40
|
+
false
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def mocked(service)
|
45
|
+
cfg[service.to_s][:mocked.to_s]
|
46
|
+
end
|
47
|
+
|
48
|
+
def service_backed(service)
|
49
|
+
cfg[service.to_s][:service_backed.to_s]
|
50
|
+
end
|
51
|
+
|
52
|
+
def login
|
53
|
+
cfg[:service_creds.to_s][:login.to_s]
|
54
|
+
end
|
55
|
+
|
56
|
+
def host(service)
|
57
|
+
begin
|
58
|
+
cfg[service.to_s]['host']
|
59
|
+
rescue
|
60
|
+
log_not_found service
|
61
|
+
nil
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def path(service)
|
66
|
+
begin
|
67
|
+
cfg[service.to_s]['path']
|
68
|
+
rescue
|
69
|
+
log_not_found service
|
70
|
+
nil
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def port(service)
|
75
|
+
begin
|
76
|
+
cfg[service.to_s]['port']
|
77
|
+
rescue
|
78
|
+
log_not_found service
|
79
|
+
nil
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def service_params(service)
|
84
|
+
cfg[service.to_s]
|
85
|
+
end
|
86
|
+
|
87
|
+
def sp_entity_value(service)
|
88
|
+
find_target(service, 'sp_entity_value')
|
89
|
+
end
|
90
|
+
|
91
|
+
def sp_target_value(service)
|
92
|
+
find_target(service, 'sp_target_value')
|
93
|
+
end
|
94
|
+
|
95
|
+
def find_target(service, name)
|
96
|
+
begin
|
97
|
+
cfg[service.to_s][name]
|
98
|
+
rescue
|
99
|
+
log_not_found "service: #{service.to_s}, attr: #{name}"
|
100
|
+
nil
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def ends_with_slash(url)
|
105
|
+
(url[-1,1] == '/') ? url : url + '/'
|
106
|
+
end
|
107
|
+
|
108
|
+
private
|
109
|
+
|
110
|
+
def log_not_found(service)
|
111
|
+
ConfigLogger.error "Could not load endpoint configuration for #{service.to_s}"
|
112
|
+
end
|
113
|
+
|
114
|
+
def cfg
|
115
|
+
ConfigLoader.load_service_config
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
|
+
|
3
|
+
class ConfigurationLoader
|
4
|
+
alias :file_loader_old :file_loader
|
5
|
+
|
6
|
+
def reset_loader_called
|
7
|
+
@@loader_called = false
|
8
|
+
end
|
9
|
+
|
10
|
+
def loader_called
|
11
|
+
(@@loader_called ||= false)
|
12
|
+
end
|
13
|
+
|
14
|
+
def file_loader(file, file_type)
|
15
|
+
@@loader_called = true
|
16
|
+
file_loader_old(file, file_type)
|
17
|
+
end
|
18
|
+
|
19
|
+
def loaded_file_changed_checkonly?
|
20
|
+
orig = @@last_file_changed_check if defined? @@last_file_changed_check
|
21
|
+
changed = loaded_file_changed?
|
22
|
+
@@last_file_changed_check = orig
|
23
|
+
changed
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class TestConfigReload < Test::Unit::TestCase
|
28
|
+
|
29
|
+
def test_config_reload
|
30
|
+
loaded = ConfigLoader.load_file('test_reload.yml')
|
31
|
+
assert_not_nil loaded
|
32
|
+
assert ConfigLoader.loader_called
|
33
|
+
|
34
|
+
another_loaded = ConfigLoader.load_file('database.yml')
|
35
|
+
assert_not_nil another_loaded
|
36
|
+
|
37
|
+
# should be under threshold of timeout
|
38
|
+
changed = false
|
39
|
+
changed = ConfigLoader.loaded_file_changed_checkonly?
|
40
|
+
assert !changed
|
41
|
+
|
42
|
+
sleep 5
|
43
|
+
ConfigLoader.reset_loader_called
|
44
|
+
## after sleeping it should not reload since the mtime is same
|
45
|
+
|
46
|
+
changed = false
|
47
|
+
changed = ConfigLoader.loaded_file_changed_checkonly?
|
48
|
+
assert !changed
|
49
|
+
|
50
|
+
|
51
|
+
not_reloaded = ConfigLoader.load_file('test_reload.yml')
|
52
|
+
assert_equal loaded, not_reloaded
|
53
|
+
|
54
|
+
assert !ConfigLoader.loader_called
|
55
|
+
|
56
|
+
#try another file
|
57
|
+
another_loaded = ConfigLoader.load_file('database.yml')
|
58
|
+
assert_not_nil another_loaded
|
59
|
+
assert !ConfigLoader.loader_called
|
60
|
+
|
61
|
+
## change the file
|
62
|
+
new_adapter = 'oracle'
|
63
|
+
orig_adapter = loaded['test']['adapter']
|
64
|
+
loaded['test']['adapter'] = new_adapter
|
65
|
+
|
66
|
+
|
67
|
+
test_reload_file = File.join(File.dirname(__FILE__), 'config', 'test_reload.yml')
|
68
|
+
File.open(test_reload_file, 'w' ) do |out|
|
69
|
+
YAML.dump(loaded, out )
|
70
|
+
end
|
71
|
+
|
72
|
+
sleep 5
|
73
|
+
ConfigLoader.reset_loader_called
|
74
|
+
|
75
|
+
changed = ConfigLoader.loaded_file_changed_checkonly?
|
76
|
+
assert changed
|
77
|
+
|
78
|
+
reloaded = ConfigLoader.load_file('test_reload.yml')
|
79
|
+
assert ConfigLoader.loader_called
|
80
|
+
assert_equal new_adapter, reloaded['test']['adapter']
|
81
|
+
|
82
|
+
# should be under threshold of timeout
|
83
|
+
changed = false
|
84
|
+
changed = ConfigLoader.loaded_file_changed_checkonly?
|
85
|
+
assert !changed
|
86
|
+
|
87
|
+
#try another file
|
88
|
+
another_loaded = ConfigLoader.load_file('database.yml')
|
89
|
+
assert_not_nil another_loaded
|
90
|
+
assert ConfigLoader.loader_called
|
91
|
+
|
92
|
+
|
93
|
+
loaded['test']['adapter'] = orig_adapter
|
94
|
+
File.open( test_reload_file, 'w' ) do |out|
|
95
|
+
YAML.dump(loaded, out )
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_check_only
|
100
|
+
ConfigLoader.loaded_file_changed_checkonly?
|
101
|
+
ConfigLoader.instance_eval { loaded_file_changed? }
|
102
|
+
ConfigLoader.loaded_file_changed_checkonly?
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_use_alternate_base_path
|
106
|
+
Object.send(:remove_const, "RAILS_ROOT")
|
107
|
+
assert_raise(RuntimeError) {
|
108
|
+
ConfigurationLoader.new(false).send(:app_root)
|
109
|
+
}
|
110
|
+
top_dir = File.join(File.dirname(__FILE__), '..')
|
111
|
+
conf = ConfigurationLoader.new(false, top_dir)
|
112
|
+
assert conf.send(:app_root).first == File.expand_path(top_dir)
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
class TestServiceConfig < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_service_config
|
7
|
+
service_yaml = YAML::load_file(File.join(File.dirname(__FILE__), 'config', 'service.yml'))
|
8
|
+
service_yaml['test']['search'].each do |k, v|
|
9
|
+
real_key = case k
|
10
|
+
when 'url'
|
11
|
+
'endpoint'
|
12
|
+
when 'timeout'
|
13
|
+
'config_timeout'
|
14
|
+
else
|
15
|
+
k
|
16
|
+
end
|
17
|
+
|
18
|
+
assert ServiceConfig.send(real_key.to_sym, 'search') == v
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: revolutionhealth-config_loader
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Revolution Health
|
8
|
+
autorequire: config_loader
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-06-25 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Abstracts out the loading of common configuration files such as database.yml dependent on the Rails environment
|
17
|
+
email: rails-trunk@revolutionhealth.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- Manifest.txt
|
24
|
+
- README
|
25
|
+
files:
|
26
|
+
- Manifest.txt
|
27
|
+
- LICENSE
|
28
|
+
- README
|
29
|
+
- TODO
|
30
|
+
- Rakefile
|
31
|
+
- lib/config_loader.rb
|
32
|
+
- lib/service_config.rb
|
33
|
+
- test/config
|
34
|
+
- test/test_config_reload.rb
|
35
|
+
- test/test_helper.rb
|
36
|
+
- test/test_service_config.rb
|
37
|
+
- test/config/database.yml
|
38
|
+
- test/config/service.yml
|
39
|
+
- test/config/test_reload.yml
|
40
|
+
has_rdoc: true
|
41
|
+
homepage: http://github.com/revolutionhealth/config_loader
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options:
|
44
|
+
- --main
|
45
|
+
- README
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
requirements: []
|
61
|
+
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.2.0
|
64
|
+
signing_key:
|
65
|
+
specification_version: 2
|
66
|
+
summary: Provides convenience methods for the loading of configuration files
|
67
|
+
test_files:
|
68
|
+
- test/test_config_reload.rb
|
69
|
+
- test/test_service_config.rb
|