configatron 2.9.1 → 2.10.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ doc
2
+ *.gem
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+
19
+ .DS_Store
20
+ *.rbc
21
+ *.log
data/.rvmrc ADDED
@@ -0,0 +1,2 @@
1
+ # rvm use ruby-2.0.0-preview1
2
+ rvm use 1.9.3
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - rbx-19mode
6
+ - 2.0.0
7
+
8
+ script: bundle exec rspec
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bar.gemspec
4
+ gemspec
5
+
6
+ gem 'rake'
7
+ gem 'rspec'
@@ -1,112 +1,110 @@
1
- h1. Configatron
1
+ # Configatron [![Build Status](https://travis-ci.org/markbates/configatron.png?branch=master)](https://travis-ci.org/markbates/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
- h2. Installation
5
+ ## Installation
6
6
 
7
7
  Installation of Configatron is easy, as it is just a RubyGem:
8
8
 
9
- <pre><code>
9
+ ```ruby
10
10
  $ sudo gem install configatron
11
- </code></pre>
11
+ ```
12
12
 
13
13
  If you'd like to live on the bleedin' edge you can install the development version from GitHub:
14
14
 
15
- <pre><code>
15
+ ```
16
16
  $ sudo gem install markbates-configatron --source=http://gems.github.com
17
- </code></pre>
17
+ ```
18
18
 
19
19
  Once installed you just need to require it:
20
20
 
21
- <pre><code>
21
+ ```ruby
22
22
  require 'configatron'
23
- </code></pre>
23
+ ```
24
24
 
25
- h2. Examples
25
+ ## Examples
26
26
 
27
- h3. Simple
27
+ ### Simple
28
28
 
29
- <pre><code>
29
+ ```ruby
30
30
  configatron.email = 'me@example.com'
31
31
  configatron.database_url = "postgres://localhost/mack_framework_rocks"
32
- </code></pre>
32
+ ```
33
33
 
34
34
  Now, anywhere in your code you can do the following:
35
35
 
36
- <pre><code>
36
+ ```ruby
37
37
  configatron.email # => "me@example.com"
38
38
  configatron.database_url # => "postgres://localhost/mack_framework_rocks"
39
- </code></pre>
39
+ ```
40
40
 
41
41
  Viola! Simple as can be.
42
42
 
43
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:
44
44
 
45
- <pre><code>
45
+ ```ruby
46
46
  configatron.database_url = "postgres://localhost/mack_framework_rocks_development"
47
- </code></pre>
47
+ ```
48
48
 
49
49
  becomes:
50
50
 
51
- <pre><code>
51
+ ```ruby
52
52
  configatron.email # => "me@example.com"
53
53
  configatron.database_url # => "postgres://localhost/mack_framework_rocks_development"
54
- </code></pre>
54
+ ```
55
55
 
56
56
  Notice how our other configuration parameters haven't changed? Cool, eh?
57
57
 
58
- h3. Hash/YAML
58
+ ### Hash/YAML
59
59
 
60
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):
61
61
 
62
- <pre><code>
63
- configatron.configure_from_hash({:email => {:pop => {:address => 'pop.example.com', :port => 110}}, :smtp => {:address => 'smtp.example.com'}})
64
-
62
+ ```ruby
63
+ configatron.configure_from_hash({:email => {:pop => {:address => 'pop.example.com', :port => 110}}, :smtp => {:address => 'smtp.example.com'}})####
65
64
  configatron.email.pop.address # => 'pop.example.com'
66
65
  configatron.email.pop.port # => 110
67
66
  # and so on...
68
- </code></pre>
67
+ ```
69
68
 
70
- h4. YAML
69
+ #### YAML
71
70
 
72
71
  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?
73
72
 
74
- h3. Namespaces
73
+ ### Namespaces
75
74
 
76
75
  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.
77
76
 
78
- <pre><code>
77
+ ```ruby
79
78
  configatron.website_url = "http://www.mackframework.com"
80
79
  configatron.email.pop.address = "pop.example.com"
81
80
  configatron.email.pop.port = 110
82
81
  configatron.email.smtp.address = "smtp.example.com"
83
82
  configatron.email.smtp.port = 25
84
- </code></pre>
83
+ ```
85
84
 
86
85
  becomes:
87
86
 
88
- <pre><code>
87
+ ```ruby
89
88
  configatron.email.pop.address # => "pop.example.com"
90
89
  configatron.email.smtp.address # => "smtp.example.com"
91
90
  configatron.website_url # => "http://www.mackframework.com"
92
- </code></pre>
93
-
94
- Configatron allows you to nest namespaces to your hearts content! Just keep going, it's that easy.
91
+ ```
92
+ ####onfigatron allows you to nest namespaces to your hearts content! Just keep going, it's that easy.
95
93
 
