fig_tree 0.0.1 → 0.0.4

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
  SHA256:
3
- metadata.gz: 359a81208b3825233a55fd631b7b4e9c00ba34451286679c5ceb2c93cc50787d
4
- data.tar.gz: 58f8342253d10c7d37575a05761b05f89d9bff3579f85ba682cc5341a0b55b29
3
+ metadata.gz: 05e904a52d3b7edb7d02d48b4d063db396de7918b6b1de8f277cfcc6fcbe7cb2
4
+ data.tar.gz: 58334d1b630a134a2e08cefc03d1131d130ff550d9d0a89e350f57ccdb676002
5
5
  SHA512:
6
- metadata.gz: 6ec628b03063fc54940a2a6503362d4c13937541865a786f1a1197719b0b4e4e08a4eb24c07acf32cd99237010ef53b7d2247c224f7900da41567a8b414d30c4
7
- data.tar.gz: d29f30d43a4bc821da27b36c5c3c3af7e983780eb758f6baa5a55e638427a94844a2284341977e3a75d0792ab8a725cc9c80e9c20608d61e5ebf1c02bb015d08
6
+ metadata.gz: ef3fff25ee51e1bc67f5657a3574e303ca2346cf651379261ca915878d9b3d9e679b5acc6778b995bdac8dc1c412724c9540926563e020983d570ad53103493a
7
+ data.tar.gz: 0b80fb3feee7165b47a7ce1121cd7f374ba40fa2687adfbc94e7237692f78c36c3698e72643666e96da6d208a329b784e0789aaec7352e1461cca4e36d712d5d
data/CHANGELOG.md CHANGED
@@ -7,5 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## UNRELEASED
9
9
 
10
- ## [0.0.1] - 2021-02-17
10
+ ## [0.0.4] - 2022-05-16
11
+ - Added `with_config` method for testing.
12
+
13
+ ## [0.0.2] - 2021-02-17
11
14
  - Initial release.
data/README.md CHANGED
@@ -188,6 +188,18 @@ FileCreator.user_read = true
188
188
  # config.user_read is deprecated - use config.file_options.user_read
