diff --git a/lib/config/options.rb b/lib/config/options.rb index 92e8c2b0..8e58f56d 100644 --- a/lib/config/options.rb +++ b/lib/config/options.rb @@ -37,9 +37,12 @@ def reload_env! hash = Hash.new ENV.each do |variable, value| - keys = variable.to_s.split(Config.env_separator) + separator = Config.env_separator + prefix = (Config.env_prefix || Config.const_name).to_s.split(separator) - next if keys.shift != (Config.env_prefix || Config.const_name) + keys = variable.to_s.split(separator) + + next if keys.shift(prefix.size) != prefix keys.map! { |key| case Config.env_converter diff --git a/spec/config_env_spec.rb b/spec/config_env_spec.rb index 42f30392..639c6c76 100644 --- a/spec/config_env_spec.rb +++ b/spec/config_env_spec.rb @@ -126,6 +126,45 @@ end end + context 'and custom ENV variables prefix includes custom ENV variables separator' do + before :each do + Config.env_prefix = 'MY_CONFIG' + Config.env_separator = '_' + end + + it 'should load environment variables which begin with the custom prefix' do + ENV['MY_CONFIG_KEY'] = 'value' + + expect(config.key).to eq('value') + end + + it 'should not load environment variables which begin with the default prefix' do + ENV['Settings_key'] = 'value' + + expect(config.key).to eq(nil) + end + + it 'should not load environment variables which partially begin with the custom prefix' do + ENV['MY_CONFIGS_KEY'] = 'value' + + expect(config.key).to eq(nil) + end + + it 'should recognize the custom separator' do + ENV['MY_CONFIG_KEY.WITH.DOT'] = 'value' + ENV['MY_CONFIG_WORLD_COUNTRIES_EUROPE'] = '0' + + expect(config['key.with.dot']).to eq('value') + expect(config.world.countries.europe).to eq(0) + end + + it 'should not recognize the default separator' do + ENV['MY_CONFIG.KEY'] = 'value' + + expect(config.key).to eq(nil) + end + end + context 'and variable names conversion is enabled' do it 'should downcase variable names when :downcase conversion enabled' do ENV['Settings.NEW_VAR'] = 'value'