96
94
  Of course you can update a single parameter n levels deep as well:
97
95
 
98
- <pre><code>
96
+ ```ruby
99
97
  configatron.email.pop.address = "pop2.example.com"
100
98
 
101
99
  configatron.email.pop.address # => "pop2.example.com"
102
100
  configatron.email.smtp.address # => "smtp.example.com"
103
- </code></pre>
101
+ ```
104
102
 
105
- h3. Temp Configurations
103
+ ### Temp Configurations
106
104
 
107
105
  Sometimes in testing, or other situations, you want to temporarily change some settings. You can do this with the @temp@ method:
108
106
 
109
- <pre><code>
107
+ ```ruby
110
108
  configatron.one = 1
111
109
  configatron.letters.a = 'A'
112
110
  configatron.letters.b = 'B'
@@ -122,16 +120,16 @@ Sometimes in testing, or other situations, you want to temporarily change some s
122
120
  configatron.letters.a # => 'A'
123
121
  configatron.letters.b # => 'B'
124
122
  configatron.letters.c # => nil
125
- </code></pre>
123
+ ```
126
124
 
127
125
  You can also pass in an optional Hash to the @temp@:
128
126
 
129
- <pre><code>
127
+ ```ruby
130
128
  configatron.one = 1
131
129
  configatron.letters.a = 'A'
132
130
  configatron.letters.b = 'B'
133
131
  configatron.temp(:letters => {:b => 'bb', :c => 'c'}) do
134
- configatron.one == 1
132
+ ####one == 1
135
133
  configatron.letters.a # => 'A'
136
134
  configatron.letters.b # => 'bb'
137
135
  configatron.letters.c # => 'c'
@@ -140,13 +138,13 @@ You can also pass in an optional Hash to the @temp@:
140
138
  configatron.letters.a # => 'A'
141
139
  configatron.letters.b # => 'B'
142
140
  configatron.letters.c # => nil
143
- </code></pre>
141
+ ```
144
142
 
145
- h3. Delayed and Dynamic Configurations
143
+ ### Delayed and Dynamic Configurations
146
144
 
147
145
  There are times when you want to refer to one configuration setting in another configuration setting. Let's look at a fairly contrived example:
148
146
 
149
- <pre><code>
147
+ ```ruby
150
148
  configatron.memcached.servers = ['127.0.0.1:11211']
151
149
  configatron.page_caching.servers = configatron.memcached.servers
152
150
  configatron.object_caching.servers = configatron.memcached.servers
@@ -160,15 +158,15 @@ There are times when you want to refer to one configuration setting in another c
160
158
  configatron.page_caching.servers = configatron.memcached.servers
161
159
  configatron.object_caching.servers = configatron.memcached.servers
162
160
  end
163
- </code></pre>
161
+ ```
164
162
 
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.
163
+ Now, we could've written that slightly differently, but it helps to illustrate the point. With Configatron you can create `Delayed` and `Dynamic` settings.
166
164
 
167
- h4. Delayed
165
+ #### Delayed
168
166
 
169
- With <code>Delayed</code> settings execution of the setting doesn't happen until the first time it is executed.
167
+ With `Delayed` settings execution of the setting doesn't happen until the first time it is executed.
170
168
 
171
- <pre><code>
169
+ ```ruby
172
170
  configatron.memcached.servers = ['127.0.0.1:11211']
173
171
  configatron.page_caching.servers = Configatron::Delayed.new {configatron.memcached.servers}
174
172
  configatron.object_caching.servers = Configatron::Delayed.new {configatron.memcached.servers}
@@ -178,49 +176,49 @@ With <code>Delayed</code> settings execution of the setting doesn't happen until
178
176
  elsif Rails.env == 'staging'
179
177
  configatron.memcached.servers = ['192.168.0.2:11211']
180
178
  end
181
- </code></pre>
179
+ ```
182
180
 
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>.
181
+ Execution occurs once and after that the result of that execution is returned. So in our case the first time someone calls the setting `configatron.page_caching.servers` it will find the `configatron.memcached.servers` setting and return that. After that point if the `configatron.memcached.servers` setting is changed, the original settings are returned by `configatron.page_caching.servers`.
184
182
 
185
- h4. Dynamic
183
+ #### Dynamic
186
184
 
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:
185
+ `Dynamic` settings are very similar to `Delayed` settings, but with one big difference. Every time you call a `Dynamic` setting is executed. Take this example:
188
186
 
