Skip to content

Commit

Permalink
Deprecate constructor as a block
Browse files Browse the repository at this point in the history
So far, we have been accepting both a block and the `constructor:`
keyword argument for providing a constructor. The former will not be
available in 1.0, so we're deprecating it now.

We also prepare the test suite for the change and make naming consistent
using "constructor" where we were using "pre-processor."
  • Loading branch information
waiting-for-dev committed May 11, 2021
1 parent 5e1316a commit c8d12c2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docsite/source/index.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class App
end
# Defaults to nil if no default value is given
setting :adapter
# Pre-process values
# Construct values
setting(:path, 'test') { |value| Pathname(value) }
# Passing the reader option as true will create attr_reader method for the class
setting :pool, 5, reader: true
Expand Down
2 changes: 2 additions & 0 deletions lib/dry/configurable/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def setting(name, *args, &block)
if block.arity.zero?
ast << [:nested, [node, DSL.new(&block).ast]]
else
# TODO: [Before 1.0] Remove constructor-as-block
warn '[dry-configurable] Passing a constructor as a bloc is deprecated, please use the `constructor:` kwarg instead', uplevel: 1
ast << [:constructor, [node, block]]
end
else
Expand Down
22 changes: 10 additions & 12 deletions spec/integration/dry/configurable/setting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@
end
end

context 'with a value pre-processor' do
it 'pre-processes the value with nil default' do
klass.setting(:path, nil) { |value| "test:#{value || "fallback"}" }
context 'with a value constructor' do
it 'constructs the value with nil default' do
klass.setting(:path, nil, constructor: ->(value) { "test:#{value || "fallback"}" })

expect(object.config.path).to eql("test:fallback")

Expand All @@ -110,22 +110,20 @@
expect(object.config.path).to eql('test:foo')
end

it 'pre-processes the value with undefined default' do
klass.setting(:path) { |value| "test:#{value || "fallback"}" }
it 'constructs the value with undefined default' do
klass.setting(:path, constructor: ->(value) { "test:#{value || "fallback"}" })

expect(object.config.path).to eql('test:fallback')
end

it 'pre-processes the value with non-nil default' do
klass.setting(:path, 'test') { |value| Pathname(value) }
it 'constructs the value with non-nil default' do
klass.setting(:path, 'test', constructor: ->(value) { Pathname(value) })

expect(object.config.path).to eql(Pathname('test'))
end

it 'raises pre-processor errors immediately' do
klass.setting :failable do |value|
value.to_sym unless value.nil?
end
it 'raises constructor errors immediately' do
klass.setting(:failable, constructor: ->(value) { value.to_sym unless value.nil? })

expect {
object.config.failable = 12
Expand Down Expand Up @@ -316,7 +314,7 @@
end

it 'creates distinct setting values across instances' do
klass.setting(:path, 'test') { |m| Pathname(m) }
klass.setting(:path, 'test', constructor: ->(m) { Pathname(m) })

new_object = klass.new

Expand Down
8 changes: 8 additions & 0 deletions spec/unit/dry/configurable/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@
end

it 'compiles a setting with a constructor' do
setting = dsl.setting(:dsn, 'sqlite', constructor: ->(value) { "jdbc:#{value}" })

expect(setting.name).to be(:dsn)
expect(setting.value).to eql('jdbc:sqlite')
end

it 'supports but deprecates giving a constructor as a block' do

setting = dsl.setting(:dsn, 'sqlite') { |value| "jdbc:#{value}" }

expect(setting.name).to be(:dsn)
Expand Down

0 comments on commit c8d12c2

Please sign in to comment.