nested_config 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -62,6 +62,52 @@ The initial access to key +country_list+ calls (via +call+ method) the proc and
62
62
  Subsequent calls just fetch the replaced value.
63
63
 
64
64
 
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
+ === Modifying config using NestedConfig::WithConfig
77
+
78
+ Values of NestedConfig can be temporalily modified.
79
+
80
+ This can be used in tests modifying a global application config inside a block:
81
+
82
+ require 'nested_config/with_config'
83
+
84
+ class MyCase < MiniTest::TestCase
85
+ include NestedConfig::WithConfig
86
+
87
+ def with_app_config(key, &block)
88
+ with_config(MyApp.config, key, &block)
89
+ end
90
+ end
91
+
92
+ class SomeCase < MyCase
93
+ def setup
94
+ app_config.tap do |config|
95
+ config.workers do |workers|
96
+ workers.max = 5
97
+ end
98
+ end
99
+ end
100
+
101
+ def test_with_config
102
+ with_app_config(:workers) do |workers|
103
+ workers.max = 1
104
+ # do stuff with modified config max value
105
+ end
106
+ # global config reset to previous config
107
+ end
108
+ end
109
+
110
+
65
111
  === Key names
66
112
 
67
113
  *Note*: NestedConfig is not a blank slate so you CANNOT use defined method names as keys like +object_id+.
@@ -1,3 +1,3 @@
1
1
  class NestedConfig
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,42 @@
1
+ class NestedConfig
2
+ # Test helper for modifying config inside a block.
3
+ #
4
+ # == Example
5
+ #
6
+ # require 'nested_config/with_config'
7
+ #
8
+ # class MyCase < MiniTest::TestCase
9
+ # include NestedConfig::WithConfig
10
+ #
11
+ # def app_config
12
+ # MyApp.config # global
13
+ # end
14
+ # end
15
+ #
16
+ # class SomeCase < MyCase
17
+ # def setup
18
+ # app_config.tap do |config|
19
+ # config.workers do |workers|
20
+ # workers.max = 5
21
+ # end
22
+ # end
23
+ # end
24
+ #
25
+ # def test_with_config
26
+ # with_config(app_config, :workers) do |workers|
27
+ # workers.max = 1
28
+ # # do stuff with modified config max value
29
+ # end
30
+ # # global config reset to previous config
31
+ # end
32
+ # end
33
+ 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
40
+ end
41
+ end
42
+ end
data/lib/nested_config.rb CHANGED
@@ -19,6 +19,10 @@ class NestedConfig
19
19
  @hash
20
20
  end
21
21
 
22
+ def __clone__
23
+ Marshal.load(Marshal.dump(self))
24
+ end
25
+
22
26
  def inspect
23
27
  @hash
24
28
  end
@@ -124,5 +124,34 @@ class NestedConfigTest < NestedConfigSpec
124
124
  assert_equal :bar!, c.users.foo?
125
125
  end
126
126
  end
127
+
128
+ context "clone" do
129
+ let(:config) do
130
+ NestedConfig.new.tap do |config|
131
+ config.nest do |nest|
132
+ nest.level = 1
133
+ nest.deep do |deep|
134
+ deep.level = 2
135
+ end
136
+ end
137
+ end
138
+ end
139
+
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
147
+ end
148
+
149
+ test "cannot clone procs" do
150
+ config.proc = proc {}
151
+ assert_raises TypeError do
152
+ config.__clone__
153
+ end
154
+ end
155
+ end
127
156
  end
128
157
  end
@@ -0,0 +1,28 @@
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
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.2.1
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -76,11 +76,12 @@ files:
76
76
  - lib/nested_config.rb
77
77
  - lib/nested_config/evaluate_once.rb
78
78
  - lib/nested_config/version.rb
79
+ - lib/nested_config/with_config.rb
79
80
  - nested_config.gemspec
80
81
  - test/helper.rb
81
- - test/neopoly_config_evaluate_once_test.rb
82
82
  - test/nested_config_evaluate_once_test.rb
83
83
  - test/nested_config_test.rb
84
+ - test/nested_config_with_config_test.rb
84
85
  homepage: https://rubygems.org/gems/nested_config
85
86
  licenses: []
86
87
  post_install_message:
@@ -1,42 +0,0 @@
1
- require 'helper'
2
-
3
- class NestedConfigEvaluateOnceTest < NestedConfigSpec
4
- let(:config) do
5
- Class.new(NestedConfig) do
6
- include NestedConfig::EvaluateOnce
7
- end.new
8
- end
9
-
10
- context "evaluates once w/o argument" do
11
- context "w/o argument" do
12
- before do
13
- config.startup_time = proc { Time.now }
14
- end
15
-
16
- test "has time value" do
17
- assert_instance_of Time, config.startup_time
18
- end
19
-
20
- test "value does not change" do
21
- n = config.startup_time
22
- assert_same n, config.startup_time
23
- end
24
- end
25
-
26
- context "with argument" do
27
- before do
28
- config.env = proc { |e| e.to_s.upcase }
29
- config.env(:development)
30
- end
31
-
32
- test "replaces original value" do
33
- assert_equal "DEVELOPMENT", config.env
34
- end
35
-
36
- test "value does not change" do
37
- env = config.env
38
- assert_same env, config.env
39
- end
40
- end
41
- end
42
- end