189
- <pre><code>
187
+ ```ruby
190
188
  configatron.current.time = Configatron::Dynamic.new {Time.now}
191
- </code></pre>
189
+ ```
192
190
 
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.
191
+ Each time you call `configatron.current.time` it will return a new value to you. While this seems a bit useless, it is pretty useful if you have ever changing configurations.
194
192
 
195
- h3. Misc.
193
+ ### Misc.
196
194
 
197
195
  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.
198
196
 
199
- <pre><code>
197
+ ```ruby
200
198
  configatron.i.dont.exist.nil? # => true
201
199
  configatron.i.dont.exist # => Configatron::Store
202
- </code></pre>
200
+ ```
203
201
 
204
202
  If you want to get back an actual @nil@ then you can use the @retrieve@ method:
205
203
 
206
- <pre><code>
204
+ ```ruby
207
205
  configatron.i.do.exist = [:some, :array]
208
206
  configatron.i.dont.retrieve(:exist, nil) # => nil
209
207
  configatron.i.do.retrieve(:exist, :foo) # => [:some, :array]
210
- </code></pre>
208
+ ```
211
209
 
212
210
  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.
213
211
 
214
- <pre><code>
212
+ ```ruby
215
213
  configatron.set_default(:name, 'Mark Bates')
216
214
  configatron.name # => 'Mark Bates'
217
215
  configatron.set_default(:name, 'Me')
218
216
  configatron.name # => 'Mark Bates'
219
- </code></pre>
217
+ ```
220
218
 
221
219
  Enjoy!
222
220
 
223
- h2. Contributors
221
+ ## Contributors
224
222
 
225
223
  * Mark Bates
226
224
  * Kurtis Rainbolt-Greene
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ desc "Run tests"
5
+ task :default do
6
+ system "bundle exec rspec"
7
+ end
@@ -0,0 +1,49 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'configatron/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "configatron"
8
+ gem.version = Configatron::VERSION
9
+ gem.authors = ["Mark Bates"]
10
+ gem.email = ["mark@markbates.com"]
11
+ gem.description = %q{A powerful Ruby configuration system.}
12
+ gem.summary = %q{A powerful Ruby configuration system.}
13
+ gem.homepage = "http://www.metabates.com"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency("yamler", ">= 0.1.0")
21
+ end
22
+
23
+
24
+
25
+ # # -*- encoding: utf-8 -*-
26
+ # require File.expand_path('../lib/configatron/version', __FILE__)
27
+
28
+ # Gem::Specification.new do |s|
29
+ # s.name = "configatron"
30
+ # s.version = Configatron::VERSION
31
+
32
+ # s.authors = ["markbates"]
33
+ # s.description = "configatron was developed by: markbates"
34
+ # s.email = "mark@markbates.com"
35
+ # s.extra_rdoc_files = ["LICENSE"]
36
+
37
+ # ignored_files = File.read('.gitignore').split("\n").compact.reject(&:empty?) + ["Rakefile", "Gemfile", "configatron.gemspec"]
38
+ # test_files = Dir['spec/**/*'].reject {|f| File.directory?(f)}
39
+ # library_files = Dir['**/*'].reject{|f| File.directory?(f)}
40
+ # s.files = library_files - test_files - ignored_files
41
+ # s.homepage = "http://www.metabates.com"
42
+ # s.require_paths = ["lib"]
43
+ # s.summary = "A powerful Ruby configuration system."
44
+
45
+ # s.add_runtime_dependency "yamler", ">= 0.1.0"
46
+ # s.add_development_dependency 'rake'
47
+ # s.add_development_dependency 'rspec'
48
+ # s.add_development_dependency 'gemstub'
49
+ # end
@@ -299,11 +299,19 @@ class Configatron
299
299
  self.methods.include?(RUBY_VERSION > '1.9.0' ? name.to_sym : name.to_s)
300
300
  end
301
301
 
302
+ def is_syck?(obj)
303
+ if defined?(SYCK_CONSTANT)
304
+ Configatron.log.warn "DEPRECATED! (SYCK) Syck support has been removed from Configatron in Ruby 2.x. This feature will be removed entirely in Configatron 3.0. Please be advised."
305
+ return obj.is_a?(SYCK_CONSTANT)
306
+ end
307
+ return false
308
+ end
309
+
302
310
  def parse_options(options)
303
311
  if options.is_a?(Hash)
304
312
  options.each do |k,v|
305
313
  if v.is_a?(Hash)
