Skip to content

Commit 1f7bc91

Browse files
committed
Allow ENV prefix to include separator
1 parent d3ebfe2 commit 1f7bc91

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

lib/config/options.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ def reload_env!
3939
ENV.each do |variable, value|
4040
keys = variable.to_s.split(Config.env_separator)
4141

42-
next if keys.shift != (Config.env_prefix || Config.const_name)
42+
prefix = (Config.env_prefix || Config.const_name).to_s.split(Config.env_separator)
43+
44+
next if keys.shift(prefix.size) != prefix
4345

4446
keys.map! { |key|
4547
case Config.env_converter

spec/config_env_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,47 @@
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 variables from the new prefix' do
136+
ENV['MY_CONFIG_KEY'] = 'value'
137+
138+
expect(config.key).to eq('value')
139+
end
140+
141+
it 'should not load variables from the default prefix' do
142+
ENV['Settings_key'] = 'value'
143+
144+
expect(config.key).to eq(nil)
145+
end
146+
147+
it 'should skip ENV variable when partial prefix match' do
148+
ENV['MY_CONFIGS_KEY'] = 'value'
149+
150+
expect(config.key).to eq(nil)
151+
end
152+
153+
it 'should load variables and correctly recognize the new separator' do
154+
ENV['MY_CONFIG_KEY'] = 'value'
155+
ENV['MY_CONFIG_KEY.WITH.DOT'] = 'value'
156+
ENV['MY_CONFIG_WORLD_COUNTRIES_EUROPE'] = '0'
157+
158+
expect(config.key).to eq('value')
159+
expect(config['key.with.dot']).to eq('value')
160+
expect(config.world.countries.europe).to eq(0)
161+
end
162+
163+
it 'should ignore variables wit default separator' do
164+
ENV['MY_CONFIG.KEY'] = 'value'
165+
166+
expect(config.key).to eq(nil)
167+
end
168+
end
169+
129170
context 'and variable names conversion is enabled' do
130171
it 'should downcase variable names when :downcase conversion enabled' do
131172
ENV['Settings.NEW_VAR'] = 'value'

0 commit comments

Comments
 (0)