Skip to content

Commit

Permalink
🔧 Add default configs for named versions
Browse files Browse the repository at this point in the history
Config name can be :default, :current, :next, or :future.
  • Loading branch information
nevans committed Jun 22, 2024
1 parent 1741c0a commit be8d5cd
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/net/imap/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ class IMAP
# client.config.sasl_ir # => true
# client.config.responses_without_block # => :warn
#
# client = Net::IMAP.new(hostname, config: :future)
# client.config.sasl_ir # => true
# client.config.responses_without_block # => :raise
#
# The versioned default configs inherit certain specific config options from
# Config.global, for example #debug:
#
Expand All @@ -83,6 +87,26 @@ class IMAP
# Net::IMAP.debug = true
# client.config.debug? # => true
#
# === Named defaults
# In addition to +x.y+ version numbers, the following aliases are supported:
#
# [+:default+]
# An alias for +:current+.
#
# >>>
# *NOTE*: This is _not_ the same as Config.default. It inherits some
# attributes from Config.global, for example: #debug.
# [+:current+]
# An alias for the current +x.y+ version's defaults.
# [+:next+]
# The _planned_ config for the next +x.y+ version.
# [+:future+]
# The _planned_ eventual config for some future +x.y+ version.
#
# For example, to raise exceptions for all current deprecations:
# client = Net::IMAP.new(hostname, config: :future)
# client.responses # raises an ArgumentError
#
# == Thread Safety
#
# *NOTE:* Updates to config objects are not synchronized for thread-safety.
Expand Down Expand Up @@ -128,6 +152,8 @@ def self.[](config)
case config
when Numeric
raise RangeError, "unknown config version: %p" % [config]
when Symbol
raise KeyError, "unknown config name: %p" % [config]
else
raise TypeError, "no implicit conversion of %s to %s" % [
config.class, Config
Expand Down Expand Up @@ -275,6 +301,14 @@ def to_h; data.members.to_h { [_1, send(_1)] } end
responses_without_block: :warn,
).freeze

version_defaults[:default] = Config[0.4]
version_defaults[:current] = Config[0.4]
version_defaults[:next] = Config[0.5]

version_defaults[:future] = Config[0.5].dup.update(
responses_without_block: :raise,
).freeze

version_defaults.freeze
end
end
Expand Down
12 changes: 12 additions & 0 deletions test/net/imap/test_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ class ConfigTest < Test::Unit::TestCase
assert_raise(RangeError) do Config[1] end
end

test ".[] key errors" do
assert_raise(KeyError) do Config[:nonexistent] end
end

test ".[] with symbol names" do
assert_same Config[0.4], Config[:current]
assert_same Config[0.4], Config[:default]
assert_same Config[0.5], Config[:next]
assert_kind_of Config, Config[:future]
end

test ".[] with a hash" do
config = Config[{responses_without_block: :raise, sasl_ir: false}]
assert config.frozen?
Expand All @@ -179,6 +190,7 @@ class ConfigTest < Test::Unit::TestCase
assert_same Config.default, Config.new(Config.default).parent
assert_same Config.global, Config.new(Config.global).parent
assert_same Config[0.4], Config.new(0.4).parent
assert_same Config[0.5], Config.new(:next).parent
assert_equal true, Config.new({debug: true}, debug: false).parent.debug?
assert_equal true, Config.new({debug: true}, debug: false).parent.frozen?
end
Expand Down

0 comments on commit be8d5cd

Please sign in to comment.