dry-config 1.2.2 → 1.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -0
- data/lib/dry/config/base.rb +29 -13
- data/lib/dry/config/version.rb +1 -1
- data/spec/dry/config/base_spec.rb +46 -13
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 930941affd9017b1baf6523ff3f7bdd1446556f5
|
4
|
+
data.tar.gz: fe728b093446b3db3cbb0c4433230572246e4ada
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f5a1875c5fd8705e613b950fc08ac9ef983ba6a5b52d392a23721e332806782978d3fa6fc7d0fbca57f49255caf6c611b04d65694a01af12536a2c771a94c7e7
|
7
|
+
data.tar.gz: edf646def929bb16f294ffef1badeabfbd38c0856f35e1a7e6680214ec6bc11cf2cc4bfb3d13e4378d65a827e1613e8bd2d2db9b528b6976338a97b8e50b2d25
|
data/README.md
CHANGED
@@ -140,6 +140,19 @@ interpolations:
|
|
140
140
|
```
|
141
141
|
|
142
142
|
## Options
|
143
|
+
|
144
|
+
The default configuration is provided below, override any of these values on construction:
|
145
|
+
```ruby
|
146
|
+
{
|
147
|
+
env: ENV, # default to ENV for interpolation
|
148
|
+
interpolation: true, # interpolate contents on read with env (above defaults to the current process's ENV)
|
149
|
+
symbolize: true, # provide symbol based key access for everything i.e. access :development instead of 'development' as a rule
|
150
|
+
unsymbolize_to_yaml: true, # on to_yaml or write_yaml_file unsymbolize keys (several external tools do not do well with symbolized keys i.e. write `development:` instead of `:development:`
|
151
|
+
default_configuration: {}, # seed configuration (file contents overlay this)
|
152
|
+
potential_environments:
|
153
|
+
[:development, :test, :staging, :production] # used for pruning (optional)
|
154
|
+
}
|
155
|
+
```
|
143
156
|
- Expected environments are `[:development, :test, :staging, :production]`. Expand or redefine `@potential_environments` these by overriding the `#initialize` or doing so in your optional `#seed_default_configuration`. This is used in the used in the overlay pruning process to prevent unused branches of configuration from showing up in the resolved configuration.
|
144
157
|
- An optional `#seed_default_configuration` allows you to provide a configuration base
|
145
158
|
- `#clear` will restore to the seed configuration, allowing you to `#load!` new settings.
|
data/lib/dry/config/base.rb
CHANGED
@@ -23,15 +23,17 @@ module Dry
|
|
23
23
|
|
24
24
|
def initialize(options = {})
|
25
25
|
@options = {
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
26
|
+
env: ENV, # default to ENV for interpolation
|
27
|
+
interpolation: true, # interpolate string contents on read with given ENV
|
28
|
+
symbolize: true, # provide symbol based key access for everything
|
29
|
+
unsymbolize_to_yaml: true, # on to_yaml or write_yaml_file unsymbolize keys
|
30
|
+
default_configuration: {}, # seed configuration
|
31
|
+
potential_environments: [:development, :test, :staging, :production] # used for pruning (optional)
|
30
32
|
}.merge options
|
31
33
|
|
32
34
|
@default_configuration = @options[:default_configuration]
|
33
35
|
|
34
|
-
# used for pruning initial base set. See #
|
36
|
+
# (optional) used for pruning initial base set. See #resolve_config
|
35
37
|
@potential_environments = @options[:potential_environments]
|
36
38
|
|
37
39
|
# setup a default configuration
|
@@ -47,8 +49,8 @@ module Dry
|
|
47
49
|
# raise 'Unspecified environment' if environment.nil?
|
48
50
|
raise 'Unspecified filename' if filenames.nil?
|
49
51
|
|
50
|
-
# ensure symbol
|
51
|
-
environment = environment.to_sym
|
52
|
+
# ensure symbol if symbolize?
|
53
|
+
environment = environment.to_sym if symbolize? && !environment.nil?
|
52
54
|
|
53
55
|
# save these in case we #reload
|
54
56
|
@environment = environment
|
@@ -95,16 +97,17 @@ module Dry
|
|
95
97
|
file = File.open(filename, 'r:bom|utf-8')
|
96
98
|
contents = file.read
|
97
99
|
|
100
|
+
env = @options[:env]
|
98
101
|
if interpolate?
|
99
102
|
# interpolate/substitute/expand ENV variables with the string contents before parsing
|
100
103
|
# bash - $VAR
|
101
|
-
contents = contents.gsub(/\$(\w+)/) {
|
104
|
+
contents = contents.gsub(/\$(\w+)/) { env[$1] }
|
102
105
|
# bash - ${VAR}
|
103
|
-
contents = contents.gsub(/\${(\w+)}/) {
|
104
|
-
# bash - ~ is
|
105
|
-
contents = contents.gsub(/(~)/) {
|
106
|
+
contents = contents.gsub(/\${(\w+)}/) { env[$1] }
|
107
|
+
# bash - ~ is env['HOME']
|
108
|
+
contents = contents.gsub(/(~)/) { env['HOME'] }
|
106
109
|
# ruby - #{VAR}
|
107
|
-
contents = contents.gsub(/\#{(\w+)}/) {
|
110
|
+
contents = contents.gsub(/\#{(\w+)}/) { env[$1] }
|
108
111
|
end
|
109
112
|
# now parse
|
110
113
|
config = Psych.load(contents, filename)
|
@@ -114,10 +117,19 @@ module Dry
|
|
114
117
|
config
|
115
118
|
end
|
116
119
|
|
120
|
+
def to_yaml
|
121
|
+
if unsymbolize_to_yaml?
|
122
|
+
config = @configuration.dup.deep_symbolize(true)
|
123
|
+
else
|
124
|
+
config = @configuration
|
125
|
+
end
|
126
|
+
|
127
|
+
Psych.dump(config)
|
128
|
+
end
|
117
129
|
|
118
130
|
def write_yaml_file(filename)
|
119
131
|
File.open(filename, 'w') do |file|
|
120
|
-
file.write(
|
132
|
+
file.write(to_yaml)
|
121
133
|
end
|
122
134
|
end
|
123
135
|
|
@@ -160,6 +172,10 @@ module Dry
|
|
160
172
|
@options[:symbolize]
|
161
173
|
end
|
162
174
|
|
175
|
+
def unsymbolize_to_yaml?
|
176
|
+
@options[:unsymbolize_to_yaml]
|
177
|
+
end
|
178
|
+
|
163
179
|
def interpolate?
|
164
180
|
@options[:interpolation]
|
165
181
|
end
|
data/lib/dry/config/version.rb
CHANGED
@@ -4,8 +4,6 @@ require 'singleton'
|
|
4
4
|
describe Dry::Config::Base do
|
5
5
|
|
6
6
|
class AcmeConfig < Dry::Config::Base
|
7
|
-
include Singleton
|
8
|
-
|
9
7
|
def initialize(options = {})
|
10
8
|
options = {
|
11
9
|
# seed the sensible defaults here (overridable)
|
@@ -22,19 +20,24 @@ describe Dry::Config::Base do
|
|
22
20
|
end
|
23
21
|
end
|
24
22
|
|
25
|
-
|
23
|
+
class SingletonAcmeConfig < AcmeConfig
|
24
|
+
include Singleton
|
25
|
+
end
|
26
|
+
|
27
|
+
subject(:acmeConfig) { AcmeConfig.new }
|
28
|
+
before(:each) {
|
29
|
+
acmeConfig.clear
|
30
|
+
}
|
26
31
|
|
27
32
|
it 'should work as a singleton' do
|
28
|
-
expect(
|
33
|
+
expect(SingletonAcmeConfig.instance).to equal(SingletonAcmeConfig.instance)
|
29
34
|
end
|
30
35
|
|
31
36
|
it 'should not raise error when key is not found' do
|
32
|
-
acmeConfig.clear
|
33
37
|
expect(acmeConfig.app).to be_nil
|
34
38
|
end
|
35
39
|
|
36
40
|
it 'should provide seed configuration' do
|
37
|
-
acmeConfig.clear
|
38
41
|
assert_common_seed_settings
|
39
42
|
expect(acmeConfig.options.length).to eql 0
|
40
43
|
|
@@ -45,13 +48,12 @@ describe Dry::Config::Base do
|
|
45
48
|
|
46
49
|
context 'hash delegation' do
|
47
50
|
it 'manupulated delegated methods setter/getter/include?' do
|
48
|
-
acmeConfig.clear
|
49
51
|
acmeConfig.load!(nil, config_file_path)
|
50
52
|
expect(acmeConfig.symbolize?).to be_truthy
|
51
53
|
|
52
54
|
acmeConfig['foo'] = 'bar'
|
53
|
-
expect(acmeConfig.configuration['foo']).to be_nil # no symbolization on direct access
|
54
|
-
expect(acmeConfig['foo']).to eq 'bar' # symbolization based on options
|
55
|
+
expect(acmeConfig.configuration['foo']).to be_nil # no symbolization manipulation to key on direct access to underlying hash
|
56
|
+
expect(acmeConfig['foo']).to eq 'bar' # symbolization manipulation of key based on options
|
55
57
|
|
56
58
|
expect(acmeConfig.configuration[:foo]).to eq 'bar' # setter symbolizes, this works
|
57
59
|
expect(acmeConfig[:foo]).to eq 'bar'
|
@@ -61,7 +63,7 @@ describe Dry::Config::Base do
|
|
61
63
|
end
|
62
64
|
|
63
65
|
it 'direct delegation' do
|
64
|
-
|
66
|
+
|
65
67
|
acmeConfig.load!(nil, config_file_path)
|
66
68
|
expect(acmeConfig.symbolize?).to be_truthy
|
67
69
|
|
@@ -76,10 +78,41 @@ describe Dry::Config::Base do
|
|
76
78
|
end
|
77
79
|
end
|
78
80
|
|
81
|
+
context 'unsymbolize_to_yaml' do
|
82
|
+
it 'defaults to unsymbolized' do
|
83
|
+
acmeConfig.load!(nil, config_file_path)
|
84
|
+
expect(acmeConfig.symbolize?).to be_truthy
|
85
|
+
expect(acmeConfig.unsymbolize_to_yaml?).to be_truthy
|
86
|
+
|
87
|
+
yaml = acmeConfig.to_yaml
|
88
|
+
# puts yaml
|
89
|
+
expect(yaml =~ /\:app\:/).to be_falsey
|
90
|
+
expect(yaml =~ /app\:/).to be_truthy
|
91
|
+
|
92
|
+
expect(yaml =~ /\:strategy\:/).to be_falsey
|
93
|
+
expect(yaml =~ /strategy\:/).to be_truthy
|
94
|
+
end
|
95
|
+
|
96
|
+
context 'symbolize: false' do
|
97
|
+
subject(:acmeConfig) { AcmeConfig.new(unsymbolize_to_yaml:false) }
|
98
|
+
|
99
|
+
it 'should leave keys symbolized' do
|
100
|
+
acmeConfig.load!(nil, config_file_path)
|
101
|
+
expect(acmeConfig.symbolize?).to be_truthy
|
102
|
+
expect(acmeConfig.unsymbolize_to_yaml?).to be_falsey
|
103
|
+
|
104
|
+
yaml = acmeConfig.to_yaml
|
105
|
+
# puts yaml
|
106
|
+
expect(yaml =~ /\:app\:/).to be_truthy
|
107
|
+
expect(yaml =~ /\:strategy\:/).to be_truthy
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
79
112
|
context 'when loading a single configuration file' do
|
80
113
|
|
81
114
|
it 'should read file with nil environment' do
|
82
|
-
|
115
|
+
|
83
116
|
acmeConfig.load!(nil, config_file_path)
|
84
117
|
|
85
118
|
assert_common_seed_settings
|
@@ -94,7 +127,7 @@ describe Dry::Config::Base do
|
|
94
127
|
end
|
95
128
|
|
96
129
|
it 'should override with development environment' do
|
97
|
-
|
130
|
+
|
98
131
|
acmeConfig.load!(:development, config_file_path)
|
99
132
|
|
100
133
|
# assert_common_seed_settings
|
@@ -111,7 +144,7 @@ describe Dry::Config::Base do
|
|
111
144
|
end
|
112
145
|
|
113
146
|
it 'should override with production environment' do
|
114
|
-
|
147
|
+
|
115
148
|
acmeConfig.load!(:production, config_file_path)
|
116
149
|
|
117
150
|
# assert_common_seed_settings
|