Skip to content

Commit

Permalink
Merge pull request #83 from dry-rb/setting_str_sym
Browse files Browse the repository at this point in the history
Store settings as symbol and fetch them from symbol or string

[changelog]

version: 0.11.3
fixed: "Retrieving settings by a string name works again (issue #82) (@waiting-for-dev)"
  • Loading branch information
solnic authored Feb 22, 2020
2 parents edde596 + adac57b commit 1ad8821
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/dry/configurable/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/dry/configurable/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
6 changes: 6 additions & 0 deletions spec/integration/dry/configurable/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions spec/integration/dry/configurable/setting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1ad8821

Please sign in to comment.