dry-configurable 0.6.2 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +13 -0
- data/LICENSE +1 -1
- data/lib/dry/configurable.rb +17 -1
- data/lib/dry/configurable/config.rb +12 -1
- data/lib/dry/configurable/error.rb +1 -0
- data/lib/dry/configurable/version.rb +1 -1
- data/spec/support/shared_examples/configurable.rb +34 -0
- data/spec/unit/dry/configurable/config_spec.rb +12 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eeb18b428640615a653b1e669bcbedf1130f83ef
|
4
|
+
data.tar.gz: 294ec402fe8dbfb378c6a5e1d5d442c49ac7baf1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f3c268381efd9f202447a9785873ea151e5ce4edc004c87006a0c5b91165b3b29827d99799cf2f75a386214c5906b09d9b12983aec05502ad88591f1fa702896
|
7
|
+
data.tar.gz: fd8c289e25a8fa28ec55e1f69ec5f569e2b6dbf0506ca7df49a80c6277c99510bc0505dc7c063fd97144f2723105bba62448472bc2db73c941dce1bca8db9cbf
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
## Unreleased
|
2
|
+
|
3
|
+
## Added
|
4
|
+
|
5
|
+
* Introduce `Configurable.finalize!` which freezes config and its dependencies ([qcam](https://github.com/qcam))
|
6
|
+
|
7
|
+
## Fixed
|
8
|
+
|
9
|
+
* Allow for boolean false as default setting value ([yuszuv](https://github.com/yuszuv))
|
10
|
+
* Convert nested configs to nested hashes with `Config#to_h` ([saverio-kantox](https://github.com/saverio-kantox))
|
11
|
+
* Disallow modification on frozen config ([qcam](https://github.com/qcam))
|
12
|
+
|
13
|
+
[Compare v0.6.2...HEAD](https://github.com/dry-rb/dry-configurable/compare/v0.6.2...HEAD)
|
data/LICENSE
CHANGED
data/lib/dry/configurable.rb
CHANGED
@@ -66,9 +66,20 @@ module Dry
|
|
66
66
|
#
|
67
67
|
# @api public
|
68
68
|
def configure
|
69
|
+
raise_frozen_config if frozen?
|
69
70
|
yield(config) if block_given?
|
70
71
|
end
|
71
72
|
|
73
|
+
# Finalize and freeze configuration
|
74
|
+
#
|
75
|
+
# @return [Dry::Configurable::Config]
|
76
|
+
#
|
77
|
+
# @api public
|
78
|
+
def finalize!
|
79
|
+
freeze
|
80
|
+
config.finalize!
|
81
|
+
end
|
82
|
+
|
72
83
|
# Add a setting to the configuration
|
73
84
|
#
|
74
85
|
# @param [Mixed] key
|
@@ -96,7 +107,7 @@ module Dry
|
|
96
107
|
|
97
108
|
_settings << ::Dry::Configurable::Config::Value.new(
|
98
109
|
key,
|
99
|
-
value
|
110
|
+
!value.nil? ? value : ::Dry::Configurable::Config::Value::NONE,
|
100
111
|
processor || ::Dry::Configurable::Config::DEFAULT_PROCESSOR
|
101
112
|
)
|
102
113
|
store_reader_options(key, options) if options.any?
|
@@ -151,6 +162,11 @@ module Dry
|
|
151
162
|
"Cannot add setting +#{key}+, #{self} is already configured"
|
152
163
|
end
|
153
164
|
|
165
|
+
# @private
|
166
|
+
def raise_frozen_config
|
167
|
+
raise FrozenConfig, 'Cannot modify frozen config'
|
168
|
+
end
|
169
|
+
|
154
170
|
# @private
|
155
171
|
def store_reader_options(key, options)
|
156
172
|
_reader_attributes << key if options.fetch(:reader, false)
|
@@ -13,6 +13,7 @@ module Dry
|
|
13
13
|
end
|
14
14
|
|
15
15
|
klass.__send__(:define_method, "#{setting.name}=") do |value|
|
16
|
+
raise_frozen_config if frozen?
|
16
17
|
@config[setting.name] = setting.processor.call(value)
|
17
18
|
end
|
18
19
|
end
|
@@ -44,11 +45,17 @@ module Dry
|
|
44
45
|
clone
|
45
46
|
end
|
46
47
|
|
48
|
+
def finalize!
|
49
|
+
@config.freeze
|
50
|
+
freeze
|
51
|
+
end
|
52
|
+
|
47
53
|
def to_h
|
48
54
|
@config.each_with_object({}) do |tuple, hash|
|
49
55
|
key, value = tuple
|
50
56
|
|
51
|
-
|
57
|
+
case value
|
58
|
+
when ::Dry::Configurable::Config, ::Dry::Configurable::NestedConfig
|
52
59
|
hash[key] = value.to_h
|
53
60
|
else
|
54
61
|
hash[key] = value
|
@@ -69,6 +76,10 @@ module Dry
|
|
69
76
|
|
70
77
|
private
|
71
78
|
|
79
|
+
def raise_frozen_config
|
80
|
+
raise FrozenConfig, 'Cannot modify frozen config'
|
81
|
+
end
|
82
|
+
|
72
83
|
def raise_unknown_setting_error(name)
|
73
84
|
raise ArgumentError, "+#{name}+ is not a setting name"
|
74
85
|
end
|
@@ -23,6 +23,16 @@ RSpec.shared_examples 'a configurable class' do
|
|
23
23
|
end
|
24
24
|
end
|
25
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
|
+
|
26
36
|
context 'with a string default value' do
|
27
37
|
before do
|
28
38
|
klass.setting :dsn, 'sqlite:memory'
|
@@ -241,6 +251,30 @@ RSpec.shared_examples 'a configurable class' do
|
|
241
251
|
end
|
242
252
|
end
|
243
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
|
+
|
244
278
|
context 'when inherited' do
|
245
279
|
context 'without processor' do
|
246
280
|
before do
|
@@ -53,6 +53,15 @@ RSpec.describe Dry::Configurable::Config do
|
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
|
+
describe '#finalize!' do
|
57
|
+
subject!(:dup) { config.finalize! }
|
58
|
+
|
59
|
+
it 'freezes itself and the config' do
|
60
|
+
expect { config.user = 'whoami' }
|
61
|
+
.to raise_error(Dry::Configurable::FrozenConfig, 'Cannot modify frozen config')
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
56
65
|
describe '#to_h' do
|
57
66
|
subject! { config.to_h }
|
58
67
|
|
@@ -68,7 +77,9 @@ RSpec.describe Dry::Configurable::Config do
|
|
68
77
|
|
69
78
|
context 'with nesting' do
|
70
79
|
let(:nested_setting) do
|
71
|
-
|
80
|
+
::Dry::Configurable::NestedConfig.new do
|
81
|
+
setting(:bar, 'baz') { |v| v }
|
82
|
+
end.tap(&:create_config)
|
72
83
|
end
|
73
84
|
let(:settings) do
|
74
85
|
[
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dry-configurable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andy Holland
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-04-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: concurrent-ruby
|
@@ -78,6 +78,7 @@ files:
|
|
78
78
|
- .rspec
|
79
79
|
- .ruby-version
|
80
80
|
- .travis.yml
|
81
|
+
- CHANGELOG.md
|
81
82
|
- Gemfile
|
82
83
|
- LICENSE
|
83
84
|
- README.md
|
@@ -119,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
119
120
|
version: '0'
|
120
121
|
requirements: []
|
121
122
|
rubyforge_project:
|
122
|
-
rubygems_version: 2.
|
123
|
+
rubygems_version: 2.4.8
|
123
124
|
signing_key:
|
124
125
|
specification_version: 4
|
125
126
|
summary: A mixin to add configuration functionality to your classes
|