diff --git a/lib/net/imap/config.rb b/lib/net/imap/config.rb index 5cff7b87..bb8c0ce3 100644 --- a/lib/net/imap/config.rb +++ b/lib/net/imap/config.rb @@ -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: # @@ -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. @@ -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 @@ -271,6 +297,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 diff --git a/test/net/imap/test_config.rb b/test/net/imap/test_config.rb index 18659216..a49fc107 100644 --- a/test/net/imap/test_config.rb +++ b/test/net/imap/test_config.rb @@ -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? @@ -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