dry-configurable 0.7.0 → 0.8.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1 +0,0 @@
1
- 2.0.0-p247
@@ -1,91 +0,0 @@
1
- module Dry
2
- # Argument parser
3
- #
4
- # Passing and array or arguments, it will decide wich one are arguments
5
- # and which one are options.
6
- #
7
- # We have a limitation if setting the value without options, as a hash
8
- # having the same key as one of the valid options, will parse the value
9
- # as options.
10
- #
11
- # @example
12
- # p = Dry::Configurable::ArgumentParser.new(['db:sqlite', { reader: true })
13
- #
14
- # p.value # => 'db:sqlite'
15
- # p.options # => { reader: true }
16
- #
17
- # Dry::Configurable::ArgumentParser.call(['db:sqlite', { reader: true })
18
- # # => [ 'db:sqlite', { reader: true } ]
19
- module Configurable
20
- # @private
21
- class ArgumentParser
22
- VALID_OPTIONS = %i(reader).freeze
23
-
24
- def self.call(data)
25
- parsed = new(data)
26
- [parsed.value, parsed.options]
27
- end
28
-
29
- def initialize(data)
30
- @data = data
31
- end
32
-
33
- def value
34
- parse_args[:value]
35
- end
36
-
37
- def options
38
- parse_args[:options]
39
- end
40
-
41
- private
42
-
43
- attr_reader :data
44
-
45
- # @private
46
- def default_args
47
- { value: nil, options: {} }
48
- end
49
-
50
- # @private
51
- def parse_args
52
- return default_args if data.empty?
53
- if data.size > 1
54
- { value: data.first, options: check_options(data.last) }
55
- else
56
- default_args.merge(check_for_value_or_options(data.first))
57
- end
58
- end
59
-
60
- # @private
61
- def check_options(opts)
62
- return {} if opts.empty?
63
- opts.select { |k, _| VALID_OPTIONS.include?(k) }
64
- end
65
-
66
- # @private
67
- def check_for_value_or_options(args)
68
- case args
69
- when Hash
70
- parse_hash(args)
71
- else
72
- { value: args }
73
- end
74
- end
75
-
76
- # @private
77
- def parse_hash(args)
78
- if hash_include_options_key(args)
79
- { options: check_options(args) }
80
- else
81
- { value: args }
82
- end
83
- end
84
-
85
- # @private
86
- def hash_include_options_key(hash)
87
- hash.any? { |k, _| VALID_OPTIONS.include?(k) }
88
- end
89
- end
90
- end
91
- end
@@ -1,27 +0,0 @@
1
- module Dry
2
- module Configurable
3
- class Config
4
- # @private
5
- class Value
6
- # @private
7
- NONE = ::Object.new.freeze
8
-
9
- attr_reader :name, :processor
10
-
11
- def initialize(name, value, processor)
12
- @name = name.to_sym
13
- @value = value
14
- @processor = processor
15
- end
16
-
17
- def value
18
- none? ? nil : @value
19
- end
20
-
21
- def none?
22
- @value.equal?(::Dry::Configurable::Config::Value::NONE)
23
- end
24
- end
25
- end
26
- end
27
- end
@@ -1,33 +0,0 @@
1
- module Dry
2
- module Configurable
3
- # @private
4
- class NestedConfig
5
- def initialize(&block)
6
- klass = ::Class.new { extend ::Dry::Configurable }
7
- klass.instance_eval(&block)
8
- @klass = klass
9
- end
10
-
11
- # @private no, really...
12
- def create_config
13
- if @klass.instance_variables.include?(:@_config)
14
- @klass.__send__(:create_config)
15
- end
16
- end
17
-
18
- private
19
-
20
- def config
21
- @klass.config
22
- end
23
-
24
- def method_missing(method, *args, &block)
25
- config.respond_to?(method) ? config.public_send(method, *args, &block) : super
26
- end
27
-
28
- def respond_to_missing?(method, _include_private = false)
29
- config.respond_to?(method) || super
30
- end
31
- end
32
- end
33
- end
@@ -1,25 +0,0 @@
1
- RSpec.describe Dry::Configurable do
2
- context 'when extended' do
3
- let(:klass) do
4
- Class.new do
5
- extend Dry::Configurable
6
- end
7
- end
8
-
9
- it_behaves_like 'a configurable class'
10
- end
11
-
12
- context 'when extended then inherited' do
13
- let(:base_klass) do
14
- Class.new do
15
- extend Dry::Configurable
16
- end
17
- end
18
-
19
- let(:klass) do
20
- Class.new(base_klass)
21
- end
22
-
23
- it_behaves_like 'a configurable class'
24
- end
25
- end
@@ -1,92 +0,0 @@
1
- if ENV['COVERAGE'] == 'true' && RUBY_ENGINE == 'ruby' && RUBY_VERSION == '2.3.1'
2
- require 'simplecov'
3
-
4
- SimpleCov.start do
5
- add_filter '/spec/'
6
- end
7
- end
8
-
9
- # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
10
- RSpec.configure do |config|
11
- config.expect_with :rspec do |expectations|
12
- # This option will default to `true` in RSpec 4. It makes the `description`
13
- # and `failure_message` of custom matchers include text for helper methods
14
- # defined using `chain`, e.g.:
15
- # be_bigger_than(2).and_smaller_than(4).description
16
- # # => "be bigger than 2 and smaller than 4"
17
- # ...rather than:
18
- # # => "be bigger than 2"
19
- expectations.include_chain_clauses_in_custom_matcher_descriptions = true
20
- end
21
-
22
- config.mock_with :rspec do |mocks|
23
- # Prevents you from mocking or stubbing a method that does not exist on
24
- # a real object. This is generally recommended, and will default to
25
- # `true` in RSpec 4.
26
- mocks.verify_partial_doubles = true
27
- end
28
-
29
- # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
30
- # have no way to turn it off -- the option exists only for backwards
31
- # compatibility in RSpec 3). It causes shared context metadata to be
32
- # inherited by the metadata hash of host groups and examples, rather than
33
- # triggering implicit auto-inclusion in groups with matching metadata.
34
- config.shared_context_metadata_behavior = :apply_to_host_groups
35
-
36
- # This allows you to limit a spec run to individual examples or groups
37
- # you care about by tagging them with `:focus` metadata. When nothing
38
- # is tagged with `:focus`, all examples get run. RSpec also provides
39
- # aliases for `it`, `describe`, and `context` that include `:focus`
40
- # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
41
- config.filter_run_when_matching :focus
42
-
43
- # Allows RSpec to persist some state between runs in order to support
44
- # the `--only-failures` and `--next-failure` CLI options. We recommend
45
- # you configure your source control system to ignore this file.
46
- config.example_status_persistence_file_path = 'spec/examples.txt'
47
-
48
- # Limits the available syntax to the non-monkey patched syntax that is
49
- # recommended. For more details, see:
50
- # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
51
- # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
52
- # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
53
- config.disable_monkey_patching!
54
-
55
- # This setting enables warnings. It's recommended, but in some cases may
56
- # be too noisy due to issues in dependencies.
57
- config.warnings = true
58
-
59
- # Many RSpec users commonly either run the entire suite or an individual
60
- # file, and it's useful to allow more verbose output when running an
61
- # individual spec file.
62
- if config.files_to_run.one?
63
- # Use the documentation formatter for detailed output,
64
- # unless a formatter has already been configured
65
- # (e.g. via a command-line flag).
66
- config.default_formatter = 'doc'
67
- end
68
-
69
- # Print the n slowest examples and example groups at the
70
- # end of the spec run, to help surface which specs are running
71
- # particularly slow.
72
- config.profile_examples = 3
73
-
74
- # Run specs in random order to surface order dependencies. If you find an
75
- # order dependency and want to debug it, you can fix the order by providing
76
- # the seed, which is printed after each run.
77
- # --seed 1234
78
- config.order = :random
79
-
80
- # Seed global randomization in this process using the `--seed` CLI option.
81
- # Setting this allows you to use `--seed` to deterministically reproduce
82
- # test failures related to randomization by passing the same `--seed` value
83
- # as the one that triggered the failure.
84
- Kernel.srand config.seed
85
- end
86
-
87
- require 'dry/configurable'
88
- require 'dry/configurable/test_interface'
89
-
90
- Dir[Pathname(__FILE__).dirname.join('support/**/*.rb').to_s].each do |file|
91
- require file
92
- end
@@ -1,391 +0,0 @@
1
- RSpec.shared_examples 'a configurable class' do
2
- describe Dry::Configurable do
3
- describe 'settings' do
4
- context 'without processor option' do
5
- context 'without default value' do
6
- before do
7
- klass.setting :dsn
8
- end
9
-
10
- it 'returns nil' do
11
- expect(klass.config.dsn).to be(nil)
12
- end
13
- end
14
-
15
- context 'with default value' do
16
- context 'with a nil default value' do
17
- before do
18
- klass.setting :dsn, nil
19
- end
20
-
21
- it 'returns the default value' do
22
- expect(klass.config.dsn).to be(nil)
23
- end
24
- end
25
-
26
- context 'with a false default value' do
27
- before do
28
- klass.setting :dsn, false
29
- end
30
-
31
- it 'returns the default value' do
32
- expect(klass.config.dsn).to be(false)
33
- end
34
- end
35
-
36
- context 'with a string default value' do
37
- before do
38
- klass.setting :dsn, 'sqlite:memory'
39
- end
40
-
41
- it 'returns the default value' do
42
- expect(klass.config.dsn).to eq('sqlite:memory')
43
- end
44
- end
45
-
46
- context 'with a hash default value' do
47
- before do
48
- klass.setting :db_config, {
49
- user: 'root',
50
- password: ''
51
- }
52
- end
53
-
54
- it 'returns the default value' do
55
- expect(klass.config.db_config).to eq(
56
- user: 'root',
57
- password: ''
58
- )
59
- end
60
- end
61
- end
62
-
63
- context 'reader option' do
64
- context 'without passing option' do
65
- before do
66
- klass.setting :dsn
67
- end
68
-
69
- before do
70
- klass.configure do |config|
71
- config.dsn = 'jdbc:sqlite:memory'
72
- end
73
- end
74
-
75
- it 'will not create a getter method' do
76
- expect(klass.respond_to?(:dsn)).to be_falsey
77
- end
78
- end
79
-
80
- context 'with hash as value ' do
81
- before do
82
- klass.setting :dsn, { foo: 'bar' }, reader: true
83
- end
84
-
85
- it 'will create a getter method' do
86
- expect(klass.dsn).to eq(foo: 'bar')
87
- expect(klass.respond_to?(:dsn)).to be_truthy
88
- end
89
- end
90
-
91
- context 'with option set to true' do
92
- before do
93
- klass.setting :dsn, 'testing', reader: true
94
- end
95
-
96
- it 'will create a getter method' do
97
- expect(klass.dsn).to eq 'testing'
98
- expect(klass.respond_to?(:dsn)).to be_truthy
99
- end
100
- end
101
-
102
- context 'with nested configuration' do
103
- before do
104
- klass.setting :dsn, reader: true do
105
- setting :pool, 5
106
- end
107
- end
108
-
109
- it 'will create a nested getter method' do
110
- expect(klass.dsn.pool).to eq 5
111
- end
112
- end
113
-
114
- context 'with processor' do
115
- context 'with default value' do
116
- before do
117
- klass.setting(:dsn, 'memory', reader: true) { |dsn| "sqlite:#{dsn}" }
118
- end
119
-
120
- it 'returns the default value' do
121
- expect(klass.dsn).to eq('sqlite:memory')
122
- end
123
- end
124
-
125
- context 'without default value' do
126
- before do
127
- klass.setting(:dsn, reader: true) { |dsn| "sqlite:#{dsn}" }
128
- end
129
-
130
- it 'returns the default value' do
131
- expect(klass.dsn).to eq(nil)
132
- end
133
- end
134
- end
135
- end
136
-
137
- context 'nested configuration' do
138
- before do
139
- klass.setting :database do
140
- setting :dsn, 'sqlite:memory'
141
- end
142
- end
143
-
144
- it 'returns the default value' do
145
- expect(klass.config.database.dsn).to eq('sqlite:memory')
146
- end
147
- end
148
- end
149
-
150
- context 'with processor' do
151
- context 'without default value' do
152
- before do
153
- klass.setting(:dsn) { |dsn| "sqlite:#{dsn}" }
154
- end
155
-
156
- it 'returns nil' do
157
- expect(klass.config.dsn).to be(nil)
158
- end
159
- end
160
-
161
- context 'with default value' do
162
- before do
163
- klass.setting(:dsn, 'memory') { |dsn| "sqlite:#{dsn}" }
164
- end
165
-
166
- it 'returns the default value' do
167
- expect(klass.config.dsn).to eq('sqlite:memory')
168
- end
169
- end
170
-
171
- context 'nested configuration' do
172
- before do
173
- klass.setting :database do
174
- setting(:dsn, 'memory') { |dsn| "sqlite:#{dsn}" }
175
- end
176
- end
177
-
178
- it 'returns the default value' do
179
- expect(klass.config.database.dsn).to eq('sqlite:memory')
180
- end
181
- end
182
- end
183
- end
184
-
185
- describe 'configuration' do
186
- context 'without nesting' do
187
- context 'without processor' do
188
- before do
189
- klass.setting :dsn, 'sqlite:memory'
190
- end
191
-
192
- before do
193
- klass.configure do |config|
194
- config.dsn = 'jdbc:sqlite:memory'
195
- end
196
- end
197
-
198
- it 'updates the config value' do
199
- expect(klass.config.dsn).to eq('jdbc:sqlite:memory')
200
- end
201
- end
202
-
203
- context 'with processor' do
204
- before do
205
- klass.setting(:dsn, 'sqlite') { |dsn| "#{dsn}:memory" }
206
- end
207
-
208
- before do
209
- klass.configure do |config|
210
- config.dsn = 'jdbc:sqlite'
211
- end
212
- end
213
-
214
- it 'updates the config value' do
215
- expect(klass.config.dsn).to eq('jdbc:sqlite:memory')
216
- end
217
- end
218
- end
219
-
220
- context 'with nesting' do
221
- context 'without processor' do
222
- before do
223
- klass.setting :database do
224
- setting :dsn, 'sqlite:memory'
225
- end
226
-
227
- klass.configure do |config|
228
- config.database.dsn = 'jdbc:sqlite:memory'
229
- end
230
- end
231
-
232
- it 'updates the config value' do
233
- expect(klass.config.database.dsn).to eq('jdbc:sqlite:memory')
234
- end
235
- end
236
-
237
- context 'with processor' do
238
- before do
239
- klass.setting :database do
240
- setting(:dsn, 'sqlite') { |dsn| "#{dsn}:memory" }
241
- end
242
-
243
- klass.configure do |config|
244
- config.database.dsn = 'jdbc:sqlite'
245
- end
246
- end
247
-
248
- it 'updates the config value' do
249
- expect(klass.config.database.dsn).to eq('jdbc:sqlite:memory')
250
- end
251
- end
252
- end
253
-
254
- context 'when finalized' do
255
- before do
256
- klass.setting :dsn
257
- klass.configure do |config|
258
- config.dsn = 'jdbc:sqlite'
259
- end
260
- klass.finalize!
261
- end
262
-
263
- it 'disallows modification' do
264
- expect do
265
- klass.configure do |config|
266
- config.dsn = 'jdbc:sqlite'
267
- end
268
- end.to raise_error(Dry::Configurable::FrozenConfig, 'Cannot modify frozen config')
269
- end
270
-
271
- it 'disallows direct modification on config' do
272
- expect do
273
- klass.config.dsn = 'jdbc:sqlite:memory'
274
- end.to raise_error(Dry::Configurable::FrozenConfig, 'Cannot modify frozen config')
275
- end
276
- end
277
-
278
- context 'when inherited' do
279
- context 'without processor' do
280
- before do
281
- klass.setting :dsn
282
- klass.configure do |config|
283
- config.dsn = 'jdbc:sqlite:memory'
284
- end
285
- end
286
-
287
- subject!(:subclass) { Class.new(klass) }
288
-
289
- it 'retains its configuration' do
290
- expect(subclass.config.dsn).to eq('jdbc:sqlite:memory')
291
- end
292
-
293
- context 'when the inherited config is modified' do
294
- before do
295
- subclass.configure do |config|
296
- config.dsn = 'jdbc:sqlite:file'
297
- end
298
- end
299
-
300
- it 'does not modify the original' do
301
- expect(klass.config.dsn).to eq('jdbc:sqlite:memory')
302
- expect(subclass.config.dsn).to eq('jdbc:sqlite:file')
303
- end
304
- end
305
- end
306
-
307
- context 'with processor' do
308
- before do
309
- klass.setting(:dsn) { |dsn| "#{dsn}:memory" }
310
- klass.configure do |config|
311
- config.dsn = 'jdbc:sqlite'
312
- end
313
- end
314
-
315
- subject!(:subclass) { Class.new(klass) }
316
-
317
- it 'retains its configuration' do
318
- expect(subclass.config.dsn).to eq('jdbc:sqlite:memory')
319
- end
320
-
321
- context 'when the inherited config is modified' do
322
- before do
323
- subclass.configure do |config|
324
- config.dsn = 'sqlite'
325
- end
326
- end
327
-
328
- it 'does not modify the original' do
329
- expect(klass.config.dsn).to eq('jdbc:sqlite:memory')
330
- expect(subclass.config.dsn).to eq('sqlite:memory')
331
- end
332
- end
333
- end
334
-
335
- context 'when the inherited settings are modified' do
336
- before do
337
- klass.setting :dsn
338
- subclass.setting :db
339
- klass.configure do |config|
340
- config.dsn = 'jdbc:sqlite:memory'
341
- end
342
- end
343
-
344
- subject!(:subclass) { Class.new(klass) }
345
-
346
- it 'does not modify the original' do
347
- expect(klass.settings).to_not include(:db)
348
- end
349
- end
350
- end
351
- end
352
-
353
- context 'Test Interface' do
354
- before { klass.enable_test_interface }
355
-
356
- describe 'reset_config' do
357
- before do
358
- klass.setting :dsn, nil
359
- klass.setting :pool do
360
- setting :size, nil
361
- end
362
-
363
- klass.configure do |config|
364
- config.dsn = 'sqlite:memory'
365
- config.pool.size = 5
366
- end
367
-
368
- klass.reset_config
369
- end
370
-
371
- it 'resets configuration to default values' do
372
- expect(klass.config.dsn).to be_nil
373
- expect(klass.config.pool.size).to be_nil
374
- end
375
- end
376
- end
377
-
378
- context 'Try to set new value after config has been created' do
379
- before do
380
- klass.setting :dsn, 'sqlite:memory'
381
- klass.config
382
- end
383
-
384
- it 'raise an exception' do
385
- expect { klass.setting :pool, 5 }.to raise_error(
386
- Dry::Configurable::AlreadyDefinedConfig
387
- )
388
- end
389
- end
390
- end
391
- end