Skip to content

Commit

Permalink
add Config.env_parse_arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Mlynek committed Mar 23, 2024
1 parent b21d8c9 commit 070b279
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module Config
env_separator: '.',
env_converter: :downcase,
env_parse_values: true,
env_parse_arrays: false,
fail_on_missing: false,
file_name: 'settings',
dir_name: 'settings',
Expand Down
7 changes: 5 additions & 2 deletions lib/config/sources/env_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ class EnvSource
attr_reader :separator
attr_reader :converter
attr_reader :parse_values
attr_reader :parse_arrays

def initialize(env,
prefix: Config.env_prefix || Config.const_name,
separator: Config.env_separator,
converter: Config.env_converter,
parse_values: Config.env_parse_values)
parse_values: Config.env_parse_values,
parse_arrays: Config.env_parse_arrays)
@env = env
@prefix = prefix.to_s.split(separator)
@separator = separator
@converter = converter
@parse_values = parse_values
@parse_arrays = parse_arrays
end

def load
Expand Down Expand Up @@ -52,7 +55,7 @@ def load
leaf[keys.last] = parse_values ? __value(value) : value
end

convert_hashes_to_arrays(hash)
parse_arrays ? convert_hashes_to_arrays(hash) : hash
end

private
Expand Down
31 changes: 31 additions & 0 deletions spec/config_env_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
Config.env_separator = '.'
Config.env_converter = :downcase
Config.env_parse_values = true
Config.env_parse_arrays = true
end

it 'should add new setting from ENV variable' do
Expand Down Expand Up @@ -96,6 +97,36 @@
end
end

context 'and parsing ENV variables arrays' do
context 'is enabled' do
before :each do
Config.env_parse_arrays = true
end

it 'should recognize ENV variables with subsequent numeric suffixes starting from 0 as array' do
ENV['Settings.SomeConfig.0'] = 'first'
ENV['Settings.SomeConfig.1'] = 'second'

expect(config.someconfig).to eq(['first', 'second'])
end
end

context 'is disabled' do
before :each do
Config.env_parse_arrays = false
end

it 'should not recognize ENV variables with subsequent numeric suffixes starting from 0 as array' do
ENV['Settings.SomeConfig.0'] = 'first'
ENV['Settings.SomeConfig.1'] = 'second'

expect(config.someconfig).to be_a Config::Options
expect(config.someconfig['0']).to eq('first')
expect(config.someconfig['1']).to eq('second')
end
end
end

context 'and custom ENV variables prefix is defined' do
before :each do
Config.env_prefix = 'MyConfig'
Expand Down
4 changes: 4 additions & 0 deletions spec/sources/env_source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ module Config::Sources
end

describe 'arrays' do
before(:each) do
Config.env_parse_arrays = true
end

let(:source) do
Config.env_converter = nil
EnvSource.new({
Expand Down

0 comments on commit 070b279

Please sign in to comment.