dry-config 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +14 -3
- data/lib/dry/config/base.rb +39 -22
- data/lib/dry/config/version.rb +1 -1
- data/spec/dry/config/base_spec.rb +87 -30
- data/spec/dry/config/white-label/acme.com.yml +12 -0
- data/spec/dry/config/white-label/base.yml +12 -0
- data/spec/dry/config/white-label/scheduling.acme.com.yml +10 -0
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 412477071e60ae65b5f06549108ee7864926964c
|
4
|
+
data.tar.gz: c7f28f2f85e928ff693b6526a315916e9db6e200
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 46da6bd6986668591d96de8b464d38b163d0f1182d46e99897e71c957773e3516ee269cb253197c4c8e785eb130f59c34321f7f16c37bbf20c1967c67564eb75
|
7
|
+
data.tar.gz: b29babccda05bd28332141231c0d144b7210155c9e7633fc4e50363ec631b07ab038bc9d39fc2a86fd46705df790899e24f2a5ba499fe97cefcdf0fc8f44edfa
|
data/README.md
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
# Dry::Config
|
2
2
|
|
3
|
-
Simple base class for DRY
|
3
|
+
Simple base class for DRY configurations configurations that can be loaded from multiple overriding yml files.
|
4
|
+
|
5
|
+
Sample uses include environment based e.g. `development, test, production` and multi-domain white label configurations.
|
4
6
|
|
5
|
-
A programmatic seed configuration may be specified, as well as the ability to load
|
6
|
-
(think multi-environment and a white label multi-domain configuration).
|
7
|
+
A programmatic seed configuration may be specified, as well as the ability to load one to many overriding configuration files.
|
7
8
|
|
8
9
|
The [elastic-beanstalk gem](https://github.com/alienfast/elastic-beanstalk) is a real world example that utilized `Dry::Config::Base`.
|
9
10
|
|
@@ -109,6 +110,16 @@ Note this sample uses the `Singleton` pattern, which is useful but not required.
|
|
109
110
|
- 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.
|
110
111
|
- An optional `#seed_default_configuration` allows you to provide a configuration base
|
111
112
|
- `#clear` will restore to the seed configuration, allowing you to `#load!` new settings.
|
113
|
+
|
114
|
+
## White Label sample
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
class WhiteLabelConfig < Dry::Config::Base
|
118
|
+
end
|
119
|
+
|
120
|
+
config = WhiteLabelConfig.new
|
121
|
+
config.load!(nil, 'base.yml', 'acme.com.yml', 'scheduling.acme.com.yml')
|
122
|
+
```
|
112
123
|
|
113
124
|
## Contributing
|
114
125
|
|
data/lib/dry/config/base.rb
CHANGED
@@ -31,49 +31,62 @@ module Dry
|
|
31
31
|
# a name of the file to read as it's argument. We can also pass in some
|
32
32
|
# options, but at the moment it's being used to allow per-environment
|
33
33
|
# overrides in Rails
|
34
|
-
def load!(environment,
|
34
|
+
def load!(environment, *filenames)
|
35
35
|
|
36
36
|
# raise 'Unspecified environment' if environment.nil?
|
37
|
-
raise 'Unspecified filename' if
|
37
|
+
raise 'Unspecified filename' if filenames.nil?
|
38
|
+
|
39
|
+
# ensure symbol
|
40
|
+
if environment
|
41
|
+
environment = environment.to_sym unless environment.is_a? Symbol
|
42
|
+
end
|
38
43
|
|
39
44
|
# save these in case we #reload
|
40
45
|
@environment = environment
|
41
|
-
@
|
46
|
+
@filenames = filenames
|
42
47
|
|
43
|
-
|
44
|
-
|
45
|
-
|
48
|
+
filenames.each do |filename|
|
49
|
+
# merge all top level settings with the defaults set in the #init
|
50
|
+
deep_merge!(@configuration, resolve_config(environment, filename))
|
51
|
+
end
|
46
52
|
|
47
53
|
# add the environment to the top level settings
|
48
54
|
@configuration[:environment] = (environment.nil? ? nil : environment.to_s)
|
55
|
+
end
|
49
56
|
|
50
|
-
|
51
|
-
# TODO: otherwise this will kill prior wanted settings when merging multiple files.
|
57
|
+
def resolve_config(environment, filename)
|
52
58
|
|
53
|
-
|
54
|
-
if environment && @configuration[environment.to_sym]
|
59
|
+
config = load_yaml_file(filename)
|
55
60
|
|
56
|
-
|
57
|
-
# based settings from the initial set so that they can be overlaid.
|
58
|
-
@potential_environments.each do |env|
|
59
|
-
@configuration.delete(env)
|
60
|
-
end
|
61
|
+
should_overlay_environment = environment && config[environment]
|
61
62
|
|
63
|
+
# Prune all known environments so that we end up with the top-level configuration.
|
64
|
+
@potential_environments.each do |env|
|
65
|
+
config.delete(env)
|
66
|
+
end
|
67
|
+
|
68
|
+
# overlay the specific environment if provided
|
69
|
+
if should_overlay_environment
|
62
70
|
# re-read the file
|
63
|
-
environment_settings =
|
71
|
+
environment_settings = load_yaml_file(filename)
|
64
72
|
|
65
73
|
# snag the requested environment
|
66
74
|
environment_settings = environment_settings[environment.to_sym]
|
67
75
|
|
68
|
-
# finally overlay what was provided
|
69
|
-
|
70
|
-
deep_merge!(@configuration, environment_settings)
|
76
|
+
# finally overlay what was provided the settings from the specific environment
|
77
|
+
deep_merge!(config, environment_settings)
|
71
78
|
end
|
79
|
+
|
80
|
+
config
|
81
|
+
end
|
82
|
+
|
83
|
+
def load_yaml_file(filename)
|
84
|
+
YAML::load_file(filename).deep_symbolize
|
72
85
|
end
|
73
86
|
|
74
87
|
def reload!
|
75
88
|
clear
|
76
|
-
load! @environment, @
|
89
|
+
load! @environment, @filenames
|
77
90
|
end
|
78
91
|
|
79
92
|
def clear
|
@@ -88,7 +101,11 @@ module Dry
|
|
88
101
|
|
89
102
|
private
|
90
103
|
|
91
|
-
def deep_merge!(target,
|
104
|
+
def deep_merge!(target, overrides)
|
105
|
+
|
106
|
+
raise 'target cannot be nil' if target.nil?
|
107
|
+
raise 'overrides cannot be nil' if overrides.nil?
|
108
|
+
|
92
109
|
merger = proc { |key, v1, v2|
|
93
110
|
if (Hash === v1 && Hash === v2)
|
94
111
|
v1.merge(v2, &merger)
|
@@ -98,7 +115,7 @@ module Dry
|
|
98
115
|
v2
|
99
116
|
end
|
100
117
|
}
|
101
|
-
target.merge!
|
118
|
+
target.merge! overrides, &merger
|
102
119
|
end
|
103
120
|
end
|
104
121
|
end
|
data/lib/dry/config/version.rb
CHANGED
@@ -34,50 +34,66 @@ describe Dry::Config::Base do
|
|
34
34
|
acmeConfig.clear
|
35
35
|
assert_common_seed_settings
|
36
36
|
expect(acmeConfig.options.length).to eql 0
|
37
|
+
|
38
|
+
# ensure no unnecessary environments make it into the resolved configuration
|
39
|
+
expect(acmeConfig.development).to be_nil
|
40
|
+
expect(acmeConfig.production).to be_nil
|
37
41
|
end
|
38
42
|
|
39
|
-
|
40
|
-
acmeConfig.clear
|
41
|
-
acmeConfig.load!(nil, config_file_path)
|
43
|
+
context 'when loading a single configuration file' do
|
42
44
|
|
43
|
-
|
44
|
-
|
45
|
+
it 'should read file with nil environment' do
|
46
|
+
acmeConfig.clear
|
47
|
+
acmeConfig.load!(nil, config_file_path)
|
45
48
|
|
46
|
-
|
47
|
-
|
48
|
-
end
|
49
|
+
assert_common_seed_settings
|
50
|
+
assert_common_top_level_settings
|
49
51
|
|
50
|
-
|
51
|
-
|
52
|
-
acmeConfig.load!(:development, config_file_path)
|
52
|
+
assert_option :'aws:elasticbeanstalk:application:environment', :RAILS_ENV, 'foobar'
|
53
|
+
assert_option :'aws:autoscaling:launchconfiguration', :InstanceType, 'foo'
|
53
54
|
|
54
|
-
|
55
|
-
|
55
|
+
# ensure no unnecessary environments make it into the resolved configuration
|
56
|
+
expect(acmeConfig.development).to be_nil
|
57
|
+
expect(acmeConfig.production).to be_nil
|
58
|
+
end
|
56
59
|
|
57
|
-
|
58
|
-
|
60
|
+
it 'should override with development environment' do
|
61
|
+
acmeConfig.clear
|
62
|
+
acmeConfig.load!(:development, config_file_path)
|
59
63
|
|
60
|
-
|
61
|
-
|
62
|
-
end
|
64
|
+
# assert_common_seed_settings
|
65
|
+
assert_common_top_level_settings
|
63
66
|
|
64
|
-
|
65
|
-
|
66
|
-
acmeConfig.load!(:production, config_file_path)
|
67
|
+
assert_option :'aws:elasticbeanstalk:application:environment', :RAILS_ENV, 'development'
|
68
|
+
assert_option :'aws:autoscaling:launchconfiguration', :InstanceType, 't1.micro'
|
67
69
|
|
68
|
-
|
69
|
-
|
70
|
+
expect(acmeConfig.package[:verbose]).to eql true
|
71
|
+
expect(acmeConfig.strategy).to eql 'inplace-update'
|
70
72
|
|
71
|
-
|
72
|
-
|
73
|
+
# ensure no unnecessary environments make it into the resolved configuration
|
74
|
+
expect(acmeConfig.production).to be_nil
|
75
|
+
end
|
73
76
|
|
74
|
-
|
77
|
+
it 'should override with production environment' do
|
78
|
+
acmeConfig.clear
|
79
|
+
acmeConfig.load!(:production, config_file_path)
|
75
80
|
|
76
|
-
|
77
|
-
|
78
|
-
expect(acmeConfig.package[:verbose]).to eql false
|
79
|
-
end
|
81
|
+
# assert_common_seed_settings
|
82
|
+
assert_common_top_level_settings
|
80
83
|
|
84
|
+
assert_option :'aws:elasticbeanstalk:application:environment', :RAILS_ENV, 'production'
|
85
|
+
assert_option :'aws:autoscaling:launchconfiguration', :InstanceType, 't1.large'
|
86
|
+
|
87
|
+
expect(acmeConfig.package[:verbose]).to eql false
|
88
|
+
|
89
|
+
# seed values
|
90
|
+
expect(acmeConfig.strategy).to eql :blue_green
|
91
|
+
expect(acmeConfig.package[:verbose]).to eql false
|
92
|
+
|
93
|
+
# ensure no unnecessary environments make it into the resolved configuration
|
94
|
+
expect(acmeConfig.development).to be_nil
|
95
|
+
end
|
96
|
+
end
|
81
97
|
private
|
82
98
|
|
83
99
|
def assert_option(section, name, value)
|
@@ -100,4 +116,45 @@ describe Dry::Config::Base do
|
|
100
116
|
def config_file_path
|
101
117
|
File.expand_path('../acme.yml', __FILE__)
|
102
118
|
end
|
119
|
+
|
120
|
+
context 'white label' do
|
121
|
+
class WhiteLabelConfig < Dry::Config::Base
|
122
|
+
end
|
123
|
+
|
124
|
+
subject(:white_label_config) { WhiteLabelConfig.new }
|
125
|
+
|
126
|
+
it 'should read the base white_label_config ' do
|
127
|
+
white_label_config.clear
|
128
|
+
white_label_config.load!(nil, base_file_path)
|
129
|
+
|
130
|
+
expect(white_label_config.domain).to eql '<undefined>'
|
131
|
+
expect(white_label_config.title).to eql '<undefined2>'
|
132
|
+
expect(white_label_config.layouts[:app]).to eql '<undefined3>'
|
133
|
+
expect(white_label_config.layouts[:public]).to eql '<undefined4>'
|
134
|
+
expect(white_label_config.features[:scheduling]).to eql true
|
135
|
+
end
|
136
|
+
|
137
|
+
it 'should read the scheduling.acme.com white_label_config ' do
|
138
|
+
white_label_config.clear
|
139
|
+
white_label_config.load!(nil, base_file_path, acme_file_path, scheduling_acme_file_path)
|
140
|
+
|
141
|
+
expect(white_label_config.domain).to eql 'scheduling.acme.com'
|
142
|
+
expect(white_label_config.title).to eql 'Acme Industries Limited, Inc.'
|
143
|
+
expect(white_label_config.layouts[:app]).to eql 'layouts/scheduling_app.haml'
|
144
|
+
expect(white_label_config.layouts[:public]).to eql 'layouts/public.haml'
|
145
|
+
expect(white_label_config.features[:scheduling]).to eql 8
|
146
|
+
end
|
147
|
+
|
148
|
+
def base_file_path
|
149
|
+
File.expand_path('../white-label/base.yml', __FILE__)
|
150
|
+
end
|
151
|
+
|
152
|
+
def acme_file_path
|
153
|
+
File.expand_path('../white-label/acme.com.yml', __FILE__)
|
154
|
+
end
|
155
|
+
|
156
|
+
def scheduling_acme_file_path
|
157
|
+
File.expand_path('../white-label/scheduling.acme.com.yml', __FILE__)
|
158
|
+
end
|
159
|
+
end
|
103
160
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dry-config
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin Ross
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-09-
|
11
|
+
date: 2014-09-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -74,6 +74,9 @@ files:
|
|
74
74
|
- spec/dry/config/acme.yml
|
75
75
|
- spec/dry/config/base_spec.rb
|
76
76
|
- spec/dry/config/deep_symbolizable_spec.rb
|
77
|
+
- spec/dry/config/white-label/acme.com.yml
|
78
|
+
- spec/dry/config/white-label/base.yml
|
79
|
+
- spec/dry/config/white-label/scheduling.acme.com.yml
|
77
80
|
- spec/spec_helper.rb
|
78
81
|
homepage: https://github.com/alienfast/dry-config
|
79
82
|
licenses:
|
@@ -103,4 +106,7 @@ test_files:
|
|
103
106
|
- spec/dry/config/acme.yml
|
104
107
|
- spec/dry/config/base_spec.rb
|
105
108
|
- spec/dry/config/deep_symbolizable_spec.rb
|
109
|
+
- spec/dry/config/white-label/acme.com.yml
|
110
|
+
- spec/dry/config/white-label/base.yml
|
111
|
+
- spec/dry/config/white-label/scheduling.acme.com.yml
|
106
112
|
- spec/spec_helper.rb
|