merb-plugins-app-config 0.3 → 0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/application_config/config_builder.rb +19 -12
- data/lib/application_config/view_helpers.rb +10 -3
- data/lib/merb-plugins-app-config.rb +31 -14
- data/spec/app_config_spec.rb +22 -5
- metadata +2 -2
data/Rakefile
CHANGED
@@ -10,19 +10,26 @@ module ApplicationConfig
|
|
10
10
|
# Create a config object (OpenStruct) from a yaml file. If a second yaml file is given, then the sections of that file will overwrite the sections
|
11
11
|
# if the first file if they exist in the first file.
|
12
12
|
def self.load_files(*conf_load_paths)
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
13
|
+
config = OpenStruct.new
|
14
|
+
config.load_paths = []
|
15
|
+
config.load_paths << conf_load_paths
|
16
|
+
config.load_paths.flatten!.uniq!
|
17
|
+
|
18
|
+
# add singleton method to our AppConfig that reloads its settings from the load_paths options
|
19
|
+
def config.reload!
|
20
|
+
conf = {:load_paths => self.load_paths}
|
21
|
+
self.load_paths.to_a.each do |path|
|
22
|
+
file_conf = YAML.load(ERB.new(IO.read(path)).result) if path and File.exists?(path)
|
23
|
+
conf.merge!(file_conf) if file_conf
|
24
|
+
end
|
25
|
+
|
26
|
+
# load all the new values into the openstruct
|
27
|
+
marshal_load(ApplicationConfig::ConfigBuilder.convert(conf).marshal_dump)
|
28
|
+
return self
|
24
29
|
end
|
25
|
-
|
30
|
+
|
31
|
+
config.reload!
|
32
|
+
return config
|
26
33
|
end
|
27
34
|
|
28
35
|
# Recursively converts Hashes to OpenStructs (including Hashes inside Arrays)
|
@@ -2,15 +2,22 @@ module ApplicationConfig
|
|
2
2
|
module ViewHelpers
|
3
3
|
def javascripts
|
4
4
|
js_hash = AppConfigJavascripts.marshal_dump
|
5
|
-
|
5
|
+
if AppConfig.javascripts
|
6
|
+
js_hash.merge!(AppConfig.javascripts.marshal_dump)
|
7
|
+
end
|
8
|
+
js_hash.reject{|key, val| key.to_s == "load_paths"}.each do |key, val|
|
6
9
|
require_js val, :bundle => key.to_sym
|
7
10
|
end
|
8
11
|
include_required_js
|
9
12
|
end
|
10
13
|
|
11
14
|
def stylesheets
|
12
|
-
|
13
|
-
|
15
|
+
css_hash = AppConfigStylesheets.marshal_dump
|
16
|
+
if AppConfig.stylesheets
|
17
|
+
css_hash.merge!(AppConfig.stylesheets.marshal_dump)
|
18
|
+
end
|
19
|
+
|
20
|
+
css_hash.reject{|key, val| key.to_s == "load_paths"}.each do |key, val|
|
14
21
|
require_css val, :bundle => key.to_sym
|
15
22
|
end
|
16
23
|
|
@@ -10,27 +10,44 @@ if defined?(Merb::Plugins)
|
|
10
10
|
}
|
11
11
|
|
12
12
|
Merb::BootLoader.before_app_loads do
|
13
|
-
::AppConfig
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
::AppConfigJavascripts = ApplicationConfig::ConfigBuilder.load_files(Merb.root+"/config/app_config/javascripts.yml")
|
13
|
+
if defined?(::AppConfig)
|
14
|
+
AppConfig.reload!
|
15
|
+
else
|
16
|
+
::AppConfig = ApplicationConfig::ConfigBuilder.load_files("#{Merb.root}/config/app_config/settings.yml",
|
17
|
+
"#{Merb.root}/config/app_config/#{Merb.env}.yml")
|
19
18
|
end
|
20
19
|
|
21
|
-
# add reload method to our ostruct
|
22
|
-
def AppConfig.reload!
|
23
|
-
AppConfig.marshal_load(ApplicationConfig::ConfigBuilder.reload.marshal_dump)
|
24
|
-
return AppConfig
|
25
|
-
end
|
26
|
-
|
27
20
|
if Merb::Plugins.config[:app_config][:include_viewhelpers]
|
21
|
+
if defined?(::AppConfigStylesheets)
|
22
|
+
AppConfigStylesheets.reload!
|
23
|
+
else
|
24
|
+
::AppConfigStylesheets = ApplicationConfig::ConfigBuilder.load_files("#{Merb.root}/config/stylesheets.yml",
|
25
|
+
"#{Merb.root}/config/app_config/stylesheets.yml")
|
26
|
+
end
|
27
|
+
|
28
|
+
if defined?(::AppConfigJavascripts)
|
29
|
+
AppConfigJavascripts.reload!
|
30
|
+
else
|
31
|
+
::AppConfigJavascripts = ApplicationConfig::ConfigBuilder.load_files("#{Merb.root}/config/javascripts.yml",
|
32
|
+
"#{Merb.root}/config/app_config/javascripts.yml")
|
33
|
+
end
|
34
|
+
|
28
35
|
Merb::Controller.send(:include, ApplicationConfig::ViewHelpers)
|
29
36
|
end
|
30
37
|
end
|
31
38
|
|
32
|
-
|
33
|
-
|
39
|
+
Merb::BootLoader.after_app_loads do
|
40
|
+
if Merb::Plugins.config[:app_config][:auto_reload]
|
41
|
+
Merb.logger.info "Auto reloading AppConfig on *every request*. Set via Merb::Plugins.config[:app_config][:auto_reload]"
|
42
|
+
Merb::Controller.before do
|
43
|
+
AppConfig.reload!
|
44
|
+
if Merb::Plugins.config[:app_config][:include_viewhelpers]
|
45
|
+
AppConfigStylesheets.reload!
|
46
|
+
AppConfigJavascripts.reload!
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
34
51
|
|
35
52
|
Merb::Plugins.add_rakefiles "merbtasks"
|
36
53
|
end
|
data/spec/app_config_spec.rb
CHANGED
@@ -30,17 +30,34 @@ describe "AppConfig Plugin" do
|
|
30
30
|
end
|
31
31
|
|
32
32
|
it "should not crash on missing files" do
|
33
|
-
|
34
|
-
config
|
33
|
+
files = ["#{@settings_path}/nothing_here.yml", "#{@settings_path}/nothing_here2.yml"]
|
34
|
+
config = ApplicationConfig::ConfigBuilder.load_files(*files)
|
35
|
+
config.should == OpenStruct.new(:load_paths => files)
|
35
36
|
end
|
36
37
|
|
37
38
|
it "should allow erb in settings" do
|
38
|
-
|
39
|
+
files = ["#{@settings_path}/development.yml"]
|
40
|
+
config = ApplicationConfig::ConfigBuilder.load_files(*files)
|
39
41
|
config.computed.should == 6
|
40
42
|
end
|
41
43
|
|
42
44
|
it "should not crash on empty files" do
|
43
|
-
|
44
|
-
config
|
45
|
+
files = ["#{@settings_path}/empty1.yml", "#{@settings_path}/empty2.yml"]
|
46
|
+
config = ApplicationConfig::ConfigBuilder.load_files(*files)
|
47
|
+
config.should == OpenStruct.new(:load_paths => files)
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
it "should allow reloads" do
|
52
|
+
config = ApplicationConfig::ConfigBuilder.load_files("#{@settings_path}/development.yml")
|
53
|
+
check = lambda { |config|
|
54
|
+
config.section.size.should == 3
|
55
|
+
config.section.servers.is_a?(Array).should be_true
|
56
|
+
config.section.servers.first.name.should == "yahoo.com"
|
57
|
+
config.section.servers.last.name.should == "amazon.com"
|
58
|
+
}
|
59
|
+
check.call(config)
|
60
|
+
config.reload!
|
61
|
+
check.call(config)
|
45
62
|
end
|
46
63
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: merb-plugins-app-config
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: "0.
|
4
|
+
version: "0.4"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jacques Crocker
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-11-
|
12
|
+
date: 2008-11-16 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|