From d8ce1cb8abd3ef4e5cb3e0c5cd755ad1724b2b8c Mon Sep 17 00:00:00 2001 From: nick evans Date: Fri, 14 Jun 2024 18:27:38 -0400 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=94=A7=20Add=20Config#load=5Fdefaults?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is inspired by Rails::Application::Configuration#load_defaults in rails. `#load_defaults` resets the current config to behave like the versioned default configuration for that version. `#parent` will not be changed. Some config attributes default to inheriting from their `#parent` (which is usually Config.global) and are left unchanged, for example: `#debug`. --- lib/net/imap/config.rb | 23 +++++++++++++++++++++++ test/net/imap/test_config.rb | 23 +++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/lib/net/imap/config.rb b/lib/net/imap/config.rb index 0b0c028d..b5bb069a 100644 --- a/lib/net/imap/config.rb +++ b/lib/net/imap/config.rb @@ -87,6 +87,12 @@ class IMAP # Net::IMAP.debug = true # client.config.debug? # => true # + # Use #load_defaults to globally behave like a specific version: + # client = Net::IMAP.new(hostname) + # client.config.sasl_ir # => true + # Net::IMAP.config.load_defaults 0.3 + # client.config.sasl_ir # => false + # # === Named defaults # In addition to +x.y+ version numbers, the following aliases are supported: # @@ -270,6 +276,23 @@ def with(**attrs) block_given? ? yield(copy) : copy end + # :call-seq: load_defaults(version) -> self + # + # Resets the current config to behave like the versioned default + # configuration for +version+. #parent will not be changed. + # + # Some config attributes default to inheriting from their #parent (which + # is usually Config.global) and are left unchanged, for example: #debug. + # + # See Config@Versioned+defaults and Config@Named+defaults. + def load_defaults(version) + [Numeric, Symbol, String].any? { _1 === version } or + raise ArgumentError, "expected number or symbol, got %p" % [version] + config = Config[version] + defaults = config.to_h.reject {|k,v| DEFAULT_TO_INHERIT.include?(k) } + update(**defaults) + end + # :call-seq: to_h -> hash # # Returns all config attributes in a hash. diff --git a/test/net/imap/test_config.rb b/test/net/imap/test_config.rb index 8176f725..a8afb4fb 100644 --- a/test/net/imap/test_config.rb +++ b/test/net/imap/test_config.rb @@ -332,4 +332,27 @@ class ConfigTest < Test::Unit::TestCase assert_equal [11, 5, true], vals end + test "#load_defaults" do + config = Config.global.load_defaults 0.3 + assert_same Config.global, config + assert_same true, config.inherited?(:debug) + assert_same false, config.inherited?(:sasl_ir) + assert_same false, config.sasl_ir + # does not _reset_ default + config.debug = true + Config.global.load_defaults 0.3 + assert_same false, config.inherited?(:debug) + assert_same true, config.debug? + # does not change parent + child = Config.global.new + grandchild = child.new + greatgrandchild = grandchild.new + child.load_defaults :current + grandchild.load_defaults :next + greatgrandchild.load_defaults :future + assert_same Config.global, child.parent + assert_same child, grandchild.parent + assert_same grandchild, greatgrandchild.parent + end + end From 4aa0f5fd39d0b31362a96b167cec475f853bc488 Mon Sep 17 00:00:00 2001 From: nick evans Date: Sat, 22 Jun 2024 11:25:21 -0400 Subject: [PATCH 2/2] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor=20config=20at?= =?UTF-8?q?trs=20that=20inherit=20by=20default?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/net/imap/config.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/net/imap/config.rb b/lib/net/imap/config.rb index b5bb069a..a597bedd 100644 --- a/lib/net/imap/config.rb +++ b/lib/net/imap/config.rb @@ -288,9 +288,7 @@ def with(**attrs) def load_defaults(version) [Numeric, Symbol, String].any? { _1 === version } or raise ArgumentError, "expected number or symbol, got %p" % [version] - config = Config[version] - defaults = config.to_h.reject {|k,v| DEFAULT_TO_INHERIT.include?(k) } - update(**defaults) + update(**Config[version].defaults_hash) end # :call-seq: to_h -> hash @@ -298,6 +296,12 @@ def load_defaults(version) # Returns all config attributes in a hash. def to_h; data.members.to_h { [_1, send(_1)] } end + protected + + def defaults_hash + to_h.reject {|k,v| DEFAULT_TO_INHERIT.include?(k) } + end + @default = new( debug: false, open_timeout: 30, @@ -308,9 +312,7 @@ def to_h; data.members.to_h { [_1, send(_1)] } end @global = default.new - version_defaults[0.4] = Config[ - default.to_h.reject {|k,v| DEFAULT_TO_INHERIT.include?(k) } - ] + version_defaults[0.4] = Config[default.send(:defaults_hash)] version_defaults[0] = Config[0.4].dup.update( sasl_ir: false,