Skip to content

Commit c65e52f

Browse files
committed
♻️ Define config defaults with the attr definitions
At the cost of one big conflict now, this should significantly reduce future merge conflicts in the config file. It's also easier to match documentation with default values when they're right next to each other. This also assigns all of the original (`Config[0]`) defaults explicitly, and not only where they differ from Config.default.
1 parent a4f4206 commit c65e52f

File tree

2 files changed

+43
-38
lines changed

2 files changed

+43
-38
lines changed

lib/net/imap/config.rb

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,15 @@ def self.[](config)
212212
# See Net::IMAP.new and Net::IMAP#starttls.
213213
#
214214
# The default value is +30+ seconds.
215-
attr_accessor :open_timeout, type: Integer
215+
attr_accessor :open_timeout, type: Integer, default: 30
216216

217217
# Seconds to wait until an IDLE response is received, after
218218
# the client asks to leave the IDLE state.
219219
#
220220
# See Net::IMAP#idle and Net::IMAP#idle_done.
221221
#
222222
# The default value is +5+ seconds.
223-
attr_accessor :idle_response_timeout, type: Integer
223+
attr_accessor :idle_response_timeout, type: Integer, default: 5
224224

225225
# Whether to use the +SASL-IR+ extension when the server and \SASL
226226
# mechanism both support it. Can be overridden by the +sasl_ir+ keyword
@@ -236,7 +236,10 @@ def self.[](config)
236236
#
237237
# [+true+ <em>(default since +v0.4+)</em>]
238238
# Use +SASL-IR+ when it is supported by the server and the mechanism.
239-
attr_accessor :sasl_ir, type: :boolean
239+
attr_accessor :sasl_ir, type: :boolean, defaults: {
240+
0.0r => false,
241+
0.4r => true,
242+
}
240243

241244
# Controls the behavior of Net::IMAP#login when the +LOGINDISABLED+
242245
# capability is present. When enforced, Net::IMAP will raise a
@@ -260,7 +263,10 @@ def self.[](config)
260263
#
261264
attr_accessor :enforce_logindisabled, type: Enum[
262265
false, :when_capabilities_cached, true
263-
]
266+
], defaults: {
267+
0.0r => false,
268+
0.5r => true,
269+
}
264270

265271
# The maximum allowed server response size. When +nil+, there is no limit
266272
# on response size.
@@ -294,7 +300,10 @@ def self.[](config)
294300
#
295301
# * original: +nil+ <em>(no limit)</em>
296302
# * +0.5+: 512 MiB
297-
attr_accessor :max_response_size, type: Integer?
303+
attr_accessor :max_response_size, type: Integer?, defaults: {
304+
0.0r => nil,
305+
0.5r => 512 << 20, # 512 MiB
306+
}
298307

299308
# Controls the behavior of Net::IMAP#responses when called without any
300309
# arguments (+type+ or +block+).
@@ -324,7 +333,11 @@ def self.[](config)
324333
# Note: #responses_without_args is an alias for #responses_without_block.
325334
attr_accessor :responses_without_block, type: Enum[
326335
:silence_deprecation_warning, :warn, :frozen_dup, :raise,
327-
]
336+
], defaults: {
337+
0.0r => :silence_deprecation_warning,
338+
0.5r => :warn,
339+
0.6r => :frozen_dup,
340+
}
328341

329342
alias responses_without_args responses_without_block # :nodoc:
330343
alias responses_without_args= responses_without_block= # :nodoc:
@@ -369,7 +382,11 @@ def self.[](config)
369382
# ResponseParser _only_ uses AppendUIDData and CopyUIDData.
370383
attr_accessor :parser_use_deprecated_uidplus_data, type: Enum[
371384
true, :up_to_max_size, false
372-
]
385+
], defaults: {
386+
0.0r => true,
387+
0.5r => :up_to_max_size,
388+
0.6r => false,
389+
}
373390

374391
# The maximum +uid-set+ size that ResponseParser will parse into
375392
# deprecated UIDPlusData. This limit only applies when
@@ -393,7 +410,13 @@ def self.[](config)
393410
# * +0.5+: <tt>100</tt>
394411
# * +0.6+: <tt>0</tt>
395412
#
396-
attr_accessor :parser_max_deprecated_uidplus_data_size, type: Integer
413+
attr_accessor :parser_max_deprecated_uidplus_data_size, type: Integer,
414+
defaults: {
415+
0.0r => 10_000,
416+
0.4r => 1_000,
417+
0.5r => 100,
418+
0.6r => 0,
419+
}
397420

398421
# Creates a new config object and initialize its attribute with +attrs+.
399422
#
@@ -482,34 +505,6 @@ def defaults_hash
482505

483506
@global = default.new
484507

485-
version_defaults[0r] = {
486-
sasl_ir: false,
487-
responses_without_block: :silence_deprecation_warning,
488-
enforce_logindisabled: false,
489-
max_response_size: nil,
490-
parser_use_deprecated_uidplus_data: true,
491-
parser_max_deprecated_uidplus_data_size: 10_000,
492-
}
493-
494-
version_defaults[0.4r] = {
495-
sasl_ir: true,
496-
parser_max_deprecated_uidplus_data_size: 1000,
497-
}
498-
499-
version_defaults[0.5r] = {
500-
enforce_logindisabled: true,
501-
max_response_size: 512 << 20, # 512 MiB
502-
responses_without_block: :warn,
503-
parser_use_deprecated_uidplus_data: :up_to_max_size,
504-
parser_max_deprecated_uidplus_data_size: 100,
505-
}
506-
507-
version_defaults[0.6r] = {
508-
responses_without_block: :frozen_dup,
509-
parser_use_deprecated_uidplus_data: false,
510-
parser_max_deprecated_uidplus_data_size: 0,
511-
}
512-
513508
AttrVersionDefaults.compile_version_defaults!
514509

515510
if ($VERBOSE || $DEBUG) && self[:current].to_h != self[:default].to_h

lib/net/imap/config/attr_version_defaults.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,23 @@ module AttrVersionDefaults
3838

3939
# :stopdoc: internal APIs only
4040

41+
def attr_accessor(name, defaults: nil, default: (unset = true), **kw)
42+
unless unset
43+
defaults ||= { 0.0r => default }
44+
end
45+
defaults&.each_pair do |version, default|
46+
AttrVersionDefaults.version_defaults[version] ||= {}
47+
AttrVersionDefaults.version_defaults[version][name] = default
48+
end
49+
super(name, **kw)
50+
end
51+
4152
def self.compile_version_defaults!
4253
# Temporarily assign Config.default, enabling #load_defaults(:default)
4354
version_defaults[:default] = Config.default
4455
# Use #load_defaults so some attributes are inherited from global.
4556
version_defaults[:default] = Config.new.load_defaults(:default).freeze
46-
version_defaults[0.0r] = Config[:default].dup
47-
.update(**version_defaults[0.0r]).freeze
57+
version_defaults[0.0r] = Config[version_defaults.fetch(0.0r)]
4858

4959
VERSIONS.each_cons(2) do |prior, version|
5060
updates = version_defaults[version]

0 commit comments

Comments
 (0)