Skip to content

Commit 958e872

Browse files
authored
🔀 Merge pull request #301 from nevans/config-load_defaults
🔧 Add `Config#load_defaults`
2 parents 394452e + 4aa0f5f commit 958e872

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

lib/net/imap/config.rb

+28-3
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ class IMAP
8787
# Net::IMAP.debug = true
8888
# client.config.debug? # => true
8989
#
90+
# Use #load_defaults to globally behave like a specific version:
91+
# client = Net::IMAP.new(hostname)
92+
# client.config.sasl_ir # => true
93+
# Net::IMAP.config.load_defaults 0.3
94+
# client.config.sasl_ir # => false
95+
#
9096
# === Named defaults
9197
# In addition to +x.y+ version numbers, the following aliases are supported:
9298
#
@@ -270,11 +276,32 @@ def with(**attrs)
270276
block_given? ? yield(copy) : copy
271277
end
272278

279+
# :call-seq: load_defaults(version) -> self
280+
#
281+
# Resets the current config to behave like the versioned default
282+
# configuration for +version+. #parent will not be changed.
283+
#
284+
# Some config attributes default to inheriting from their #parent (which
285+
# is usually Config.global) and are left unchanged, for example: #debug.
286+
#
287+
# See Config@Versioned+defaults and Config@Named+defaults.
288+
def load_defaults(version)
289+
[Numeric, Symbol, String].any? { _1 === version } or
290+
raise ArgumentError, "expected number or symbol, got %p" % [version]
291+
update(**Config[version].defaults_hash)
292+
end
293+
273294
# :call-seq: to_h -> hash
274295
#
275296
# Returns all config attributes in a hash.
276297
def to_h; data.members.to_h { [_1, send(_1)] } end
277298

299+
protected
300+
301+
def defaults_hash
302+
to_h.reject {|k,v| DEFAULT_TO_INHERIT.include?(k) }
303+
end
304+
278305
@default = new(
279306
debug: false,
280307
open_timeout: 30,
@@ -285,9 +312,7 @@ def to_h; data.members.to_h { [_1, send(_1)] } end
285312

286313
@global = default.new
287314

288-
version_defaults[0.4] = Config[
289-
default.to_h.reject {|k,v| DEFAULT_TO_INHERIT.include?(k) }
290-
]
315+
version_defaults[0.4] = Config[default.send(:defaults_hash)]
291316

292317
version_defaults[0] = Config[0.4].dup.update(
293318
sasl_ir: false,

test/net/imap/test_config.rb

+23
Original file line numberDiff line numberDiff line change
@@ -332,4 +332,27 @@ class ConfigTest < Test::Unit::TestCase
332332
assert_equal [11, 5, true], vals
333333
end
334334

335+
test "#load_defaults" do
336+
config = Config.global.load_defaults 0.3
337+
assert_same Config.global, config
338+
assert_same true, config.inherited?(:debug)
339+
assert_same false, config.inherited?(:sasl_ir)
340+
assert_same false, config.sasl_ir
341+
# does not _reset_ default
342+
config.debug = true
343+
Config.global.load_defaults 0.3
344+
assert_same false, config.inherited?(:debug)
345+
assert_same true, config.debug?
346+
# does not change parent
347+
child = Config.global.new
348+
grandchild = child.new
349+
greatgrandchild = grandchild.new
350+
child.load_defaults :current
351+
grandchild.load_defaults :next
352+
greatgrandchild.load_defaults :future
353+
assert_same Config.global, child.parent
354+
assert_same child, grandchild.parent
355+
assert_same grandchild, greatgrandchild.parent
356+
end
357+
335358
end

0 commit comments

Comments
 (0)