diff --git a/lib/dry/configurable/config.rb b/lib/dry/configurable/config.rb index 29f2c324..6f9d79b1 100644 --- a/lib/dry/configurable/config.rb +++ b/lib/dry/configurable/config.rb @@ -33,6 +33,7 @@ def initialize(settings) # # @return Config value def [](name) + name = name.to_sym raise ArgumentError, "+#{name}+ is not a setting name" unless _settings.key?(name) _settings[name].value diff --git a/lib/dry/configurable/dsl.rb b/lib/dry/configurable/dsl.rb index 5dfa2235..c12268c2 100644 --- a/lib/dry/configurable/dsl.rb +++ b/lib/dry/configurable/dsl.rb @@ -43,7 +43,7 @@ def setting(name, *args, &block) default, opts = args - node = [:setting, [name, default, opts == default ? EMPTY_HASH : opts]] + node = [:setting, [name.to_sym, default, opts == default ? EMPTY_HASH : opts]] if block if block.arity.zero? diff --git a/spec/integration/dry/configurable/config_spec.rb b/spec/integration/dry/configurable/config_spec.rb index 53ede179..29bbe2ba 100644 --- a/spec/integration/dry/configurable/config_spec.rb +++ b/spec/integration/dry/configurable/config_spec.rb @@ -113,6 +113,12 @@ end describe '#[]' do + it 'coerces name from string' do + klass.setting :db, :sqlite + + expect(klass.config['db']).to eql(:sqlite) + end + it 'raises ArgumentError when name is not valid' do expect { klass.config[:hello] }.to raise_error(ArgumentError, /hello/) end diff --git a/spec/integration/dry/configurable/setting_spec.rb b/spec/integration/dry/configurable/setting_spec.rb index 99e91d0e..88e151d6 100644 --- a/spec/integration/dry/configurable/setting_spec.rb +++ b/spec/integration/dry/configurable/setting_spec.rb @@ -38,6 +38,12 @@ ) end + it 'stores setting name as symbol' do + klass.setting 'db', 'sqlite' + + expect(object.config.values.keys).to include(:db) + end + context 'with a default value' do context 'string' do before do