189
189
  ```
190
190
 
191
+ ## Testing
192
+
193
+ You can use the `with_config` method to test your code with specific config values that get
194
+ reset after the block is done executing:
195
+
196
+ ```ruby
197
+ FileCreator.with_config('file_options.group_name' => 'root') do
198
+ # test this code
199
+ end
200
+ # file_options.group_name will be set back to the original value afterwards
201
+ ```
202
+
191
203
  ## Contributing
192
204
 
193
205
  Bug reports and pull requests are welcome on GitHub at https://github.com/flipp-oss/fig_tree .
data/fig_tree.gemspec CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
12
12
  spec.summary = 'Configuration framework for Ruby.'
13
13
  spec.homepage = ''
14
14
  spec.license = 'Apache-2.0'
15
- spec.required_ruby_version = '2.3'
15
+ spec.required_ruby_version = '>= 2.3'
16
16
 
17
17
  spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
18
18
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FigTree
4
- VERSION = '0.0.1'
4
+ VERSION = '0.0.4'
5
5
  end
data/lib/fig_tree.rb CHANGED
@@ -241,6 +241,34 @@ module FigTree
241
241
  @config ||= ConfigStruct.new('config')
242
242
  end
243
243
 
244
+ # Evaluate a block with the given configuration values. Reset back to the original values
245
+ # when done.
246
+ # @param values [Hash]
247
+ def with_config(values={}, &block) # rubocop:disable Metrics/AbcSize
248
+ set_value = lambda do |k, v|
249
+ obj = self.config
250
+ tokens = k.split('.')
251
+ tokens[0..-2].each do |token|
252
+ obj = obj.send(token)
253
+ end
254
+ obj.send(:"#{tokens.last}=", v)
255
+ end
256
+
257
+ get_value = lambda do |k|
258
+ obj = self.config
259
+ tokens = k.split('.')
260
+ tokens.each do |token|
261
+ obj = obj.send(token)
262
+ end
263
+ obj
264
+ end
265
+
266
+ old_values = values.keys.map { |k| [k, get_value.call(k)] }.to_h
267
+ values.each { |k, v| set_value.call(k, v) }
268
+ block.call
269
+ old_values.each { |k, v| set_value.call(k, v) }
270
+ end
271
+
244
272
  # Pass a block to run after configuration is done.
245
273
  def after_configure(&block)
246
274
  mod = self
@@ -0,0 +1,138 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fig_tree'
4
+
5
+ # :nodoc:
6
+ class MyConfig
7
+ include FigTree
8
+
9
+ define_settings do
10
+ setting :set1
11
+ setting :set2, 'hi mom'
12
+ setting :group do
13
+ setting :set3, default_proc: proc { false }
14
+ setting :set5, (proc { 5 })
15
+ end
16
+
17
+ setting_object :listy do
18
+ setting :list1, 10
19
+ setting :list2, 5
20
+ end
21
+ end
22
+ end
23
+
24
+ describe FigTree do
25
+ it 'should configure correctly with default values' do
26
+ expect(MyConfig.config.set1).to be_nil
27
+ expect(MyConfig.config.set2).to eq('hi mom')
28
+ expect(MyConfig.config.group.set3).to eq(false)
29
+ expect(MyConfig.config.listy_objects).to be_empty
30
+ expect { MyConfig.config.blah }.to raise_error(NameError)
31
+ expect { MyConfig.config.group.set4 }.to raise_error(NameError)
32
+ end
33
+
34
+ it 'should not call the proc until it has to' do
35
+ num_calls = 0
36
+ value_proc = proc do
37
+ num_calls += 1
38
+ num_calls
39
+ end
40
+ MyConfig.define_settings do
41
+ setting :set_with_proc, default_proc: value_proc
42
+ end
43
+ expect(num_calls).to eq(0)
44
+ expect(MyConfig.config.set_with_proc).to eq(1)
45
+ # calling twice should not call the proc again
46
+ expect(MyConfig.config.set_with_proc).to eq(1)
47
+ expect(num_calls).to eq(1)
48
+ end
49
+
50
+ it "should raise error when setting configs that don't exist" do
51
+ expect { MyConfig.configure { set15('some_value') } }.to raise_error(NameError)
52
+ end
53
+
54
+ it 'should add values' do
55
+ MyConfig.configure do |config|
56
+ config.set1 = 5 # config.x syntax
57
+ set2 nil # method_missing syntax
58
+ config.group.set3 = true
59
+ end
60
+
61
+ # second configure should not blow anything away
62
+ MyConfig.configure do
63
+ listy do
64
+ list1 0
65
+ list2 1
66
+ end
67
+ listy do
68
+ list1 100
69
+ list2 200
70
+ end
71
+ end
72
+
73
+ expect(MyConfig.config.set1).to eq(5)
74
+ expect(MyConfig.config.set2).to be_nil
75
+ expect(MyConfig.config.group.set3).to eq(true)
76
+ expect(MyConfig.config.listy_objects.map(&:to_h)).
77
+ to eq([
78
+ { list1: 0, list2: 1 },
79
+ { list1: 100, list2: 200 }
80
+ ])
81
+
82
+ # test reset!
83
+ MyConfig.config.reset!
84
+ expect(MyConfig.config.set1).to be_nil
85
+ expect(MyConfig.config.set2).to eq('hi mom')
86
+ expect(MyConfig.config.group.set3).to eq(false)
87
+ expect(MyConfig.config.listy_objects).to be_empty
88
+ end
89
+
90
+ it 'should add with block syntax' do
91
+ MyConfig.configure do
92
+ group do
93
+ set5(proc { 10 })
94
+ end
95
+ end
96
+ expect(MyConfig.config.group.set5.call).to eq(10)
97
+ end
98
+
99
+ it 'should add or redefine settings' do
100
+ MyConfig.define_settings do
101
+ setting :group do
102
+ setting :set6, 15
103
+ setting :set5, (proc { 15 })
104
+ end
105
+ setting_object :notey do
106
+ setting :note_title, 'some-title'
107
+ end
108
+ end
109
+
110
+ expect(MyConfig.config.group.set6).to eq(15)
111
+ expect(MyConfig.config.group.set5.call).to eq(15)
112
+ expect(MyConfig.config.listy_objects).to be_empty
113
+ expect(MyConfig.config.notey_objects).to be_empty
114
+
115
+ MyConfig.configure do
116
+ notey do
117
+ note_title 'hi mom'
118
+ end
119
+ listy do
120
+ list1 0
121
+ end
122
+ end
123
+ expect(MyConfig.config.notey_objects.size).to eq(1)
124
+ expect(MyConfig.config.notey_objects.first.note_title).to eq('hi mom')
125
+ expect(MyConfig.config.listy_objects.size).to eq(1)
126
+ expect(MyConfig.config.listy_objects.first.list1).to eq(0)
127
+
128
+ # This should not remove any keys
129
+ MyConfig.define_settings do
130
+ setting :group do
131
+ setting :set6, 20
132
+ end
133
+ end
134
+ expect(MyConfig.config.group.set6).to eq(20)
135
+ expect(MyConfig.config.group.set5.call).to eq(15)
136
+ end
137
+
138
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fig_tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Orner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-04 00:00:00.000000000 Z
11
+ date: 2022-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -144,6 +144,7 @@ files:
144
144
  - fig_tree.gemspec
145
145
  - lib/fig_tree.rb
146
146
  - lib/fig_tree/version.rb
147
+ - spec/fig_tree_spec.rb
147
148
  - spec/spec_helper.rb
148
149
  - support/flipp-logo.png
149
150
  homepage: ''
@@ -156,7 +157,7 @@ require_paths:
156
157
  - lib
157
158
  required_ruby_version: !ruby/object:Gem::Requirement
158
159
  requirements:
159
- - - '='
160
+ - - ">="
160
161
  - !ruby/object:Gem::Version
161
162
  version: '2.3'
162
163
  required_rubygems_version: !ruby/object:Gem::Requirement
@@ -165,9 +166,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
165
166
  - !ruby/object:Gem::Version
166
167
  version: '0'
167
168
  requirements: []
168
- rubygems_version: 3.2.3
169
+ rubygems_version: 3.3.7
169
170
  signing_key:
170
171
  specification_version: 4
171
172
  summary: Configuration framework for Ruby.
172
173
  test_files:
174
+ - spec/fig_tree_spec.rb
173
175
  - spec/spec_helper.rb