306
- if v.keys.length == 1 && v.keys.first.is_a?(SYCK_CONSTANT)
314
+ if v.keys.length == 1 && is_syck?(v.keys.first)
307
315
  self.method_missing("#{k}=", v.values.first.flatten)
308
316
  else
309
317
  self.method_missing(k.to_sym).configure_from_hash(v)
@@ -324,8 +332,8 @@ class Configatron
324
332
 
325
333
  if RUBY_PLATFORM == 'java'
326
334
  SYCK_CONSTANT = YAML::Yecht::MergeKey
327
- else
328
- SYCK_CONSTANT = (RUBY_VERSION.match(/^1\.9/) ? Syck::MergeKey : YAML::Syck::MergeKey)
335
+ elsif RUBY_VERSION.match(/^1\.9/)
336
+ SYCK_CONSTANT = Syck::MergeKey
329
337
  end
330
338
 
331
339
  end # Store
@@ -1,3 +1,3 @@
1
1
  class Configatron
2
- VERSION = "2.9.1"
2
+ VERSION = "2.10.0"
3
3
  end
@@ -0,0 +1,67 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Configatron::Proc do
4
+
5
+ before(:each) do
6
+ configatron.temp_start
7
+ end
8
+
9
+ after(:each) do
10
+ configatron.temp_end
11
+ end
12
+
13
+ describe Configatron::Dynamic do
14
+
15
+ it 'should execute the code at a later date' do
16
+ configatron.tv.shows = ['seinfeld', 'simpsons']
17
+ configatron.my.tv.shows = Configatron::Dynamic.new do
18
+ "My shows are: #{configatron.tv.shows.join(', ')}"
19
+ end
20
+ configatron.my.tv.shows.should == 'My shows are: seinfeld, simpsons'
21
+ configatron.tv.shows = ['seinfeld', 'simpsons', 'entourage']
22
+ configatron.my.tv.shows.should == 'My shows are: seinfeld, simpsons, entourage'
23
+ configatron.tv.shows = ['seinfeld', 'simpsons', 'entourage', 'office']
24
+ configatron.my.tv.shows.should == 'My shows are: seinfeld, simpsons, entourage, office'
25
+ end
26
+
27
+ it 'should work with retrieve' do
28
+ configatron.tv.shows = ['seinfeld', 'simpsons']
29
+ configatron.my.tv.shows = Configatron::Dynamic.new do
30
+ "My shows are: #{configatron.tv.shows.join(', ')}"
31
+ end
32
+ configatron.my.tv.retrieve(:shows).should == 'My shows are: seinfeld, simpsons'
33
+ configatron.tv.shows = ['seinfeld', 'simpsons', 'entourage']
34
+ configatron.my.tv.retrieve(:shows).should == 'My shows are: seinfeld, simpsons, entourage'
35
+ configatron.tv.shows = ['seinfeld', 'simpsons', 'entourage', 'office']
36
+ configatron.my.tv.retrieve(:shows).should == 'My shows are: seinfeld, simpsons, entourage, office'
37
+ end
38
+
39
+ end
40
+
41
+ describe Configatron::Delayed do
42
+
43
+ it 'should execute the code once at a later date' do
44
+ configatron.tv.shows = ['seinfeld', 'simpsons']
45
+ configatron.my.tv.shows = Configatron::Delayed.new do
46
+ "My shows are: #{configatron.tv.shows.join(', ')}"
47
+ end
48
+ configatron.tv.shows = ['seinfeld', 'simpsons', 'entourage']
49
+ configatron.my.tv.shows.should == 'My shows are: seinfeld, simpsons, entourage'
50
+ configatron.tv.shows = ['seinfeld', 'simpsons', 'entourage', 'office']
51
+ configatron.my.tv.shows.should == 'My shows are: seinfeld, simpsons, entourage'
52
+ end
53
+
54
+ it 'should work with retrieve' do
55
+ configatron.tv.shows = ['seinfeld', 'simpsons']
56
+ configatron.my.tv.shows = Configatron::Delayed.new do
57
+ "My shows are: #{configatron.tv.shows.join(', ')}"
58
+ end
59
+ configatron.tv.shows = ['seinfeld', 'simpsons', 'entourage']
60
+ configatron.my.tv.retrieve(:shows).should == 'My shows are: seinfeld, simpsons, entourage'
61
+ configatron.tv.shows = ['seinfeld', 'simpsons', 'entourage', 'office']
62
+ configatron.my.tv.retrieve(:shows).should == 'My shows are: seinfeld, simpsons, entourage'
63
+ end
64
+
65
+ end
66
+
67
+ end