merb-plugins-app-config 0.3
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/README +83 -0
- data/Rakefile +53 -0
- data/lib/application_config/config_builder.rb +45 -0
- data/lib/application_config/view_helpers.rb +20 -0
- data/lib/merb-plugins-app-config.rb +36 -0
- data/lib/merbtasks.rb +17 -0
- data/spec/app_config_spec.rb +46 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/test_configs/app_config.yml +2 -0
- data/spec/test_configs/development.yml +5 -0
- data/spec/test_configs/empty1.yml +0 -0
- data/spec/test_configs/empty2.yml +0 -0
- metadata +87 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 YOUR NAME
|
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/README
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
== Summary
|
2
|
+
Application level configuration.
|
3
|
+
|
4
|
+
== Original Author
|
5
|
+
Christopher J. Bottaro
|
6
|
+
|
7
|
+
== Merb Port
|
8
|
+
Jacques Crocker
|
9
|
+
|
10
|
+
=== Accessing the AppConfig object
|
11
|
+
After installing this plugin, the AppConfig object will be global available. Entries are accessed via object member notation:
|
12
|
+
AppConfig.my_config_entry
|
13
|
+
Nested entries are supported:
|
14
|
+
AppConfig.my_section.some_entr
|
15
|
+
|
16
|
+
=== Common config file
|
17
|
+
Config entries defined in
|
18
|
+
Merb.root/config/app_config/settings.yml
|
19
|
+
will be available to all environments.
|
20
|
+
|
21
|
+
=== Environment specific config files
|
22
|
+
You can have environment specific config files. Environment specific config entries take precedence over common config entries.
|
23
|
+
|
24
|
+
Example development environment config file:
|
25
|
+
Merb.root/config/app_config/development.yml
|
26
|
+
|
27
|
+
Example production environment config file:
|
28
|
+
Merb.root/config/app_config/production.yml
|
29
|
+
|
30
|
+
=== Embedded Ruby (ERB)
|
31
|
+
Embedded Ruby is allowed in the configuration files. See examples below.
|
32
|
+
|
33
|
+
== Easy Stylesheets and Javascripts
|
34
|
+
You can also define your javascripts and stylesheets using AppConfig
|
35
|
+
|
36
|
+
Merb.root/config/javascripts.yml
|
37
|
+
Merb.root/config/stylesheets.yml
|
38
|
+
|
39
|
+
prototype:
|
40
|
+
- prototype
|
41
|
+
- scriptaculous
|
42
|
+
|
43
|
+
jquery:
|
44
|
+
- jquery
|
45
|
+
- jquery.ui/tabs
|
46
|
+
|
47
|
+
components:
|
48
|
+
- components/blah
|
49
|
+
- components/blah2
|
50
|
+
|
51
|
+
This will include all the javasciprt files listed when running the helper "javascripts", and on prod, it will bundle them up by key (prototype-bundle.js, jquery-bundle.js, components-bundle.js)
|
52
|
+
|
53
|
+
In your application layout, just include the calls to
|
54
|
+
<%= stylesheets %>
|
55
|
+
<%= javascripts %>
|
56
|
+
|
57
|
+
=== Examples
|
58
|
+
Consider the two following config files.
|
59
|
+
|
60
|
+
Merb.root/config/app_config.yml:
|
61
|
+
size: 1
|
62
|
+
server: google.com
|
63
|
+
|
64
|
+
Merb.root/config/app_config/development.yml:
|
65
|
+
size: 2
|
66
|
+
computed: <%= 1 + 2 + 3 %>
|
67
|
+
section:
|
68
|
+
size: 3
|
69
|
+
servers: [ {name: yahoo.com}, {name: amazon.com} ]
|
70
|
+
|
71
|
+
Notice that the environment specific config entries overwrite the common entries.
|
72
|
+
AppConfig.size -> 2
|
73
|
+
AppConfig.server -> google.com
|
74
|
+
|
75
|
+
Notice the embedded Ruby.
|
76
|
+
AppConfig.computed -> 6
|
77
|
+
|
78
|
+
Notice that object member notation is maintained even in nested entries.
|
79
|
+
AppConfig.section.size -> 3
|
80
|
+
|
81
|
+
Notice array notation and object member notation is maintained.
|
82
|
+
AppConfig.section.servers[0].name -> yahoo.com
|
83
|
+
AppConfig.section.servers[1].name -> amazon.com
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake/gempackagetask'
|
3
|
+
|
4
|
+
require 'merb-core'
|
5
|
+
require 'merb-core/tasks/merb'
|
6
|
+
|
7
|
+
GEM_NAME = "merb-plugins-app-config"
|
8
|
+
GEM_VERSION = "0.3"
|
9
|
+
AUTHOR = "Jacques Crocker"
|
10
|
+
EMAIL = "merbjedi@gmail.com"
|
11
|
+
HOMEPAGE = "http://www.merbjedi.com/"
|
12
|
+
SUMMARY = "Merb plugin that provides easy to use Application Configurations via YAML"
|
13
|
+
|
14
|
+
spec = Gem::Specification.new do |s|
|
15
|
+
s.rubyforge_project = 'merb'
|
16
|
+
s.name = GEM_NAME
|
17
|
+
s.version = GEM_VERSION
|
18
|
+
s.platform = Gem::Platform::RUBY
|
19
|
+
s.has_rdoc = true
|
20
|
+
s.extra_rdoc_files = ["README", "LICENSE"]
|
21
|
+
s.summary = SUMMARY
|
22
|
+
s.description = s.summary
|
23
|
+
s.author = AUTHOR
|
24
|
+
s.email = EMAIL
|
25
|
+
s.homepage = HOMEPAGE
|
26
|
+
s.add_dependency('merb', '>= 1.0')
|
27
|
+
s.add_dependency('merb-assets', '>= 1.0')
|
28
|
+
s.require_path = 'lib'
|
29
|
+
s.files = %w(LICENSE README Rakefile) + Dir.glob("{lib,spec}/**/*")
|
30
|
+
end
|
31
|
+
|
32
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
33
|
+
pkg.gem_spec = spec
|
34
|
+
end
|
35
|
+
|
36
|
+
desc "install the plugin as a gem"
|
37
|
+
task :install do
|
38
|
+
Merb::RakeHelper.install(GEM_NAME, :version => GEM_VERSION)
|
39
|
+
end
|
40
|
+
|
41
|
+
desc "Uninstall the gem"
|
42
|
+
task :uninstall do
|
43
|
+
Merb::RakeHelper.uninstall(GEM_NAME, :version => GEM_VERSION)
|
44
|
+
end
|
45
|
+
|
46
|
+
desc "Create a gemspec file"
|
47
|
+
task :gemspec do
|
48
|
+
File.open("#{GEM_NAME}.gemspec", "w") do |file|
|
49
|
+
file.puts spec.to_ruby
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
require 'lib/merbtasks'
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
require 'yaml'
|
3
|
+
require 'erb'
|
4
|
+
|
5
|
+
module ApplicationConfig
|
6
|
+
# == Summary
|
7
|
+
# This is API documentation, NOT documentation on how to use this plugin. For that, see the README.
|
8
|
+
class ConfigBuilder
|
9
|
+
|
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
|
+
# if the first file if they exist in the first file.
|
12
|
+
def self.load_files(*conf_load_paths)
|
13
|
+
@@conf_paths = []
|
14
|
+
@@conf_paths << conf_load_paths
|
15
|
+
@@conf_paths.flatten!.uniq!
|
16
|
+
return reload
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.reload
|
20
|
+
conf = {}
|
21
|
+
@@conf_paths.to_a.each do |path|
|
22
|
+
new_conf = YAML.load(ERB.new(IO.read(path)).result) if path and File.exists?(path)
|
23
|
+
conf.merge!(new_conf) if new_conf
|
24
|
+
end
|
25
|
+
return convert(conf)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Recursively converts Hashes to OpenStructs (including Hashes inside Arrays)
|
29
|
+
def self.convert(h) #:nodoc:
|
30
|
+
s = OpenStruct.new
|
31
|
+
h.each do |k, v|
|
32
|
+
s.new_ostruct_member(k)
|
33
|
+
if v.instance_of?(Hash)
|
34
|
+
s.send( (k+'=').to_sym, convert(v))
|
35
|
+
elsif v.instance_of?(Array)
|
36
|
+
converted_array = v.collect { |e| e.instance_of?(Hash) ? convert(e) : e }
|
37
|
+
s.send("#{k}=".to_sym, converted_array)
|
38
|
+
else
|
39
|
+
s.send("#{k}=".to_sym, v)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
s
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module ApplicationConfig
|
2
|
+
module ViewHelpers
|
3
|
+
def javascripts
|
4
|
+
js_hash = AppConfigJavascripts.marshal_dump
|
5
|
+
js_hash.each do |key, val|
|
6
|
+
require_js val, :bundle => key.to_sym
|
7
|
+
end
|
8
|
+
include_required_js
|
9
|
+
end
|
10
|
+
|
11
|
+
def stylesheets
|
12
|
+
style_hash = AppConfigStylesheets.marshal_dump
|
13
|
+
style_hash.each do |key, val|
|
14
|
+
require_css val, :bundle => key.to_sym
|
15
|
+
end
|
16
|
+
|
17
|
+
include_required_css
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'application_config/config_builder'
|
2
|
+
require 'application_config/view_helpers'
|
3
|
+
|
4
|
+
# make sure we're running inside Merb
|
5
|
+
if defined?(Merb::Plugins)
|
6
|
+
# Merb gives you a Merb::Plugins.config hash...feel free to put your stuff in your piece of it
|
7
|
+
Merb::Plugins.config[:app_config] = {
|
8
|
+
:auto_reload => false,
|
9
|
+
:include_viewhelpers => true
|
10
|
+
}
|
11
|
+
|
12
|
+
Merb::BootLoader.before_app_loads do
|
13
|
+
::AppConfig = ApplicationConfig::ConfigBuilder.load_files(Merb.root+"/config/app_config/settings.yml",
|
14
|
+
Merb.root+"/config/app_config/#{Merb.env}.yml")
|
15
|
+
|
16
|
+
if Merb::Plugins.config[:app_config][:include_viewhelpers]
|
17
|
+
::AppConfigStylesheets = ApplicationConfig::ConfigBuilder.load_files(Merb.root+"/config/app_config/stylesheets.yml")
|
18
|
+
::AppConfigJavascripts = ApplicationConfig::ConfigBuilder.load_files(Merb.root+"/config/app_config/javascripts.yml")
|
19
|
+
end
|
20
|
+
|
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
|
+
if Merb::Plugins.config[:app_config][:include_viewhelpers]
|
28
|
+
Merb::Controller.send(:include, ApplicationConfig::ViewHelpers)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Merb::BootLoader.after_app_loads do
|
33
|
+
# end
|
34
|
+
|
35
|
+
Merb::Plugins.add_rakefiles "merbtasks"
|
36
|
+
end
|
data/lib/merbtasks.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
namespace :app_config do
|
2
|
+
desc "Create a blank config/app_config.yml file"
|
3
|
+
task :init do
|
4
|
+
puts "Setting up AppConfig files..."
|
5
|
+
`mkdir -p config/app_config`
|
6
|
+
|
7
|
+
["config/app_config/settings.yml",
|
8
|
+
"config/app_config/development.yml",
|
9
|
+
"config/app_config/production.yml"
|
10
|
+
].each do |path|
|
11
|
+
`touch #{path}`
|
12
|
+
puts "Created: #{path}"
|
13
|
+
end
|
14
|
+
puts "Complete!"
|
15
|
+
puts "Add key/value pairs to your yaml file,\nthen access them in your Merb project via AppConfig.[key]"
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
require 'merb-plugins-app-config'
|
3
|
+
|
4
|
+
describe "AppConfig Plugin" do
|
5
|
+
before(:each) do
|
6
|
+
@settings_path = "spec/test_configs"
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should load a common app config with simple settings" do
|
10
|
+
config = ApplicationConfig::ConfigBuilder.load_files("#{@settings_path}/app_config.yml")
|
11
|
+
config.size.should == 1
|
12
|
+
config.server.should == "google.com"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should allow environment overrides" do
|
16
|
+
config = ApplicationConfig::ConfigBuilder.load_files("#{@settings_path}/app_config.yml", "#{@settings_path}/development.yml")
|
17
|
+
config.size.should == 2
|
18
|
+
config.server.should == "google.com"
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should allow nested sets of configurations" do
|
22
|
+
config = ApplicationConfig::ConfigBuilder.load_files("#{@settings_path}/development.yml")
|
23
|
+
config.section.size.should == 3
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should allow array definitions in settings" do
|
27
|
+
config = ApplicationConfig::ConfigBuilder.load_files("#{@settings_path}/development.yml")
|
28
|
+
config.section.servers[0].name.should == "yahoo.com"
|
29
|
+
config.section.servers[1].name.should == "amazon.com"
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should not crash on missing files" do
|
33
|
+
config = ApplicationConfig::ConfigBuilder.load_files("#{@settings_path}/nothing_here.yml", "#{@settings_path}/nothing_here2.yml")
|
34
|
+
config.should == OpenStruct.new
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should allow erb in settings" do
|
38
|
+
config = ApplicationConfig::ConfigBuilder.load_files("#{@settings_path}/development.yml")
|
39
|
+
config.computed.should == 6
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should not crash on empty files" do
|
43
|
+
config = ApplicationConfig::ConfigBuilder.load_files("#{@settings_path}/empty1.yml", "#{@settings_path}/empty2.yml")
|
44
|
+
config.should == OpenStruct.new
|
45
|
+
end
|
46
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
$:.push File.join(File.dirname(__FILE__), '..', 'lib')
|
File without changes
|
File without changes
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: merb-plugins-app-config
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.3"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jacques Crocker
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-11-15 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: merb
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "1.0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: merb-assets
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "1.0"
|
34
|
+
version:
|
35
|
+
description: Merb plugin that provides easy to use Application Configurations via YAML
|
36
|
+
email: merbjedi@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- README
|
43
|
+
- LICENSE
|
44
|
+
files:
|
45
|
+
- LICENSE
|
46
|
+
- README
|
47
|
+
- Rakefile
|
48
|
+
- lib/application_config
|
49
|
+
- lib/application_config/config_builder.rb
|
50
|
+
- lib/application_config/view_helpers.rb
|
51
|
+
- lib/merb-plugins-app-config.rb
|
52
|
+
- lib/merbtasks.rb
|
53
|
+
- spec/app_config_spec.rb
|
54
|
+
- spec/spec_helper.rb
|
55
|
+
- spec/test_configs
|
56
|
+
- spec/test_configs/app_config.yml
|
57
|
+
- spec/test_configs/development.yml
|
58
|
+
- spec/test_configs/empty1.yml
|
59
|
+
- spec/test_configs/empty2.yml
|
60
|
+
has_rdoc: true
|
61
|
+
homepage: http://www.merbjedi.com/
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: "0"
|
72
|
+
version:
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: "0"
|
78
|
+
version:
|
79
|
+
requirements: []
|
80
|
+
|
81
|
+
rubyforge_project: merb
|
82
|
+
rubygems_version: 1.3.1
|
83
|
+
signing_key:
|
84
|
+
specification_version: 2
|
85
|
+
summary: Merb plugin that provides easy to use Application Configurations via YAML
|
86
|
+
test_files: []
|
87
|
+
|