aerogel-configurator 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e67be32b16833293c9ac19c0ce496bc306e8b47a
4
- data.tar.gz: a552d6da5a60a15d92e802e5ddcadb0b2905db93
3
+ metadata.gz: 042aa549ffd9d4f413d6a8de433bf0ae8946c456
4
+ data.tar.gz: 41b201f4106c00992e80817d725a523f54c4f105
5
5
  SHA512:
6
- metadata.gz: 7bbb903dbf8278511398f116e39602a289de0ae6b7a8d9451c769fa66b0900e51a9bf8afbc4a23a89ce9e22808e3023f6ebfa52939d1b3ff44e1e67c5c22b5ed
7
- data.tar.gz: 37ae8ca12aeea1ad9ade209095e14bc11ea61c1f41154d6c2348039e8735eb250b980dc39c681c0dac6947a6c90dbf561dc192c370cf093d1861551b9de15c1b
6
+ metadata.gz: b04a7ab68eba0eff985fc3f4772801dd9ee3d212da69d92d280ffd040a8c81d1ee97fb2ebd1cffd8b1103204a3a5bab2e227c85c5a0f1d8ceb97c54c20e0175e
7
+ data.tar.gz: 61dfb5acaf68e312755bef0533272fd3a5f38560f0213e6c3be69e36a6c6e2dd0d6570f0a0ae5794e1b461ee31a85736eb8990bd251cd6c1a3d6746b15d868ef
data/README.md CHANGED
@@ -227,7 +227,39 @@ config.foo # => 123
227
227
  config.bar # => 'abc'
228
228
  ```
229
229
 
230
- ### 9. More examples?
230
+
231
+ ### 9. Iterating over config
232
+ You can always iterate over undelying Hash like this:
233
+ ```ruby
234
+ config.to_hash.each {|name, value| ... }
235
+ ```
236
+ But in case you have used deferred parameters, these values won't be automatically evaluated.
237
+ A built-in iterator solves this problem:
238
+ ```ruby
239
+ # my.conf
240
+ root_path "/default/path"
241
+ path_to_file lambda{ root_path+"/dir/filename.txt" }
242
+ ```
243
+ ```ruby
244
+ # my_app.rb
245
+ require 'aerogel/configurator'
246
+
247
+ config = Configurator.new "my.conf"
248
+
249
+ config.each do |name, value|
250
+ puts "#{name}: #{value}"
251
+ end
252
+ # =>
253
+ # root_path: /default/path
254
+ # path_to_file: /default/path/dir/filename.txt
255
+ ```
256
+
257
+ Of course, any config parameter containing nested group of parameters responds to ```#each```iterator too:
258
+ ```ruby
259
+ config.group1.subgroup2.each {|name, value| ... }
260
+ ```
261
+
262
+ ### 10. More examples?
231
263
  See ```examples/``` folder.
232
264
 
233
265
  ## Feedback
data/examples/ex8.conf ADDED
@@ -0,0 +1,15 @@
1
+ # nested groups and reused values example
2
+ foo "hello"
3
+ bar 42
4
+ a_group {
5
+ boolean_param true
6
+ a_nested_group {
7
+ bar 123
8
+
9
+ # reusing value of 'bar'
10
+ inner bar
11
+
12
+ # reusing value of a_group.a_nested_group.bar
13
+ ar_var [1, 2, a_group.a_nested_group.bar]
14
+ }
15
+ }
data/examples/ex8.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'aerogel/configurator'
2
+
3
+
4
+ config = Configurator.load File.dirname( __FILE__ )+"/ex8.conf"
5
+
6
+
7
+ puts "config: #{config.to_hash}"
8
+
9
+ puts "config.each:"
10
+ config.each do |name, value|
11
+ puts "#{name}: #{value}"
12
+ end
data/examples/ex9.conf ADDED
@@ -0,0 +1,5 @@
1
+ # iterating over deferred parameters example
2
+
3
+ root_path "/default/path"
4
+
5
+ path_to_file lambda{ root_path+"/dir/filename.txt" }
data/examples/ex9.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'aerogel/configurator'
2
+
3
+
4
+ config = Configurator.load File.dirname( __FILE__ )+"/ex9.conf"
5
+
6
+
7
+ puts "config: #{config.to_hash}"
8
+
9
+ puts "config.each:"
10
+ config.each do |name, value|
11
+ puts "#{name}: #{value}"
12
+ end
@@ -14,9 +14,13 @@ class Configurator
14
14
  end
15
15
 
16
16
 
17
- def method_missing(method, *args)
17
+ def method_missing(method, *args, &block)
18
18
  # puts "Configurator.method_missing: #{method}"
19
- Parameter.new( @params ).send method, *args
19
+ Parameter.new( @params ).send method, *args, &block
20
+ end
21
+
22
+ def respond_to?(method, include_private = false)
23
+ super || Parameter.new( @params ).respond_to?(method, include_private)
20
24
  end
21
25
 
22
26
  # Loads parameters from +source+.
@@ -34,6 +34,15 @@ private
34
34
  @data.inspect
35
35
  end
36
36
 
37
+ # Iterates over parameter group.
38
+ #
39
+ def each( &block )
40
+ @data.keys.each do |method|
41
+ value = send method
42
+ yield method, value
43
+ end
44
+ end
45
+
37
46
  # Undefined is a parameter that was not set yet.
38
47
  # It allows accessing its sub-parameters and defines the entire
39
48
  # chain once an assignement method on a sub-parameter is called.
@@ -1,3 +1,3 @@
1
1
  class Configurator
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -11,6 +11,12 @@ describe Configurator do
11
11
  :b => 2
12
12
  }
13
13
  }
14
+ @flat_params = {
15
+ :foo1 => "abc1",
16
+ :foo2 => "abc2",
17
+ :foo3 => "abc3",
18
+ :foo4 => "abc4"
19
+ }
14
20
  end
15
21
 
16
22
  it "should respond to 'load' and return a Configurator instance" do
@@ -133,6 +139,27 @@ describe Configurator do
133
139
  config.group.to_hash.should eql @default_params[:group]
134
140
  end
135
141
 
142
+ it "should respond to #to_hash on each level" do
143
+ config = Configurator.new @default_params
144
+ expect { config.to_hash }.to_not raise_error
145
+ config.to_hash.should be_instance_of Hash
146
+ config.to_hash.should eql @default_params
147
+ config.group.to_hash.should be_instance_of Hash
148
+ config.group.to_hash.should eql @default_params[:group]
149
+ end
150
+
151
+ it "should respond to #each" do
152
+ config = Configurator.new @default_params
153
+ config.should respond_to(:each)
154
+ end
155
+
156
+ it "#each should iterate over parameters" do
157
+ # TODO: figure out a way to properly test #each iterator for the nested parameters case
158
+ config = Configurator.new @flat_params
159
+ params_each_list = @flat_params.to_a
160
+ expect {|b| config.each(&b)}.to yield_successive_args( *params_each_list )
161
+ end
162
+
136
163
  end
137
164
  end # describe Configurator
138
165
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aerogel-configurator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Kukushkin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-08 00:00:00.000000000 Z
11
+ date: 2013-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -79,6 +79,10 @@ files:
79
79
  - examples/ex6.rb
80
80
  - examples/ex7.rb
81
81
  - examples/ex7.yml
82
+ - examples/ex8.conf
83
+ - examples/ex8.rb
84
+ - examples/ex9.conf
85
+ - examples/ex9.rb
82
86
  - lib/aerogel/configurator.rb
83
87
  - lib/aerogel/configurator/configurator.rb
84
88
  - lib/aerogel/configurator/dsl.rb