Skip to content

Commit be8d5cd

Browse files
committed
🔧 Add default configs for named versions
Config name can be :default, :current, :next, or :future.
1 parent 1741c0a commit be8d5cd

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

lib/net/imap/config.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ class IMAP
7373
# client.config.sasl_ir # => true
7474
# client.config.responses_without_block # => :warn
7575
#
76+
# client = Net::IMAP.new(hostname, config: :future)
77+
# client.config.sasl_ir # => true
78+
# client.config.responses_without_block # => :raise
79+
#
7680
# The versioned default configs inherit certain specific config options from
7781
# Config.global, for example #debug:
7882
#
@@ -83,6 +87,26 @@ class IMAP
8387
# Net::IMAP.debug = true
8488
# client.config.debug? # => true
8589
#
90+
# === Named defaults
91+
# In addition to +x.y+ version numbers, the following aliases are supported:
92+
#
93+
# [+:default+]
94+
# An alias for +:current+.
95+
#
96+
# >>>
97+
# *NOTE*: This is _not_ the same as Config.default. It inherits some
98+
# attributes from Config.global, for example: #debug.
99+
# [+:current+]
100+
# An alias for the current +x.y+ version's defaults.
101+
# [+:next+]
102+
# The _planned_ config for the next +x.y+ version.
103+
# [+:future+]
104+
# The _planned_ eventual config for some future +x.y+ version.
105+
#
106+
# For example, to raise exceptions for all current deprecations:
107+
# client = Net::IMAP.new(hostname, config: :future)
108+
# client.responses # raises an ArgumentError
109+
#
86110
# == Thread Safety
87111
#
88112
# *NOTE:* Updates to config objects are not synchronized for thread-safety.
@@ -128,6 +152,8 @@ def self.[](config)
128152
case config
129153
when Numeric
130154
raise RangeError, "unknown config version: %p" % [config]
155+
when Symbol
156+
raise KeyError, "unknown config name: %p" % [config]
131157
else
132158
raise TypeError, "no implicit conversion of %s to %s" % [
133159
config.class, Config
@@ -275,6 +301,14 @@ def to_h; data.members.to_h { [_1, send(_1)] } end
275301
responses_without_block: :warn,
276302
).freeze
277303

304+
version_defaults[:default] = Config[0.4]
305+
version_defaults[:current] = Config[0.4]
306+
version_defaults[:next] = Config[0.5]
307+
308+
version_defaults[:future] = Config[0.5].dup.update(
309+
responses_without_block: :raise,
310+
).freeze
311+
278312
version_defaults.freeze
279313
end
280314
end

test/net/imap/test_config.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,17 @@ class ConfigTest < Test::Unit::TestCase
164164
assert_raise(RangeError) do Config[1] end
165165
end
166166

167+
test ".[] key errors" do
168+
assert_raise(KeyError) do Config[:nonexistent] end
169+
end
170+
171+
test ".[] with symbol names" do
172+
assert_same Config[0.4], Config[:current]
173+
assert_same Config[0.4], Config[:default]
174+
assert_same Config[0.5], Config[:next]
175+
assert_kind_of Config, Config[:future]
176+
end
177+
167178
test ".[] with a hash" do
168179
config = Config[{responses_without_block: :raise, sasl_ir: false}]
169180
assert config.frozen?
@@ -179,6 +190,7 @@ class ConfigTest < Test::Unit::TestCase
179190
assert_same Config.default, Config.new(Config.default).parent
180191
assert_same Config.global, Config.new(Config.global).parent
181192
assert_same Config[0.4], Config.new(0.4).parent
193+
assert_same Config[0.5], Config.new(:next).parent
182194
assert_equal true, Config.new({debug: true}, debug: false).parent.debug?
183195
assert_equal true, Config.new({debug: true}, debug: false).parent.frozen?
184196
end

0 commit comments

Comments
 (0)