Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Allow a custom environment variable prefix to include the environment variable separator #177

Merged
merged 5 commits into from
Aug 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/config/options.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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