lukeredpath-simpleconfig 1.0 → 1.0.1
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.textile +158 -0
- data/Rakefile +22 -0
- data/lib/rails_compatibility.rb +7 -0
- data/lib/simple_config/utilities.rb +2 -2
- data/lib/simple_config.rb +10 -1
- data/rails/init.rb +5 -0
- data/tasks/simple_config.rake +22 -0
- data/templates/configuration.rb +8 -0
- metadata +8 -2
data/README.textile
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
Simple Config is a plugin designed to make application-wide configuration settings easy to set and access in an object-oriented fashion.
|
2
|
+
|
3
|
+
Rails already provides a way of configuring the framework on a per-environment basis but other than global variables/constants set in each environment file or environment.rb, there isn't a built-in way of providing application-specific settings.
|
4
|
+
|
5
|
+
One simple solution is to simply put all of your app configuration into a YAML file and load this somewhere in your environment, but here at Reevoo we wanted something a little bit more flexible that we could use across all of our applications and Simple Config is what we came up with.
|
6
|
+
|
7
|
+
h2. Getting started
|
8
|
+
|
9
|
+
The plugin comes with a rake task to get you up and running quickly, so start by running that.
|
10
|
+
|
11
|
+
$ rake simple_config:setup
|
12
|
+
|
13
|
+
This will create a config/settings folder and a blank settings file for each of the main Rails environments. It will also create a copy of the SimpleConfig initializer in config/initializers/configuration.rb[1].
|
14
|
+
|
15
|
+
Now, if you open up the configuration.rb initializer, you will see something like this:
|
16
|
+
|
17
|
+
<pre><code class="ruby">
|
18
|
+
SimpleConfig.for :application do
|
19
|
+
|
20
|
+
# your app configuration here
|
21
|
+
|
22
|
+
load File.join(RAILS_ROOT, 'config', "settings", "#{RAILS_ENV}.rb"), :if_exists? => true
|
23
|
+
load File.join(RAILS_ROOT, 'config', "settings", "local.rb"), :if_exists? => true
|
24
|
+
|
25
|
+
end
|
26
|
+
</code></pre>
|
27
|
+
|
28
|
+
This is where you can set any configuration variables that are required across all Rails environments. The <code>load</code> method works just like Ruby's built-in load method, except the contents of the file it loads are evaluated within the context of the <code>SimpleConfig.for</code> block. The <code>:if_exists?</code> flag, when set to true, means that the file will only be loaded if it exists, otherwise it will simply be ignored.
|
29
|
+
|
30
|
+
Variables can be overwritten, and are defined in the order that they are loaded, so you can set up default values in the above file and override them in the environment files.
|
31
|
+
|
32
|
+
As well as loading a settings file for your current Rails environment, a file called "local.rb" is loaded which is designed as a place for you to override variables specific to your own development environment -- you can just keep a copy of this locally without having to check it into your version control system[2].
|
33
|
+
|
34
|
+
h2. Variables
|
35
|
+
|
36
|
+
h3. Setting Variables
|
37
|
+
|
38
|
+
Setting variables is simple and will be familiar to anybody who has used Capistrano. Whether in your main <code>SimpleConfig.for</code> block in configuration.rb, or one of your external settings files, use the <code>set</code> method:
|
39
|
+
|
40
|
+
<pre><code class="ruby">
|
41
|
+
SimpleConfig.for :application do
|
42
|
+
set :my_variable, 'hello world'
|
43
|
+
end
|
44
|
+
</code></pre>
|
45
|
+
|
46
|
+
SimpleConfig also supports a form of namespacing that allows you to group logical sets of variables together:
|
47
|
+
|
48
|
+
<pre><code class="ruby">
|
49
|
+
SimpleConfig.for :application do
|
50
|
+
group :awesome_stuff do
|
51
|
+
set :my_variable, 'hello world'
|
52
|
+
end
|
53
|
+
end
|
54
|
+
</code></pre>
|
55
|
+
|
56
|
+
Both the <code>set</code> and <code>load</code> methods are available within <code>group</code> blocks and files loaded inside groups will be evaluated in the context of that group.
|
57
|
+
|
58
|
+
Whilst I'd recommend not nesting your groups more than one-level, there is no limit on how deep they can be nested.
|
59
|
+
|
60
|
+
h3. Unsetting variables
|
61
|
+
|
62
|
+
Sometimes you might want to completely delete a variable from the collection. Simply setting its value to nil doesn't work because <code>nil</code> might be a valid value.
|
63
|
+
You can delete a variable using the <code>unset</code> method.
|
64
|
+
|
65
|
+
<pre><code class="ruby">
|
66
|
+
SimpleConfig.for :application do
|
67
|
+
set :my_variable, 'hello world'
|
68
|
+
|
69
|
+
...
|
70
|
+
|
71
|
+
unset :my_variable
|
72
|
+
end
|
73
|
+
</code></pre>
|
74
|
+
|
75
|
+
For instance, this is useful to remove global settings at environment level instead of overwriting the default value with a nonsense-one.
|
76
|
+
<code>unset</code> returns the value of the variable just in case you need to use it elsewhere.
|
77
|
+
|
78
|
+
h3. Does a specific variable exist?
|
79
|
+
|
80
|
+
I don't know but you can check it yourself using <code>exists?</code> method.
|
81
|
+
|
82
|
+
<pre><code class="ruby">
|
83
|
+
config = SimpleConfig.for :application do
|
84
|
+
set :my_variable, 'hello world'
|
85
|
+
end
|
86
|
+
|
87
|
+
# write some nice code
|
88
|
+
config.exists? :my_variable # => true
|
89
|
+
config.exists? :your_variable # => false
|
90
|
+
</code></pre>
|
91
|
+
|
92
|
+
h2. Accessing your configuration
|
93
|
+
|
94
|
+
SimpleConfig allows you set as many separate configurations as you like using the <code>SimpleConfig.for</code> method, which takes a symbol representing the configuration name, although most people will just create a single "application" config as above. To access this config from anywhere in your application, you can also use <code>SimpleConfig.for</code> method without a block, which always returns the named configuration object.
|
95
|
+
|
96
|
+
It is worth pointing out that <code>SimpleConfig.for</code> provides an almost singleton-style access to a particular named config. Calling <code>SimpleConfig.for</code> with a block a second time for a particular named configuration will simply extend the existing configuration, not overwrite it.
|
97
|
+
|
98
|
+
Once you have a reference to your configuration object, you can access variables using method access. Given the above example, <code>:my_variable</code> would be accessed in the following way:
|
99
|
+
|
100
|
+
<pre><code class="ruby">
|
101
|
+
config = SimpleConfig.for(:application)
|
102
|
+
config.my_variable # => "hello world"
|
103
|
+
</code></pre>
|
104
|
+
|
105
|
+
Accessing grouped variables works as you would expect:
|
106
|
+
|
107
|
+
<pre><code class="ruby">
|
108
|
+
config = SimpleConfig.for(:application)
|
109
|
+
config.awesome_stuff.my_variable # => "hello world"
|
110
|
+
</code></pre>
|
111
|
+
|
112
|
+
h2. Using your configuration in your Rails app
|
113
|
+
|
114
|
+
The plugin provides a convenient mixin for your <code>ApplicationController</code> to make configuration access as simple as possible. Assuming a configuration called "application" (as in the above examples), it defines a <code>config</code> method which can be used in any of your controllers. It also defines this as a method as a view helper using the Rails <code>helper_method</code> macro so you can access configuration data in your views.
|
115
|
+
|
116
|
+
Note - there is no direct way of accessing your configuration variables in your models other than making a direct call to <code>SimpleConfig.for</code>. I'd recommend designing your models in such a way that configuration data can be passed into them at runtime as method arguments by your controller to avoid coupling your model to SimpleConfig.
|
117
|
+
|
118
|
+
To use the mixin, simply include it in your <code>ApplicationController</code>:
|
119
|
+
|
120
|
+
<pre><code class="ruby">
|
121
|
+
class ApplicationController < ActionController::Base
|
122
|
+
include SimpleConfig::ControllerMixin
|
123
|
+
end
|
124
|
+
</code></pre>
|
125
|
+
|
126
|
+
Then in your controllers:
|
127
|
+
|
128
|
+
<pre><code class="ruby">
|
129
|
+
class MyController < ApplicationController
|
130
|
+
def index
|
131
|
+
render :text => config.my_config_variable
|
132
|
+
end
|
133
|
+
end
|
134
|
+
</code></pre>
|
135
|
+
|
136
|
+
The mixin provides also a class-level <code>config</code> method to access the configuration when you don't have a controller instance available.
|
137
|
+
|
138
|
+
<pre><code class="ruby">
|
139
|
+
class MyController < ApplicationController
|
140
|
+
protect_from_forgery :secret => config.secret_token
|
141
|
+
|
142
|
+
def index
|
143
|
+
render :text => config.my_config_variable
|
144
|
+
end
|
145
|
+
end
|
146
|
+
</code></pre>
|
147
|
+
|
148
|
+
|
149
|
+
fn1(footnote). SimpleConfig was designed with Rails 2.0 in mind but it has been tested with Rails 1.2. To use the Rails-style initializers that SimpleConfig takes advantage of in Rails 1.2, simply add this to the bottom of your environment.rb file:
|
150
|
+
|
151
|
+
<pre><code class="ruby">
|
152
|
+
# backported Rails 2.x initializer folder functionality
|
153
|
+
Dir[File.join(RAILS_ROOT, 'config', 'initializers', '*.rb')].each do |initializer|
|
154
|
+
load initializer
|
155
|
+
end
|
156
|
+
</code></pre>
|
157
|
+
|
158
|
+
fn2(footnote). In fact, I recommend you make sure your version control system ignores this file otherwise you risk checking in a file that will override values in production! If you are using Subversion, simply add local.rb to the svn:ignore property for the config/settings folder.
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the simpleconfig plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the simpleconfig plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'Simpleconfig'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
@@ -20,8 +20,8 @@ module SimpleConfig
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def to_uri(uri_options = {})
|
23
|
-
|
24
|
-
URI::Generic.build(
|
23
|
+
[:host, :port].each { |opt| uri_options.delete(opt) }
|
24
|
+
URI::Generic.build({:host => name, :port => port, :scheme => default_uri_scheme}.merge(uri_options))
|
25
25
|
end
|
26
26
|
|
27
27
|
def url_for_path(path)
|
data/lib/simple_config.rb
CHANGED
@@ -1,5 +1,14 @@
|
|
1
1
|
require 'yaml'
|
2
2
|
|
3
|
+
unless defined?(Rails)
|
4
|
+
class Object
|
5
|
+
def returning(object, &block)
|
6
|
+
yield object if block_given?
|
7
|
+
object
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
3
12
|
module SimpleConfig
|
4
13
|
|
5
14
|
class << self
|
@@ -92,7 +101,7 @@ module SimpleConfig
|
|
92
101
|
end
|
93
102
|
|
94
103
|
def load(external_config_file, options={})
|
95
|
-
options
|
104
|
+
options = {:if_exists? => false}.merge(options)
|
96
105
|
|
97
106
|
if options[:if_exists?]
|
98
107
|
return unless File.exist?(external_config_file)
|
data/rails/init.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
namespace :simple_config do
|
4
|
+
include FileUtils
|
5
|
+
|
6
|
+
task :setup do
|
7
|
+
raise "This task should be run from within a Rails application." unless File.exist?('config')
|
8
|
+
raise "Already found config/settings. Have you already run this task?." if File.exist?('config/settings')
|
9
|
+
|
10
|
+
mkdir('config/settings')
|
11
|
+
mkdir("config/initializers") unless File.exist?("config/initializers")
|
12
|
+
|
13
|
+
Dir["config/environments/*.rb"].each do |f|
|
14
|
+
env = File.basename(f, ".rb")
|
15
|
+
touch("config/settings/#{env}.rb")
|
16
|
+
end
|
17
|
+
|
18
|
+
cp(File.join(File.dirname(__FILE__), *%w[.. templates configuration.rb]),
|
19
|
+
"config/initializers/configuration.rb")
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
metadata
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lukeredpath-simpleconfig
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Luke Redpath
|
8
|
-
autorequire:
|
8
|
+
autorequire: simple_config
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
@@ -25,6 +25,12 @@ files:
|
|
25
25
|
- lib/simple_config.rb
|
26
26
|
- lib/controller_mixin.rb
|
27
27
|
- lib/simple_config/utilities.rb
|
28
|
+
- lib/rails_compatibility.rb
|
29
|
+
- rails/init.rb
|
30
|
+
- Rakefile
|
31
|
+
- README.textile
|
32
|
+
- tasks/simple_config.rake
|
33
|
+
- templates/configuration.rb
|
28
34
|
has_rdoc: false
|
29
35
|
homepage: http://github.com/lukeredpath/simpleconfig
|
30
36
|
post_install_message:
|