Skip to content

Commit 0761448

Browse files
committed
♻️ Compile Config.default using AttrVersionDefaults
Similar to the versioned defaults, this could be a source of conflicts when merging, rebasing, or/cherry-picking feature and backport branches. Now that all of the attributes specify their defaults, the current version's default config can be compiled from that. The sanity-check at the end of `config.rb` has been removed. It was there to protect against bad merges. Now that `Config.default` is also compiled from the version defaults added to each attr_accessor, the tests in `test/net/imap/test_config.rb` are good enough.
1 parent c65e52f commit 0761448

File tree

2 files changed

+14
-22
lines changed

2 files changed

+14
-22
lines changed

lib/net/imap/config.rb

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def self.[](config)
194194
#
195195
# *NOTE:* Versioned default configs inherit #debug from Config.global, and
196196
# #load_defaults will not override #debug.
197-
attr_accessor :debug, type: :boolean
197+
attr_accessor :debug, type: :boolean, default: false
198198

199199
# method: debug?
200200
# :call-seq: debug? -> boolean
@@ -491,28 +491,10 @@ def defaults_hash
491491
to_h.reject {|k,v| DEFAULT_TO_INHERIT.include?(k) }
492492
end
493493

494-
@default = new(
495-
debug: false,
496-
open_timeout: 30,
497-
idle_response_timeout: 5,
498-
sasl_ir: true,
499-
enforce_logindisabled: true,
500-
max_response_size: 512 << 20, # 512 MiB
501-
responses_without_block: :warn,
502-
parser_use_deprecated_uidplus_data: :up_to_max_size,
503-
parser_max_deprecated_uidplus_data_size: 100,
504-
).freeze
505-
506-
@global = default.new
507-
494+
@default = AttrVersionDefaults.compile_default!
495+
@global = default.new
508496
AttrVersionDefaults.compile_version_defaults!
509497

510-
if ($VERBOSE || $DEBUG) && self[:current].to_h != self[:default].to_h
511-
warn "Misconfigured Net::IMAP::Config[:current] => %p,\n" \
512-
" not equal to Net::IMAP::Config[:default] => %p" % [
513-
self[:current].to_h, self[:default].to_h
514-
]
515-
end
516498
end
517499
end
518500
end

lib/net/imap/config/attr_version_defaults.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ module AttrVersionDefaults
4040

4141
def attr_accessor(name, defaults: nil, default: (unset = true), **kw)
4242
unless unset
43-
defaults ||= { 0.0r => default }
43+
version = DEFAULT_TO_INHERIT.include?(name) ? nil : 0.0r
44+
defaults = { version => default }
4445
end
4546
defaults&.each_pair do |version, default|
4647
AttrVersionDefaults.version_defaults[version] ||= {}
@@ -49,6 +50,15 @@ def attr_accessor(name, defaults: nil, default: (unset = true), **kw)
4950
super(name, **kw)
5051
end
5152

53+
def self.compile_default!
54+
raise "Config.default already compiled" if Config.default
55+
default = VERSIONS.select { _1 <= CURRENT_VERSION }
56+
.filter_map { version_defaults[_1] }
57+
.prepend(version_defaults.delete(nil))
58+
.inject(&:merge)
59+
Config.new(default).freeze
60+
end
61+
5262
def self.compile_version_defaults!
5363
# Temporarily assign Config.default, enabling #load_defaults(:default)
5464
version_defaults[:default] = Config.default

0 commit comments

Comments
 (0)