Skip to content

Commit 3131b54

Browse files
authored
Merge pull request #177 from rdodson41/allow-env-prefix-with-separator
Allow a custom environment variable prefix to include the environment variable separator
2 parents d3ebfe2 + 8bb3503 commit 3131b54

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

lib/config/options.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ def reload_env!
3737
hash = Hash.new
3838

3939
ENV.each do |variable, value|
40-
keys = variable.to_s.split(Config.env_separator)
40+
separator = Config.env_separator
41+
prefix = (Config.env_prefix || Config.const_name).to_s.split(separator)
4142

42-
next if keys.shift != (Config.env_prefix || Config.const_name)
43+
keys = variable.to_s.split(separator)
44+
45+
next if keys.shift(prefix.size) != prefix
4346

4447
keys.map! { |key|
4548
case Config.env_converter

spec/config_env_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,45 @@
126126
end
127127
end
128128

129+
context 'and custom ENV variables prefix includes custom ENV variables separator' do
130+
before :each do
131+
Config.env_prefix = 'MY_CONFIG'
132+
Config.env_separator = '_'
133+
end
134+
135+
it 'should load environment variables which begin with the custom prefix' do
136+
ENV['MY_CONFIG_KEY'] = 'value'
137+
138+
expect(config.key).to eq('value')
139+
end
140+
141+
it 'should not load environment variables which begin with the default prefix' do
142+
ENV['Settings_key'] = 'value'
143+
144+
expect(config.key).to eq(nil)
145+
end
146+
147+
it 'should not load environment variables which partially begin with the custom prefix' do
148+
ENV['MY_CONFIGS_KEY'] = 'value'
149+
150+
expect(config.key).to eq(nil)
151+
end
152+
153+
it 'should recognize the custom separator' do
154+
ENV['MY_CONFIG_KEY.WITH.DOT'] = 'value'
155+
ENV['MY_CONFIG_WORLD_COUNTRIES_EUROPE'] = '0'
156+
157+
expect(config['key.with.dot']).to eq('value')
158+
expect(config.world.countries.europe).to eq(0)
159+
end
160+
161+
it 'should not recognize the default separator' do
162+
ENV['MY_CONFIG.KEY'] = 'value'
163+
164+
expect(config.key).to eq(nil)
165+
end
166+
end
167+
129168
context 'and variable names conversion is enabled' do
130169
it 'should downcase variable names when :downcase conversion enabled' do
131170
ENV['Settings.NEW_VAR'] = 'value'

0 commit comments

Comments
 (0)