Skip to content

Commit

Permalink
Merge pull request #124 from dry-rb/add-flags-to-suppress-deprecation…
Browse files Browse the repository at this point in the history
…-warnings

Add flags to control deprecation notices
  • Loading branch information
timriley authored Aug 30, 2021
2 parents 2ed98e1 + b84b85c commit 4536dec
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 20 deletions.
47 changes: 28 additions & 19 deletions lib/dry/configurable/dsl.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "dry/configurable/constants"
require "dry/configurable/flags"
require "dry/configurable/setting"
require "dry/configurable/settings"
require "dry/configurable/compiler"
Expand Down Expand Up @@ -32,18 +33,21 @@ def initialize(&block)
# @see ClassMethods.setting
# @api private
# @return Setting
def setting(name, default = Undefined, **options, &block) # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity
def setting(name, default = Undefined, **options, &block) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
unless VALID_NAME.match?(name.to_s)
raise ArgumentError, "#{name} is not a valid setting name"
end

if default != Undefined
Dry::Core::Deprecations.announce(
"default value as positional argument to settings",
"Provide a `default:` keyword argument instead",
tag: "dry-configurable",
uplevel: 2
)
if Dry::Configurable.warn_on_setting_positional_default
Dry::Core::Deprecations.announce(
"default value as positional argument to settings",
"Provide a `default:` keyword argument instead",
tag: "dry-configurable",
uplevel: 2
)
end

options = options.merge(default: default)
end

Expand Down Expand Up @@ -101,23 +105,28 @@ def setting(name, default = Undefined, **options, &block) # rubocop:disable Metr
# Additionally, the deprecation messages will make the new behavior obvious, and
# encourage the users to upgrade their setting definitions.

Dry::Core::Deprecations.announce(
"default value as positional argument to settings",
"Provide a `default:` keyword argument instead",
tag: "dry-configurable",
uplevel: 2
)
if Dry::Configurable.warn_on_setting_positional_default
Dry::Core::Deprecations.announce(
"default value as positional argument to settings",
"Provide a `default:` keyword argument instead",
tag: "dry-configurable",
uplevel: 2
)
end

options = {default: invalid_opts}
end

if block && !block.arity.zero?
Dry::Core::Deprecations.announce(
"passing a constructor as a block",
"Provide a `constructor:` keyword argument instead",
tag: "dry-configurable",
uplevel: 2
)
if Dry::Configurable.warn_on_setting_constructor_block
Dry::Core::Deprecations.announce(
"passing a constructor as a block",
"Provide a `constructor:` keyword argument instead",
tag: "dry-configurable",
uplevel: 2
)
end

options = options.merge(constructor: block)
block = nil
end
Expand Down
19 changes: 19 additions & 0 deletions lib/dry/configurable/flags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require "dry/core/class_attributes"

module Dry
module Configurable
extend Core::ClassAttributes

# Set to false to suppress deprecation warning when a setting default is provided as a
# positional argument
defines :warn_on_setting_positional_default
warn_on_setting_positional_default true

# Set to false to suppress deprecation warning when a setting constructor is provided
# as a block
defines :warn_on_setting_constructor_block
warn_on_setting_constructor_block true
end
end
33 changes: 32 additions & 1 deletion spec/unit/dry/configurable/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@
expect(logger.string).to match(/default value as positional argument to settings is deprecated/)
end

it "compiles when giving a default as positional argument, and suppresses the warning when flagged off" do
Dry::Configurable.warn_on_setting_positional_default false

logger = StringIO.new
Dry::Core::Deprecations.set_logger!(logger)
setting = dsl.setting :user, "root"

expect(setting.name).to be(:user)
expect(setting.value).to eql("root")
logger.rewind
expect(logger.string).to be_empty

Dry::Configurable.warn_on_setting_positional_default true
end

it "compiles but deprecates giving a defalt hash value as a positional argument (without any keyword args)" do
# This test is necessary for behavior specific to Ruby 2.6 and 2.7

Expand All @@ -58,7 +73,7 @@
end
end

it "compiles but deprecates giving a defalt hash value as a positional argument (with keyword args) " do
it "compiles but deprecates giving a defalt hash value as a positional argument (with keyword args)" do
# This test is necessary for behavior specific to Ruby 2.6 and 2.7

logger = StringIO.new
Expand Down Expand Up @@ -119,6 +134,22 @@
expect(logger.string).to match(/constructor as a block is deprecated/)
end

it "supports but deprecates giving a constructor as a block, and suppresses the warning when flagged off" do
Dry::Configurable.warn_on_setting_constructor_block false

logger = StringIO.new
Dry::Core::Deprecations.set_logger!(logger)

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

expect(setting.name).to be(:dsn)
expect(setting.value).to eql("jdbc:sqlite")
logger.rewind
expect(logger.string).to be_empty

Dry::Configurable.warn_on_setting_constructor_block true
end

it "compiles a nested list of settings" do
setting =
dsl.setting(:db) do
Expand Down

0 comments on commit 4536dec

Please sign in to comment.