simpleconfig 1.1 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +10 -17
- data/lib/simple_config.rb +41 -41
- data/templates/configuration.rb +3 -3
- metadata +5 -4
data/README.textile
CHANGED
@@ -1,8 +1,11 @@
|
|
1
|
-
Simple Config is a plugin designed to make application-wide configuration settings easy to set and access in an
|
1
|
+
Simple Config is a plugin designed to make application-wide configuration settings easy to set and access in an
|
2
|
+
object-oriented fashion.
|
2
3
|
|
3
4
|
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
|
|
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
|
6
|
+
One simple solution is to simply put all of your app configuration into a YAML file and load this somewhere in your environment, but I 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.
|
7
|
+
|
8
|
+
SimpleConfig was originally written against Rails 1.x and may still work but as of version 1.1.1 the minimum required Rails version is 2.3.5. You may be able to use it with older versions of Rails but YMMV.
|
6
9
|
|
7
10
|
h2. Getting started
|
8
11
|
|
@@ -10,7 +13,7 @@ The plugin comes with a rake task to get you up and running quickly, so start by
|
|
10
13
|
|
11
14
|
$ rake simple_config:setup
|
12
15
|
|
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
|
16
|
+
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.
|
14
17
|
|
15
18
|
Now, if you open up the configuration.rb initializer, you will see something like this:
|
16
19
|
|
@@ -19,8 +22,8 @@ SimpleConfig.for :application do
|
|
19
22
|
|
20
23
|
# your app configuration here
|
21
24
|
|
22
|
-
load File.join(
|
23
|
-
load File.join(
|
25
|
+
load File.join(Rails.root, 'config', "settings", "#{RAILS_ENV}.rb"), :if_exists? => true
|
26
|
+
load File.join(Rails.root, 'config', "settings", "local.rb"), :if_exists? => true
|
24
27
|
|
25
28
|
end
|
26
29
|
</code></pre>
|
@@ -29,7 +32,7 @@ This is where you can set any configuration variables that are required across a
|
|
29
32
|
|
30
33
|
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
34
|
|
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[
|
35
|
+
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[1].
|
33
36
|
|
34
37
|
h2. Variables
|
35
38
|
|
@@ -145,14 +148,4 @@ class MyController < ApplicationController
|
|
145
148
|
end
|
146
149
|
</code></pre>
|
147
150
|
|
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.
|
151
|
+
fn1(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/lib/simple_config.rb
CHANGED
@@ -1,105 +1,105 @@
|
|
1
1
|
require 'yaml'
|
2
2
|
|
3
|
-
unless
|
3
|
+
unless Object.public_method_defined?(:tap)
|
4
4
|
class Object
|
5
|
-
def
|
6
|
-
yield
|
7
|
-
|
5
|
+
def tap(&block)
|
6
|
+
yield self
|
7
|
+
self
|
8
8
|
end
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
12
|
module SimpleConfig
|
13
|
-
|
13
|
+
|
14
14
|
class << self
|
15
15
|
def for(config_name, &block)
|
16
16
|
default_manager.for(config_name, &block)
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
def default_manager
|
20
20
|
@default_manager ||= Manager.new
|
21
21
|
end
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
class Manager
|
25
25
|
def initialize
|
26
26
|
@configs = {}
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
def for(config_name, &block)
|
30
|
-
|
30
|
+
(@configs[config_name] ||= Config.new).tap do |config|
|
31
31
|
config.configure(&block) if block_given?
|
32
32
|
end
|
33
33
|
end
|
34
34
|
end
|
35
|
-
|
35
|
+
|
36
36
|
class Config
|
37
37
|
def initialize
|
38
38
|
@groups = {}
|
39
39
|
@settings = {}
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
def configure(&block)
|
43
43
|
instance_eval(&block)
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
def group(name, &block)
|
47
|
-
|
47
|
+
(@groups[name] ||= Config.new).tap do |group|
|
48
48
|
group.configure(&block) if block_given?
|
49
49
|
end
|
50
50
|
end
|
51
|
-
|
51
|
+
|
52
52
|
def set(key, value)
|
53
53
|
@settings[key] = value
|
54
54
|
end
|
55
|
-
|
55
|
+
|
56
56
|
def get(key)
|
57
57
|
@settings[key]
|
58
58
|
end
|
59
|
-
|
60
|
-
#
|
61
|
-
# Unsets any variable with given +key+
|
59
|
+
|
60
|
+
#
|
61
|
+
# Unsets any variable with given +key+
|
62
62
|
# and returns variable value if it exists, nil otherwise.
|
63
63
|
# Any successive call to exists? :key will return false.
|
64
|
-
#
|
64
|
+
#
|
65
65
|
# exists? :bar # => false
|
66
|
-
#
|
66
|
+
#
|
67
67
|
# set :bar, 'foo'
|
68
68
|
# exists? :bar # => true
|
69
|
-
#
|
69
|
+
#
|
70
70
|
# unset :bar # => 'foo'
|
71
71
|
# exists? :bar # => false
|
72
|
-
#
|
72
|
+
#
|
73
73
|
def unset(key)
|
74
74
|
@settings.delete(key)
|
75
75
|
end
|
76
|
-
|
77
|
-
#
|
76
|
+
|
77
|
+
#
|
78
78
|
# Returns whether a variable with given +key+ is set.
|
79
|
-
#
|
79
|
+
#
|
80
80
|
# Please note that this method doesn't care about variable value.
|
81
81
|
# A nil variable is considered as set.
|
82
|
-
#
|
82
|
+
#
|
83
83
|
# exists? :bar # => false
|
84
|
-
#
|
84
|
+
#
|
85
85
|
# set :bar, 'foo'
|
86
86
|
# exists? :bar # => true
|
87
|
-
#
|
87
|
+
#
|
88
88
|
# set :bar, nil
|
89
89
|
# exists? :bar # => true
|
90
|
-
#
|
90
|
+
#
|
91
91
|
# Use unset to completely remove a variable from the collection.
|
92
|
-
#
|
92
|
+
#
|
93
93
|
# set :bar, 'foo'
|
94
94
|
# exists? :bar # => true
|
95
|
-
#
|
95
|
+
#
|
96
96
|
# unset :bar
|
97
97
|
# exists? :bar # => false
|
98
|
-
#
|
98
|
+
#
|
99
99
|
def exists?(key)
|
100
100
|
@settings.key?(key)
|
101
101
|
end
|
102
|
-
|
102
|
+
|
103
103
|
def to_hash
|
104
104
|
hash = @settings.dup
|
105
105
|
@groups.each do |key,group|
|
@@ -110,11 +110,11 @@ module SimpleConfig
|
|
110
110
|
|
111
111
|
def load(external_config_file, options={})
|
112
112
|
options = {:if_exists? => false}.merge(options)
|
113
|
-
|
113
|
+
|
114
114
|
if options[:if_exists?]
|
115
115
|
return unless File.exist?(external_config_file)
|
116
116
|
end
|
117
|
-
|
117
|
+
|
118
118
|
case File.extname(external_config_file)
|
119
119
|
when /rb/
|
120
120
|
instance_eval(File.read(external_config_file))
|
@@ -122,7 +122,7 @@ module SimpleConfig
|
|
122
122
|
YAMLParser.parse_contents_of_file(external_config_file).parse_into(self)
|
123
123
|
end
|
124
124
|
end
|
125
|
-
|
125
|
+
|
126
126
|
private
|
127
127
|
def method_missing(method_name, *args)
|
128
128
|
case true
|
@@ -135,22 +135,22 @@ module SimpleConfig
|
|
135
135
|
end
|
136
136
|
end
|
137
137
|
end
|
138
|
-
|
138
|
+
|
139
139
|
class YAMLParser
|
140
140
|
def initialize(raw_yaml_data)
|
141
141
|
@data = YAML.load(raw_yaml_data)
|
142
142
|
end
|
143
|
-
|
143
|
+
|
144
144
|
def self.parse_contents_of_file(yaml_file)
|
145
145
|
new(File.read(yaml_file))
|
146
146
|
end
|
147
|
-
|
147
|
+
|
148
148
|
def parse_into(config)
|
149
149
|
@data.each do |key, value|
|
150
150
|
parse(key, value, config)
|
151
151
|
end
|
152
152
|
end
|
153
|
-
|
153
|
+
|
154
154
|
private
|
155
155
|
def parse(key, value, config)
|
156
156
|
if value.is_a?(Hash)
|
@@ -161,5 +161,5 @@ module SimpleConfig
|
|
161
161
|
end
|
162
162
|
end
|
163
163
|
end
|
164
|
-
|
164
|
+
|
165
165
|
end
|
data/templates/configuration.rb
CHANGED
@@ -2,17 +2,17 @@ SimpleConfig.for :application do
|
|
2
2
|
|
3
3
|
# Set here your global configuration.
|
4
4
|
# All settings can be overwritten later per-environment.
|
5
|
-
load File.join(
|
5
|
+
load File.join(Rails.root.to_s, 'config', "settings", "application.rb"), :if_exists? => true
|
6
6
|
|
7
7
|
# Per Environment settings.
|
8
8
|
# At startup only the file matching current environment will be loaded.
|
9
9
|
# Settings stored here will overwrite settings with the same name stored in application.rb
|
10
|
-
load File.join(
|
10
|
+
load File.join(Rails.root.to_s, 'config', "settings", "#{RAILS_ENV}.rb"), :if_exists? => true
|
11
11
|
|
12
12
|
# Local settings. It is designed as a place for you to override variables
|
13
13
|
# specific to your own development environment.
|
14
14
|
# Make sure your version control system ignores this file otherwise
|
15
15
|
# you risk checking in a file that will override values in production
|
16
|
-
load File.join(
|
16
|
+
load File.join(Rails.root.to_s, 'config', "settings", "local.rb"), :if_exists? => true
|
17
17
|
|
18
18
|
end
|
metadata
CHANGED
@@ -1,20 +1,21 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simpleconfig
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 17
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 1
|
9
|
-
|
9
|
+
- 1
|
10
|
+
version: 1.1.1
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Luke Redpath
|
13
|
-
autorequire:
|
14
|
+
autorequire: simpleconfig
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2010-07-
|
18
|
+
date: 2010-07-31 00:00:00 +01:00
|
18
19
|
default_executable:
|
19
20
|
dependencies: []
|
20
21
|
|