configatron 1.2.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/README +61 -34
  2. data/doc/classes/Configatron.html +241 -0
  3. data/doc/classes/Configatron/ProtectedParameter.html +146 -0
  4. data/doc/classes/Configatron/Store.html +241 -121
  5. data/doc/classes/Kernel.html +13 -20
  6. data/doc/created.rid +1 -1
  7. data/doc/files/README.html +85 -43
  8. data/doc/files/lib/configatron/{configuration_rb.html → configatron_rb.html} +11 -4
  9. data/doc/files/lib/configatron/{helpers_rb.html → errors_rb.html} +4 -4
  10. data/doc/files/lib/configatron/kernel_rb.html +1 -1
  11. data/doc/files/lib/configatron/store_rb.html +1 -1
  12. data/doc/files/lib/configatron_rb.html +1 -2
  13. data/doc/fr_class_index.html +2 -4
  14. data/doc/fr_file_index.html +2 -3
  15. data/doc/fr_method_index.html +15 -21
  16. data/lib/configatron.rb +5 -44
  17. data/lib/configatron/configatron.rb +44 -0
  18. data/lib/configatron/errors.rb +7 -0
  19. data/lib/configatron/kernel.rb +3 -9
  20. data/lib/configatron/store.rb +148 -56
  21. data/spec/lib/configatron_spec.rb +293 -0
  22. data/spec/lib/futurama.yml +6 -0
  23. data/spec/spec_helper.rb +3 -2
  24. metadata +11 -18
  25. data/doc/classes/Configatron/Configuration.html +0 -402
  26. data/doc/classes/Configatron/Helpers.html +0 -174
  27. data/doc/classes/Configatron/YamlStore.html +0 -203
  28. data/doc/classes/Hash.html +0 -193
  29. data/doc/files/lib/configatron/yaml_store_rb.html +0 -101
  30. data/lib/configatron/configuration.rb +0 -112
  31. data/lib/configatron/helpers.rb +0 -27
  32. data/lib/configatron/yaml_store.rb +0 -33
  33. data/spec/unit/configuration_spec.rb +0 -299
  34. data/spec/unit/family_guy.yml +0 -2
  35. data/spec/unit/helpers_spec.rb +0 -115
  36. data/spec/unit/kernel_spec.rb +0 -48
  37. data/spec/unit/store_spec.rb +0 -91
