aerogel-configurator 1.1.0 → 1.2.0
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.
- checksums.yaml +4 -4
- data/README.md +33 -1
- data/examples/ex8.conf +15 -0
- data/examples/ex8.rb +12 -0
- data/examples/ex9.conf +5 -0
- data/examples/ex9.rb +12 -0
- data/lib/aerogel/configurator/configurator.rb +6 -2
- data/lib/aerogel/configurator/parameter.rb +9 -0
- data/lib/aerogel/configurator/version.rb +1 -1
- data/spec/lib/configurator_spec.rb +27 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 042aa549ffd9d4f413d6a8de433bf0ae8946c456
|
4
|
+
data.tar.gz: 41b201f4106c00992e80817d725a523f54c4f105
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
data/examples/ex9.conf
ADDED
data/examples/ex9.rb
ADDED
@@ -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.
|
@@ -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.
|
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-
|
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
|