nested_config 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,11 +1,10 @@
1
- = nested_config
1
+ = nested_config {<img src="https://secure.travis-ci.org/neopoly/nested_config.png?branch=master" alt="Build Status" />}[http://travis-ci.org/neopoly/nested_config]
2
2
 
3
3
  Simple, static, nested application configuration
4
4
 
5
5
  Gem[https://rubygems.org/gems/nested_config] |
6
6
  Source[https://github.com/neopoly/nested_config] |
7
- RDoc[http://rubydoc.info/github/neopoly/nested_config/master/file/README.rdoc] |
8
- {<img src="https://secure.travis-ci.org/neopoly/nested_config.png?branch=master" alt="Build Status" />}[http://travis-ci.org/neopoly/nested_config]
7
+ RDoc[http://rubydoc.info/github/neopoly/nested_config/master/file/README.rdoc]
9
8
 
10
9
  == Usage
11
10
 
@@ -62,17 +61,6 @@ The initial access to key +country_list+ calls (via +call+ method) the proc and
62
61
  Subsequent calls just fetch the replaced value.
63
62
 
64
63
 
65
- === Cloning
66
-
67
- NestedConfig can be cloned:
68
-
69
- clone = MyApp.config.__clone__
70
- clone.coins = 2000
71
- MyApp.config.conins # => 1000
72
-
73
- *Note*: Proc values CANNOT be cloned.
74
-
75
-
76
64
  === Modifying config using NestedConfig::WithConfig
77
65
 
78
66
  Values of NestedConfig can be temporalily modified.
@@ -84,22 +72,32 @@ This can be used in tests modifying a global application config inside a block:
84
72
  class MyCase < MiniTest::TestCase
85
73
  include NestedConfig::WithConfig
86
74
 
87
- def with_app_config(key, &block)
88
- with_config(MyApp.config, key, &block)
75
+ def app_config
76
+ MyApp.config # global
89
77
  end
90
78
  end
91
79
 
92
80
  class SomeCase < MyCase
93
81
  def setup
94
82
  app_config.tap do |config|
95
- config.workers do |workers|
96
- workers.max = 5
83
+ config.coins = 1000
84
+ config.queue do |queue|
85
+ queue.workers do |workers|
86
+ workers.max = 5
87
+ end
97
88
  end
98
89
  end
99
90
  end
100
91
 
101
- def test_with_config
102
- with_app_config(:workers) do |workers|
92
+ def test_with_basic_value
93
+ with_config(app_config) do |config|
94
+ config.coins = 500
95
+ end
96
+ # global config reset to previous config
97
+ end
98
+
99
+ def test_queue_with_changed_workers
100
+ with_config(app_config, :queue, :workers) do |workers|
103
101
  workers.max = 1
104
102
  # do stuff with modified config max value
105
103
  end
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ task :default => :test
6
6
  # Test
7
7
  require 'rake/testtask'
8
8
  Rake::TestTask.new(:test) do |test|
9
- test.test_files = FileList.new('test/*_test.rb')
9
+ test.test_files = FileList.new('test/**/*_test.rb')
10
10
  test.libs << 'test'
11
11
  test.verbose = true
12
12
  end
@@ -1,3 +1,3 @@
1
1
  class NestedConfig
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -16,14 +16,24 @@ class NestedConfig
16
16
  # class SomeCase < MyCase
17
17
  # def setup
18
18
  # app_config.tap do |config|
19
- # config.workers do |workers|
20
- # workers.max = 5
19
+ # config.coins = 1000
20
+ # config.queue do |queue|
21
+ # queue.workers do |workers|
22
+ # workers.max = 5
23
+ # end
21
24
  # end
22
25
  # end
23
26
  # end
24
27
  #
25
- # def test_with_config
26
- # with_config(app_config, :workers) do |workers|
28
+ # def test_with_basic_value
29
+ # with_config(app_config) do |config|
30
+ # config.coins = 500
31
+ # end
32
+ # # global config reset to previous config
33
+ # end
34
+ #
35
+ # def test_queue_with_changed_workers
36
+ # with_config(app_config, :queue, :workers) do |workers|
27
37
  # workers.max = 1
28
38
  # # do stuff with modified config max value
29
39
  # end
@@ -31,12 +41,18 @@ class NestedConfig
31
41
  # end
32
42
  # end
33
43
  module WithConfig
34
- def with_config(config, key)
35
- current = config[key]
36
- backup = current.__clone__
37
- yield current
38
- ensure
39
- config[key] = backup
44
+ def with_config(config, *keys, &block)
45
+ current = config
46
+ while key = keys.shift
47
+ current = current[key]
48
+ unless current
49
+ raise ArgumentError, "nested key #{key.inspect} not found"
50
+ end
51
+ end
52
+ unless current.respond_to?(:__with_cloned__)
53
+ raise ArgumentError, "config #{current.inspect} can't be cloned"
54
+ end
55
+ current.__with_cloned__(&block)
40
56
  end
41
57
  end
42
58
  end
data/lib/nested_config.rb CHANGED
@@ -19,8 +19,11 @@ class NestedConfig
19
19
  @hash
20
20
  end
21
21
 
22
- def __clone__
23
- Marshal.load(Marshal.dump(self))
22
+ def __with_cloned__
23
+ backup = Marshal.load(Marshal.dump(@hash))
24
+ yield(self)
25
+ ensure
26
+ @hash = backup
24
27
  end
25
28
 
26
29
  def inspect
@@ -0,0 +1,84 @@
1
+ require 'helper'
2
+
3
+ class NestedConfigWithConfigTest < NestedConfigSpec
4
+ require 'nested_config/with_config'
5
+ include NestedConfig::WithConfig
6
+
7
+ context "with_config" do
8
+ let(:config) do
9
+ NestedConfig.new.tap do |config|
10
+ config.basic = 23
11
+ config.nest do |nest|
12
+ nest.level = 1
13
+ nest.deep do |deep|
14
+ deep.level = 2
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ test "one key" do
21
+ refute_change 'config.nest.level' do
22
+ with_config(config, :nest) do |nest|
23
+ nest.level = 2
24
+ assert_equal 2, config.nest.level
25
+ end
26
+ end
27
+ end
28
+
29
+ context "without a key" do
30
+ test "restore unnested" do
31
+ refute_change 'config.basic' do
32
+ with_config(config) do |c|
33
+ c.basic = 5
34
+ assert_equal 5, config.basic
35
+ end
36
+ end
37
+ end
38
+
39
+ test "restore nested" do
40
+ refute_change 'config.nest.level' do
41
+ with_config(config) do |c|
42
+ c.nest.level = 2
43
+ assert_equal 2, config.nest.level
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ context "with nested keys" do
50
+ test "restore valid nested key" do
51
+ refute_change 'config.nest.deep.level' do
52
+ with_config(config, :nest, :deep) do |c|
53
+ c.level = 23
54
+ assert_equal 23, config.nest.deep.level
55
+ end
56
+ end
57
+ end
58
+
59
+ test "key not found raises ArgumentError" do
60
+ error = assert_raises ArgumentError do
61
+ with_config(config, :some_key, :not_found) {}
62
+ end
63
+ assert_match %r{not found}, error.message
64
+ end
65
+
66
+ test "can't change basic value" do
67
+ error = assert_raises ArgumentError do
68
+ with_config(config, :basic) {}
69
+ end
70
+ assert_match %r{can't be cloned}, error.message
71
+ end
72
+ end
73
+ end
74
+
75
+ private
76
+
77
+ def refute_change(code)
78
+ before = eval(code)
79
+ yield
80
+ ensure
81
+ after = eval(code)
82
+ assert_equal before, after, "#{code} has changed to #{mu_pp(after)} from #{mu_pp(before)}"
83
+ end
84
+ end
@@ -125,9 +125,10 @@ class NestedConfigTest < NestedConfigSpec
125
125
  end
126
126
  end
127
127
 
128
- context "clone" do
128
+ context "__with_cloned__" do
129
129
  let(:config) do
130
130
  NestedConfig.new.tap do |config|
131
+ config.top_level = 1
131
132
  config.nest do |nest|
132
133
  nest.level = 1
133
134
  nest.deep do |deep|
@@ -137,20 +138,20 @@ class NestedConfigTest < NestedConfigSpec
137
138
  end
138
139
  end
139
140
 
140
- test "deep" do
141
- clone = config.__clone__
142
- clone.nest.level = 23
143
- clone.nest.deep.level = 5
144
-
145
- assert_equal 1, config.nest.level
146
- assert_equal 2, config.nest.deep.level
141
+ test "top level" do
142
+ config.__with_cloned__ do |c|
143
+ c.top_level = 2
144
+ assert_equal 2, config.top_level
145
+ end
146
+ assert_equal 1, config.top_level
147
147
  end
148
148
 
149
- test "cannot clone procs" do
150
- config.proc = proc {}
151
- assert_raises TypeError do
152
- config.__clone__
149
+ test "nested" do
150
+ config.__with_cloned__ do |c|
151
+ c.nest.deep.level = 23
152
+ assert_equal 23, config.nest.deep.level
153
153
  end
154
+ assert_equal 2, config.nest.deep.level
154
155
  end
155
156
  end
156
157
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nested_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-26 00:00:00.000000000 Z
12
+ date: 2012-08-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -79,9 +79,9 @@ files:
79
79
  - lib/nested_config/with_config.rb
80
80
  - nested_config.gemspec
81
81
  - test/helper.rb
82
- - test/nested_config_evaluate_once_test.rb
82
+ - test/nested_config/evaluate_once_test.rb
83
+ - test/nested_config/with_config_test.rb
83
84
  - test/nested_config_test.rb
84
- - test/nested_config_with_config_test.rb
85
85
  homepage: https://rubygems.org/gems/nested_config
86
86
  licenses: []
87
87
  post_install_message:
@@ -1,28 +0,0 @@
1
- require 'helper'
2
-
3
- class NestedConfigWithConfigTest < NestedConfigSpec
4
- require 'nested_config/with_config'
5
- include NestedConfig::WithConfig
6
-
7
- let(:config) do
8
- NestedConfig.new.tap do |config|
9
- config.nest do |nest|
10
- nest.level = 1
11
- nest.deep do |deep|
12
- deep.level = 2
13
- end
14
- end
15
- end
16
- end
17
-
18
- test "with_config with one key" do
19
- assert_equal 1, config.nest.level
20
-
21
- with_config(config, :nest) do |nest|
22
- nest.level = 2
23
- assert_equal 2, config.nest.level
24
- end
25
-
26
- assert_equal 1, config.nest.level
27
- end
28
- end