Skip to content

Commit

Permalink
Merge pull request #177 from rdodson41/allow-env-prefix-with-separator
Browse files Browse the repository at this point in the history
Allow a custom environment variable prefix to include the environment variable separator
  • Loading branch information
pkuczynski committed Aug 7, 2017
2 parents d3ebfe2 + 8bb3503 commit 3131b54
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/config/options.rb
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions spec/config_env_spec.rb
Expand Up @@ -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'
Expand Down

0 comments on commit 3131b54

Please sign in to comment.