configatron 2.8.1 → 2.8.2
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 → README.textile} +58 -20
- data/lib/configatron/configatron.rb +16 -1
- data/lib/configatron/store.rb +3 -2
- metadata +4 -4
data/{README → README.textile}
RENAMED
@@ -1,89 +1,112 @@
|
|
1
|
-
|
1
|
+
h1. Configatron
|
2
2
|
|
3
3
|
Configatron makes configuring your applications and scripts incredibly easy. No longer is a there a need to use constants or global variables. Now you can use a simple and painless system to configure your life. And, because it's all Ruby, you can do any crazy thing you would like to!
|
4
4
|
|
5
|
-
|
5
|
+
h2. Installation
|
6
6
|
|
7
7
|
Installation of Configatron is easy, as it is just a RubyGem:
|
8
8
|
|
9
|
+
<pre><code>
|
9
10
|
$ sudo gem install configatron
|
11
|
+
</code></pre>
|
10
12
|
|
11
13
|
If you'd like to live on the bleedin' edge you can install the development version from GitHub:
|
12
14
|
|
15
|
+
<pre><code>
|
13
16
|
$ sudo gem install markbates-configatron --source=http://gems.github.com
|
17
|
+
</code></pre>
|
14
18
|
|
15
19
|
Once installed you just need to require it:
|
16
20
|
|
21
|
+
<pre><code>
|
17
22
|
require 'configatron'
|
23
|
+
</code></pre>
|
18
24
|
|
19
|
-
|
25
|
+
h2. Examples
|
20
26
|
|
21
|
-
|
27
|
+
h3. Simple
|
22
28
|
|
29
|
+
<pre><code>
|
23
30
|
configatron.email = 'me@example.com'
|
24
31
|
configatron.database_url = "postgres://localhost/mack_framework_rocks"
|
32
|
+
</code></pre>
|
25
33
|
|
26
34
|
Now, anywhere in your code you can do the following:
|
27
35
|
|
36
|
+
<pre><code>
|
28
37
|
configatron.email # => "me@example.com"
|
29
38
|
configatron.database_url # => "postgres://localhost/mack_framework_rocks"
|
39
|
+
</code></pre>
|
30
40
|
|
31
41
|
Viola! Simple as can be.
|
32
42
|
|
33
|
-
Now you're saying, what if I want to have a 'default' set of options, but then override them later, based on other information? Simple again. Let's use our above example. We've configured our
|
43
|
+
Now you're saying, what if I want to have a 'default' set of options, but then override them later, based on other information? Simple again. Let's use our above example. We've configured our @database_url@ option to be @postgres://localhost/mack_framework_rocks@. The problem with that is that is our production database url, not our development url. Fair enough, all you have to do is redeclare it:
|
34
44
|
|
45
|
+
<pre><code>
|
35
46
|
configatron.database_url = "postgres://localhost/mack_framework_rocks_development"
|
47
|
+
</code></pre>
|
36
48
|
|
37
49
|
becomes:
|
38
50
|
|
51
|
+
<pre><code>
|
39
52
|
configatron.email # => "me@example.com"
|
40
53
|
configatron.database_url # => "postgres://localhost/mack_framework_rocks_development"
|
54
|
+
</code></pre>
|
41
55
|
|
42
56
|
Notice how our other configuration parameters haven't changed? Cool, eh?
|
43
57
|
|
44
|
-
|
58
|
+
h3. Hash/YAML
|
45
59
|
|
46
|
-
You can configure configatron from a hash as well:
|
60
|
+
You can configure configatron from a hash as well (this is really only useful in testing or for data driven configurat, it's not recommended for actual configuration):
|
47
61
|
|
62
|
+
<pre><code>
|
48
63
|
configatron.configure_from_hash({:email => {:pop => {:address => 'pop.example.com', :port => 110}}, :smtp => {:address => 'smtp.example.com'}})
|
49
64
|
|
50
65
|
configatron.email.pop.address # => 'pop.example.com'
|
51
66
|
configatron.email.pop.port # => 110
|
52
67
|
# and so on...
|
68
|
+
</code></pre>
|
53
69
|
|
54
|
-
|
70
|
+
h4. YAML
|
55
71
|
|
56
|
-
|
72
|
+
Support for YAML has been deprecated and will be removed in version 2.9 of Configatron. Please switch to Ruby based configuration of Configatron. Trust me, it's a lot nicer and easier to use. Why would you _not_ want to?
|
57
73
|
|
58
|
-
|
74
|
+
h3. Namespaces
|
59
75
|
|
60
76
|
The question that should be on your lips is what I need to have namespaced configuration parameters. It's easy! Configatron allows you to create namespaces.
|
61
77
|
|
78
|
+
<pre><code>
|
62
79
|
configatron.website_url = "http://www.mackframework.com"
|
63
80
|
configatron.email.pop.address = "pop.example.com"
|
64
81
|
configatron.email.pop.port = 110
|
65
82
|
configatron.email.smtp.address = "smtp.example.com"
|
66
83
|
configatron.email.smtp.port = 25
|
84
|
+
</code></pre>
|
67
85
|
|
68
86
|
becomes:
|
69
87
|
|
88
|
+
<pre><code>
|
70
89
|
configatron.email.pop.address # => "pop.example.com"
|
71
90
|
configatron.email.smtp.address # => "smtp.example.com"
|
72
91
|
configatron.website_url # => "http://www.mackframework.com"
|
92
|
+
</code></pre>
|
73
93
|
|
74
94
|
Configatron allows you to nest namespaces to your hearts content! Just keep going, it's that easy.
|
75
95
|
|
76
96
|
Of course you can update a single parameter n levels deep as well:
|
77
97
|
|
98
|
+
<pre><code>
|
78
99
|
configatron.email.pop.address = "pop2.example.com"
|
79
100
|
|
80
101
|
configatron.email.pop.address # => "pop2.example.com"
|
81
102
|
configatron.email.smtp.address # => "smtp.example.com"
|
103
|
+
</code></pre>
|
82
104
|
|
83
|
-
|
105
|
+
h3. Temp Configurations
|
84
106
|
|
85
|
-
Sometimes in testing, or other situations, you want to temporarily change some settings. You can do this with the
|
107
|
+
Sometimes in testing, or other situations, you want to temporarily change some settings. You can do this with the @temp@ method:
|
86
108
|
|
109
|
+
<pre><code>
|
87
110
|
configatron.one = 1
|
88
111
|
configatron.letters.a = 'A'
|
89
112
|
configatron.letters.b = 'B'
|
@@ -99,9 +122,11 @@ Sometimes in testing, or other situations, you want to temporarily change some s
|
|
99
122
|
configatron.letters.a # => 'A'
|
100
123
|
configatron.letters.b # => 'B'
|
101
124
|
configatron.letters.c # => nil
|
125
|
+
</code></pre>
|
102
126
|
|
103
|
-
You can also pass in an optional Hash to the
|
127
|
+
You can also pass in an optional Hash to the @temp@:
|
104
128
|
|
129
|
+
<pre><code>
|
105
130
|
configatron.one = 1
|
106
131
|
configatron.letters.a = 'A'
|
107
132
|
configatron.letters.b = 'B'
|
@@ -115,11 +140,13 @@ You can also pass in an optional Hash to the <tt>temp</tt>:
|
|
115
140
|
configatron.letters.a # => 'A'
|
116
141
|
configatron.letters.b # => 'B'
|
117
142
|
configatron.letters.c # => nil
|
143
|
+
</code></pre>
|
118
144
|
|
119
|
-
|
145
|
+
h3. Delayed and Dynamic Configurations
|
120
146
|
|
121
147
|
There are times when you want to refer to one configuration setting in another configuration setting. Let's look at a fairly contrived example:
|
122
148
|
|
149
|
+
<pre><code>
|
123
150
|
configatron.memcached.servers = ['127.0.0.1:11211']
|
124
151
|
configatron.page_caching.servers = configatron.memcached.servers
|
125
152
|
configatron.object_caching.servers = configatron.memcached.servers
|
@@ -133,13 +160,15 @@ There are times when you want to refer to one configuration setting in another c
|
|
133
160
|
configatron.page_caching.servers = configatron.memcached.servers
|
134
161
|
configatron.object_caching.servers = configatron.memcached.servers
|
135
162
|
end
|
163
|
+
</code></pre>
|
136
164
|
|
137
165
|
Now, we could've written that slightly differently, but it helps to illustrate the point. With Configatron you can create <code>Delayed</code> and <code>Dynamic</code> settings.
|
138
166
|
|
139
|
-
|
167
|
+
h4. Delayed
|
140
168
|
|
141
169
|
With <code>Delayed</code> settings execution of the setting doesn't happen until the first time it is executed.
|
142
170
|
|
171
|
+
<pre><code>
|
143
172
|
configatron.memcached.servers = ['127.0.0.1:11211']
|
144
173
|
configatron.page_caching.servers = Configatron::Delayed.new {configatron.memcached.servers}
|
145
174
|
configatron.object_caching.servers = Configatron::Delayed.new {configatron.memcached.servers}
|
@@ -149,40 +178,49 @@ With <code>Delayed</code> settings execution of the setting doesn't happen until
|
|
149
178
|
elsif Rails.env == 'staging'
|
150
179
|
configatron.memcached.servers = ['192.168.0.2:11211']
|
151
180
|
end
|
181
|
+
</code></pre>
|
152
182
|
|
153
183
|
Execution occurs once and after that the result of that execution is returned. So in our case the first time someone calls the setting <code>configatron.page_caching.servers</code> it will find the <code>configatron.memcached.servers</code> setting and return that. After that point if the <code>configatron.memcached.servers</code> setting is changed, the original settings are returned by <code>configatron.page_caching.servers</code>.
|
154
184
|
|
155
|
-
|
185
|
+
h4. Dynamic
|
156
186
|
|
157
187
|
<code>Dynamic</code> settings are very similar to <code>Delayed</code> settings, but with one big difference. Every time you call a <code>Dynamic</code> setting is executed. Take this example:
|
158
188
|
|
189
|
+
<pre><code>
|
159
190
|
configatron.current.time = Configatron::Dynamic.new {Time.now}
|
191
|
+
</code></pre>
|
160
192
|
|
161
193
|
Each time you call <code>configatron.current.time</code> it will return a new value to you. While this seems a bit useless, it is pretty useful if you have ever changing configurations.
|
162
194
|
|
163
|
-
|
195
|
+
h3. Misc.
|
164
196
|
|
165
|
-
Even if parameters haven't been set, you can still call them, but you'll get a
|
197
|
+
Even if parameters haven't been set, you can still call them, but you'll get a @Configatron::Store@ object back. The Configatron::Store class, however, will respond true to @.nil?@ if there are no parameters configured on it.
|
166
198
|
|
199
|
+
<pre><code>
|
167
200
|
configatron.i.dont.exist.nil? # => true
|
168
201
|
configatron.i.dont.exist # => Configatron::Store
|
202
|
+
</code></pre>
|
169
203
|
|
170
|
-
If you want to get back an actual
|
204
|
+
If you want to get back an actual @nil@ then you can use the @retrieve@ method:
|
171
205
|
|
206
|
+
<pre><code>
|
172
207
|
configatron.i.do.exist = [:some, :array]
|
173
208
|
configatron.i.dont.retrieve(:exist, nil) # => nil
|
174
209
|
configatron.i.do.retrieve(:exist, :foo) # => [:some, :array]
|
210
|
+
</code></pre>
|
175
211
|
|
176
212
|
You can set 'default' values for parameters. If there is already a setting, it won't be replaced. This is useful if you've already done your 'configuration' and you call a library, that needs to have parameters set. The library can set its defaults, without worrying that it might have overridden your custom settings.
|
177
213
|
|
214
|
+
<pre><code>
|
178
215
|
configatron.set_default(:name, 'Mark Bates')
|
179
216
|
configatron.name # => 'Mark Bates'
|
180
217
|
configatron.set_default(:name, 'Me')
|
181
218
|
configatron.name # => 'Mark Bates'
|
219
|
+
</code></pre>
|
182
220
|
|
183
221
|
Enjoy!
|
184
222
|
|
185
|
-
|
223
|
+
h2. Contact
|
186
224
|
|
187
225
|
Please mail bugs, suggestions and patches to "development@metabates.com":mailto:development@metabates.com
|
188
226
|
|
@@ -1,15 +1,30 @@
|
|
1
1
|
require 'singleton'
|
2
|
+
require 'logger'
|
2
3
|
|
3
4
|
class Configatron
|
4
5
|
include Singleton
|
5
6
|
|
6
7
|
alias_method :send!, :send
|
8
|
+
|
9
|
+
class << self
|
10
|
+
|
11
|
+
def log
|
12
|
+
unless @logger
|
13
|
+
if defined?(::Rails)
|
14
|
+
@logger = ::Rails.logger
|
15
|
+
end
|
16
|
+
@logger = ::Logger.new(STDOUT) if @logger.nil?
|
17
|
+
end
|
18
|
+
return @logger
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
7
22
|
|
8
23
|
def initialize # :nodoc:
|
9
24
|
@_namespace = [:default]
|
10
25
|
reset!
|
11
26
|
end
|
12
|
-
|
27
|
+
|
13
28
|
# Forwards the method call onto the 'namespaced' Configatron::Store
|
14
29
|
def method_missing(sym, *args, &block)
|
15
30
|
@_store[@_namespace.last].send(sym, *args, &block)
|
data/lib/configatron/store.rb
CHANGED
@@ -99,6 +99,7 @@ class Configatron
|
|
99
99
|
# <tt>:hash</tt>, that indicates a specific hash that should be
|
100
100
|
# loaded from the file.
|
101
101
|
def configure_from_yaml(path, opts = {})
|
102
|
+
Configatron.log.warn "DEPRECATED! (configure_from_yaml) Please stop using YAML and use Ruby instead. This method will be removed in 2.9."
|
102
103
|
begin
|
103
104
|
yml = ::Yamler.load(path)
|
104
105
|
yml = yml[opts[:hash]] unless opts[:hash].nil?
|
@@ -303,12 +304,12 @@ class Configatron
|
|
303
304
|
options.each do |k,v|
|
304
305
|
if v.is_a?(Hash)
|
305
306
|
if v.keys.length == 1 && v.keys.first.is_a?(SYCK_CONSTANT)
|
306
|
-
self.method_missing("#{k
|
307
|
+
self.method_missing("#{k}=", v.values.first.flatten)
|
307
308
|
else
|
308
309
|
self.method_missing(k.to_sym).configure_from_hash(v)
|
309
310
|
end
|
310
311
|
else
|
311
|
-
self.method_missing("#{k
|
312
|
+
self.method_missing("#{k}=", v)
|
312
313
|
end
|
313
314
|
end
|
314
315
|
else
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: configatron
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 2.8.
|
5
|
+
version: 2.8.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- markbates
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-06-
|
13
|
+
date: 2011-06-28 00:00:00 -04:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -43,7 +43,7 @@ files:
|
|
43
43
|
- lib/configatron/rails.rb
|
44
44
|
- lib/configatron/store.rb
|
45
45
|
- lib/configatron.rb
|
46
|
-
- README
|
46
|
+
- README.textile
|
47
47
|
- LICENSE
|
48
48
|
- generators/configatron_generator.rb
|
49
49
|
- generators/templates/configatron/cucumber.rb
|
@@ -66,7 +66,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
66
66
|
requirements:
|
67
67
|
- - ">="
|
68
68
|
- !ruby/object:Gem::Version
|
69
|
-
hash:
|
69
|
+
hash: 3677131604948269757
|
70
70
|
segments:
|
71
71
|
- 0
|
72
72
|
version: "0"
|