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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d5cebd6a6d0383fcb039c2dfc66e262992d71c72
4
- data.tar.gz: 4aa181325a5fb085cdfc7da1c0cdfa32dc7e8747
3
+ metadata.gz: 412477071e60ae65b5f06549108ee7864926964c
4
+ data.tar.gz: c7f28f2f85e928ff693b6526a315916e9db6e200
5
5
  SHA512:
6
- metadata.gz: e9a5d9294a5b1ac10eee9b1f4733cf6ccd1340fb3ad1f58d144973d169c3d864ecb7dc9e333285c783e0110900a460648b78e320fabed735192cb9013ef5fa63
7
- data.tar.gz: a5519d08c52c8fb5d6871023286542b26615058542c8a8ccb9202b766a97896c269ab80aa299183a5168de2ea63e27f19ee0d1e0f183d4a2c8fbf4128ef8ecea
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 environment based configurations that can be loaded from multiple overriding yml files.
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 multiple overriding configuration files
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
 
@@ -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, filename)
34
+ def load!(environment, *filenames)
35
35
 
36
36
  # raise 'Unspecified environment' if environment.nil?
37
- raise 'Unspecified filename' if filename.nil?
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
- @filename = filename
46
+ @filenames = filenames
42
47
 
43
- # merge all top level settings with the defaults set in the #init
44
-
45
- deep_merge!(@configuration, YAML::load_file(filename).deep_symbolize)
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
- # TODO: the target file should be overlaid in an environment specific way in isolation, prior to merging with @configuration
51
- # TODO: otherwise this will kill prior wanted settings when merging multiple files.
57
+ def resolve_config(environment, filename)
52
58
 
53
- # overlay the specific environment if provided
54
- if environment && @configuration[environment.to_sym]
59
+ config = load_yaml_file(filename)
55
60
 
56
- # this is environment specific, so prune any environment
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 = YAML::load_file(filename).deep_symbolize
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
- #@configuration.deep_merge!(environment_settings)
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, @filename
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, data)
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! data, &merger
118
+ target.merge! overrides, &merger
102
119
  end
103
120
  end
104
121
  end
@@ -1,5 +1,5 @@
1
1
  module Dry
2
2
  module Config
3
- VERSION = '1.0.0'
3
+ VERSION = '1.1.0'
4
4
  end
5
5
  end
@@ -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
- it 'should read file with nil environment' do
40
- acmeConfig.clear
41
- acmeConfig.load!(nil, config_file_path)
43
+ context 'when loading a single configuration file' do
42
44
 
43
- assert_common_seed_settings
44
- assert_common_top_level_settings
45
+ it 'should read file with nil environment' do
46
+ acmeConfig.clear
47
+ acmeConfig.load!(nil, config_file_path)
45
48
 
46
- assert_option :'aws:elasticbeanstalk:application:environment', :RAILS_ENV, 'foobar'
47
- assert_option :'aws:autoscaling:launchconfiguration', :InstanceType, 'foo'
48
- end
49
+ assert_common_seed_settings
50
+ assert_common_top_level_settings
49
51
 
50
- it 'should read file and override with development environment' do
51
- acmeConfig.clear
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
- # assert_common_seed_settings
55
- assert_common_top_level_settings
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
- assert_option :'aws:elasticbeanstalk:application:environment', :RAILS_ENV, 'development'
58
- assert_option :'aws:autoscaling:launchconfiguration', :InstanceType, 't1.micro'
60
+ it 'should override with development environment' do
61
+ acmeConfig.clear
62
+ acmeConfig.load!(:development, config_file_path)
59
63
 
60
- expect(acmeConfig.package[:verbose]).to eql true
61
- expect(acmeConfig.strategy).to eql 'inplace-update'
62
- end
64
+ # assert_common_seed_settings
65
+ assert_common_top_level_settings
63
66
 
64
- it 'should read file and override with production environment' do
65
- acmeConfig.clear
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
- # assert_common_seed_settings
69
- assert_common_top_level_settings
70
+ expect(acmeConfig.package[:verbose]).to eql true
71
+ expect(acmeConfig.strategy).to eql 'inplace-update'
70
72
 
71
- assert_option :'aws:elasticbeanstalk:application:environment', :RAILS_ENV, 'production'
72
- assert_option :'aws:autoscaling:launchconfiguration', :InstanceType, 't1.large'
73
+ # ensure no unnecessary environments make it into the resolved configuration
74
+ expect(acmeConfig.production).to be_nil
75
+ end
73
76
 
74
- expect(acmeConfig.package[:verbose]).to eql false
77
+ it 'should override with production environment' do
78
+ acmeConfig.clear
79
+ acmeConfig.load!(:production, config_file_path)
75
80
 
76
- # seed values
77
- expect(acmeConfig.strategy).to eql :blue_green
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
@@ -0,0 +1,12 @@
1
+ # sample config demonstrating white label overrides for acme.com
2
+ ---
3
+ domain: acme.com
4
+ title: Acme Industries Limited, Inc.
5
+ layouts:
6
+ app: layouts/app.haml
7
+ public: layouts/public.haml
8
+ #---
9
+
10
+
11
+ features:
12
+ scheduling: false
@@ -0,0 +1,12 @@
1
+ # sample config demonstrating white label overrides
2
+ ---
3
+ domain: <undefined>
4
+ title: <undefined2>
5
+ layouts:
6
+ app: <undefined3>
7
+ public: <undefined4>
8
+ #---
9
+
10
+
11
+ features:
12
+ scheduling: true
@@ -0,0 +1,10 @@
1
+ # sample config demonstrating white label overrides for acme.com
2
+ ---
3
+ domain: scheduling.acme.com
4
+ layouts:
5
+ app: layouts/scheduling_app.haml
6
+ #---
7
+
8
+
9
+ features:
10
+ scheduling: 8
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.0.0
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-10 00:00:00.000000000 Z
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