@@ -1,101 +0,0 @@
1
- <?xml version="1.0" encoding="iso-8859-1"?>
2
- <!DOCTYPE html
3
- PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
-
6
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
7
- <head>
8
- <title>File: yaml_store.rb</title>
9
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
10
- <meta http-equiv="Content-Script-Type" content="text/javascript" />
11
- <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
12
- <script type="text/javascript">
13
- // <![CDATA[
14
-
15
- function popupCode( url ) {
16
- window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
17
- }
18
-
19
- function toggleCode( id ) {
20
- if ( document.getElementById )
21
- elem = document.getElementById( id );
22
- else if ( document.all )
23
- elem = eval( "document.all." + id );
24
- else
25
- return false;
26
-
27
- elemStyle = elem.style;
28
-
29
- if ( elemStyle.display != "block" ) {
30
- elemStyle.display = "block"
31
- } else {
32
- elemStyle.display = "none"
33
- }
34
-
35
- return true;
36
- }
37
-
38
- // Make codeblocks hidden by default
39
- document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
40
-
41
- // ]]>
42
- </script>
43
-
44
- </head>
45
- <body>
46
-
47
-
48
-
49
- <div id="fileHeader">
50
- <h1>yaml_store.rb</h1>
51
- <table class="header-table">
52
- <tr class="top-aligned-row">
53
- <td><strong>Path:</strong></td>
54
- <td>lib/configatron/yaml_store.rb
55
- </td>
56
- </tr>
57
- <tr class="top-aligned-row">
58
- <td><strong>Last Update:</strong></td>
59
- <td>Fri Sep 05 16:22:37 -0400 2008</td>
60
- </tr>
61
- </table>
62
- </div>
63
- <!-- banner header -->
64
-
65
- <div id="bodyContent">
66
-
67
-
68
-
69
- <div id="contextContent">
70
-
71
-
72
-
73
- </div>
74
-
75
-
76
- </div>
77
-
78
-
79
- <!-- if includes -->
80
-
81
- <div id="section">
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
- <!-- if method_list -->
91
-
92
-
93
- </div>
94
-
95
-
96
- <div id="validator-badges">
97
- <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
98
- </div>
99
-
100
- </body>
101
- </html>
@@ -1,112 +0,0 @@
1
- module Configatron
2
- # The central class for managing the configurations.
3
- class Configuration
4
- include Singleton
5
- include Configatron::Helpers
6
-
7
- # If nil_for_missing is set to true nil will be returned if the configuration
8
- # parameter doesn't exist. If set to false, default, then a NoMethodError exception
9
- # will be raised.
10
- attr_accessor :nil_for_missing
11
-
12
- def initialize # :nodoc:
13
- @_storage_list = []
14
- @_nil_for_missing = false
15
- end
16
-
17
- # Yields a new Configatron::Store class.
18
- def configure
19
- storage = Configatron::Store.new
20
- yield storage
21
- unless storage.parameters.empty?
22
- @_storage_list << storage
23
- load_methods(storage)
24
- end
25
- end
26
-
27
- # Used to load configuration settings from a Hash.
28
- def configure_from_hash(parameters)
29
- parameters = self.to_hash.recursive_merge(parameters)
30
- storage = Configatron::Store.new(parameters)
31
- @_storage_list << storage
32
- load_methods(storage)
33
- end
34
-
35
- # Used to load configuration settings from a YAML file.
36
- def configure_from_yaml(path)
37
- begin
38
- storage = Configatron::YamlStore.new(path)
39
- @_storage_list << storage
40
- load_methods(storage)
41
- rescue Errno::ENOENT => e
42
- puts e.message
43
- # file doesn't exist.
44
- end
45
- end
46
-
47
- # Replays the history of configurations.
48
- def reload
49
- @_storage_list.each do |storage|
50
- storage.reload
51
- load_methods(storage)
52
- end
53
- end
54
-
55
- # Does a hard reset of the Configatron::Configuration class.
56
- # All methods are undefined, the list of configuration parameters
57
- # is emptied, and the nil_for_missing method gets reset to false.
58
- def reset!
59
- reset
60
- self.nil_for_missing = false
61
- @_storage_list = []
62
- end
63
-
64
- # All methods are undefined.
65
- def reset
66
- @_storage_list.each do |storage|
67
- storage.parameters.each do |k,v|
68
- Configatron::Configuration.instance_eval do
69
- begin
70
- undef_method(k)
71
- rescue NameError => e
72
- end
73
- end
74
- end
75
- end
76
- end
77
-
78
- # Peels back n number of configuration parameters.
79
- def revert(step = 1)
80
- reset
81
- step.times {@_storage_list.pop}
82
- reload
83
- end
84
-
85
- def method_missing(sym, *args) # :nodoc:
86
- handle_missing_parameter(sym)
87
- end
88
-
89
- def to_hash
90
- @_storage_list.inject({}) { |acc, storage| acc.merge(storage.to_hash) }
91
- end
92
-
93
- private
94
- def load_methods(store)
95
- store.parameters.each do |k,v|
96
- if k.is_a?(Configatron::Store)
97
- load_methods(k)
98
- else
99
- if v.is_a?(Hash)
100
- v = (store.parameters[k] = Configatron::Store.new(v))
101
- end
102
- Configatron::Configuration.instance_eval do
103
- define_method(k) do
104
- v
105
- end
106
- end
107
- end
108
- end
109
- end
110
-
111
- end # Configuration
112
- end # Configatron
@@ -1,27 +0,0 @@
1
- module Configatron
2
- module Helpers
3
-
4
- # Checks whether or not configuration parameter exists.
5
- def exists?(name)
6
- self.respond_to?(name)
7
- end
8
-
9
- def handle_missing_parameter(param) # :nodoc:
10
- if configatron.nil_for_missing
11
- return nil
12
- else
13
- raise NoMethodError.new(param.to_s)
14
- end
15
- end
16
-
17
- # Retrieves the specified config parameter. An optional second
18
- # parameter can be passed that will be returned if the config
19
- # parameter doesn't exist.
20
- def retrieve(name, default_value = ArgumentError)
21
- return self.send(name) if exists?(name)
22
- return default_value unless default_value == ArgumentError
23
- handle_missing_parameter(name)
24
- end
25
-
26
- end # Helpers
27
- end # Configatron
@@ -1,33 +0,0 @@
1
- require File.join(File.dirname(__FILE__), 'store')
2
- module Configatron
3
- # Used to store each of the 'sets' of configuration parameters that came from a YAML file.
4
- class YamlStore < Configatron::Store
5
-
6
- attr_accessor :file_location
7
-
8
- # Takes the full path to the YAML file.
9
- def initialize(file_location)
10
- super(params_from_yaml(file_location))
11
- @file_location = file_location
12
- end
13
-
14
- # Re-reads the YAML file.
15
- def reload
16
- if self.file_location
17
- params_from_yaml(self.file_location)
18
- end
19
- end
20
-
21
- private
22
- def params_from_yaml(file_location)
23
- begin
24
- @parameters = YAML.load(File.read(file_location))
25
- return @parameters
26
- rescue Errno::ENOENT => e
27
- puts e
28
- end
29
- return {}
30
- end
31
-
32
- end
33
- end
@@ -1,299 +0,0 @@
1
- require 'fileutils'
2
- require 'pathname'
3
- require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
4
-
5
- describe Configatron::Configuration do
6
-
7
- before :each do
8
- configatron.reset!
9
- end
10
-
11
- it "should be a Singleton" do
12
- lambda{Configatron::Configuration.new}.should raise_error(NoMethodError)
13
- Configatron::Configuration.instance.should == Configatron::Configuration.instance
14
- end
15
-
16
- describe "configure_from_hash" do
17
-
18
- it "should take a Hash with the same outcome as the configure method" do
19
- configatron.should_not respond_to(:bart)
20
- configatron.should_not respond_to(:homer)
21
-
22
- configatron.configure_from_hash(:bart => "Bart Simpson", "homer" => "Homer Simpson")
23
-
24
- configatron.should respond_to(:bart)
25
- configatron.should respond_to(:homer)
26
-
27
- configatron.bart.should == "Bart Simpson"
28
- configatron.homer.should == "Homer Simpson"
29
- end
30
-
31
- it "doesn't wipe out existing configurations" do
32
- configatron do |c|
33
- c.namespace(:mack) do |mack|
34
- mack.one = 1
35
- mack.two = 2
36
- mack.three = 3
37
- end
38
- c.a = 'a'
39
- c.b = 'b'
40
- end
41
- configatron.to_hash.should == {:mack => {:one => 1, :two => 2, :three => 3}, :a => 'a', :b => 'b'}
42
- configatron.configure_from_hash(:mack => {:one => 'one'}, :b => 'bee')
43
- configatron.to_hash.should == {:mack => {:one => 'one', :two => 2, :three => 3}, :a => 'a', :b => 'bee'}
44
- configatron.revert
45
- configatron.to_hash.should == {:mack => {:one => 1, :two => 2, :three => 3}, :a => 'a', :b => 'b'}
46
- end
47
-
48
- end
49
-
50
- describe "hashes to namespace" do
51
-
52
- before :each do
53
- configatron.reset!
54
- @futurama = File.join(File.dirname(__FILE__), 'futurama.yml')
55
- FileUtils.rm_f(@futurama)
56
- end
57
-
58
- after :each do
59
- FileUtils.rm_f(@futurama)
60
- end
61
-
62
- it "should namespace hashes from yaml" do
63
-
64
- configatron.should_not respond_to(:cartoon)
65
-
66
- File.open(@futurama, 'w') do |f|
67
- f.puts %{
68
- cartoon:
69
- characters:
70
- fry: human
71
- leela: mutant
72
- bender: robot
73
- transportation: space ship
74
- fans:
75
- one: Mark Bates
76
- two: Dylan Bates
77
- }
78
- end
79
-
80
- configatron.configure_from_yaml(@futurama)
81
-
82
- configatron.should respond_to(:cartoon)
83
- configatron.cartoon.should be_an_instance_of(Configatron::Store)
84
-
85
- configatron.cartoon.characters.fry.should == 'human'
86
- configatron.cartoon.transportation.should == 'space ship'
87
-
88
- end
89
-
90
- it "should namespace hashes" do
91
-
92
- configatron.should_not respond_to(:cartoon)
93
-
94
- configatron.configure_from_hash({:cartoon => {:characters => {:fry => 'human', :leela => 'mutant', :bender => 'robot'}, :transportation => 'space ship'}, :fans => {:one => 'Mark Bates', :two => 'Dylan Bates'}})
95
-
96
- configatron.should respond_to(:cartoon)
97
- configatron.cartoon.should be_an_instance_of(Configatron::Store)
98
-
99
- configatron.cartoon.characters.fry.should == 'human'
100
- configatron.cartoon.transportation.should == 'space ship'
101
-
102
- end
103
-
104
- end
105
-
106
- describe "configure_from_yaml" do
107
-
108
- before :each do
109
- configatron.reset!
110
- @futurama = File.join(File.dirname(__FILE__), 'futurama.yml')
111
- FileUtils.rm_f(@futurama)
112
- end
113
-
114
- after :each do
115
- FileUtils.rm_f(@futurama)
116
- end
117
-
118
- it "should take a path to YAML file and use it configure configatron" do
119
- configatron.should_not respond_to(:family_guy)
120
- configatron.should_not respond_to(:lois)
121
-
122
- configatron.configure_from_yaml(File.join(File.dirname(__FILE__), 'family_guy.yml'))
123
-
124
- configatron.should respond_to(:family_guy)
125
- configatron.should respond_to(:lois)
126
-
127
- configatron.family_guy.should == "Peter Griffin"
128
- configatron.lois.should == "Lois Griffin"
129
- end
130
-
131
- it "should silently fail if the file doesn't exist" do
132
- lambda {configatron.configure_from_yaml(File.join(File.dirname(__FILE__), 'i_dont_exist.yml'))}.should_not raise_error(Errno::ENOENT)
133
- end
134
-
135
- it "should re-read the file during a reload" do
136
- File.open(@futurama, 'w') do |f|
137
- f.puts 'bender: robot'
138
- f.puts 'fry: human'
139
- end
140
-
141
- configatron.should_not respond_to(:bender)
142
- configatron.should_not respond_to(:fry)
143
-
144
- configatron.configure_from_yaml(@futurama)
145
-
146
- configatron.should respond_to(:bender)
147
- configatron.should respond_to(:fry)
148
-
149
- configatron.bender.should == 'robot'
150
- configatron.fry.should == 'human'
151
-
152
- File.open(@futurama, 'w') do |f|
153
- f.puts 'bender: Bending Robot'
154
- f.puts 'fry: Human Male'
155
- end
156
-
157
- configatron.reload
158
-
159
- configatron.bender.should == 'Bending Robot'
160
- configatron.fry.should == 'Human Male'
161
-
162
- end
163
-
164
- end
165
-
166
- describe "revert" do
167
-
168
- it "should roll back 1 level by default" do
169
- configatron.should_not respond_to(:bart)
170
- configatron.should_not respond_to(:homer)
171
-
172
- configatron do |config|
173
- config.bart = "Bart Simpson"
174
- end
175
- configatron do |config|
176
- config.homer = "Homer Simpson"
177
- end
178
-
179
- configatron.should respond_to(:bart)
180
- configatron.should respond_to(:homer)
181
-
182
- configatron.bart.should == "Bart Simpson"
183
- configatron.homer.should == "Homer Simpson"
184
-
185
- configatron.revert
186
-
187
- configatron.should respond_to(:bart)
188
- configatron.should_not respond_to(:homer)
189
- configatron.bart.should == "Bart Simpson"
190
- lambda{configatron.homer}.should raise_error(NoMethodError)
191
- end
192
-
193
- it "should roll back n levels if specified" do
194
- configatron.should_not respond_to(:bart)
195
- configatron.should_not respond_to(:homer)
196
-
197
- configatron do |config|
198
- config.bart = "Bart Simpson"
199
- end
200
- configatron do |config|
201
- config.homer = "Homer Simpson"
202
- end
203
-
204
- configatron.should respond_to(:bart)
205
- configatron.should respond_to(:homer)
206
-
207
- configatron.bart.should == "Bart Simpson"
208
- configatron.homer.should == "Homer Simpson"
209
-
210
- configatron.revert(2)
211
-
212
- configatron.should_not respond_to(:bart)
213
- configatron.should_not respond_to(:homer)
214
- lambda{configatron.bart}.should raise_error(NoMethodError)
215
- lambda{configatron.homer}.should raise_error(NoMethodError)
216
- end
217
-
218
- end
219
-
220
- describe "reset" do
221
-
222
- it "should remove all added methods" do
223
- configatron.should_not respond_to(:rst1)
224
- configatron.should_not respond_to(:rst2)
225
-
226
- configatron.nil_for_missing.should == false
227
-
228
- configatron do |config|
229
- config.rst1 = "RST1"
230
- config.rst2 = "RST2"
231
- end
232
-
233
- configatron.should respond_to(:rst1)
234
- configatron.should respond_to(:rst2)
235
-
236
- configatron.nil_for_missing = true
237
- configatron.nil_for_missing.should == true
238
-
239
- configatron.reset
240
-
241
- configatron.nil_for_missing.should == true
242
-
243
- configatron.should_not respond_to(:rst1)
244
- configatron.should_not respond_to(:rst2)
245
-
246
- end
247
-
248
- end
249
-
250
- describe "reset!" do
251
-
252
- it "should remove all added methods and revert the nil_for_missing setting" do
253
- configatron.should_not respond_to(:rst1)
254
- configatron.should_not respond_to(:rst2)
255
-
256
- configatron.nil_for_missing.should == false
257
-
258
- configatron do |config|
259
- config.rst1 = "RST1"
260
- config.rst2 = "RST2"
261
- end
262
-
263
- configatron.should respond_to(:rst1)
264
- configatron.should respond_to(:rst2)
265
-
266
- configatron.nil_for_missing = true
267
- configatron.nil_for_missing.should == true
268
-
269
- configatron.reset!
270
-
271
- configatron.nil_for_missing.should == false
272
-
273
- configatron.should_not respond_to(:rst1)
274
- configatron.should_not respond_to(:rst2)
275
- end
276
-
277
- end
278
-
279
- describe 'to_hash' do
280
- it "should convert entire configuration to a hash" do
281
- hash = {:foo => 'bar', :baz => {:quux => 'foobar'}}
282
-
283
- configatron.configure_from_hash(hash)
284
-
285
- configatron.to_hash.should == hash
286
- end
287
-
288
- it 'should convert the configration with multiple calls to configure' do
289
- hash1 = {:foo => 'bar', :baz => {:quux => 'foobar'}}
290
- hash2 = {:foo => 'notbar', :beep => 'boop'}
291
-
292
- configatron.configure_from_hash(hash1)
293
- configatron.configure_from_hash(hash2)
294
-
295
- configatron.to_hash.should == hash1.merge(hash2)
296
- end
297
- end
298
-
299
- end