Skip to content

Commit 200b51d

Browse files
committed
🔧 Add Config#load_defaults
1 parent be8d5cd commit 200b51d

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

lib/net/imap/config.rb

Lines changed: 20 additions & 0 deletions
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,6 +276,20 @@ 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+.
283+
#
284+
# See Config@Versioned+defaults and Config@Named+defaults.
285+
def load_defaults(version)
286+
[Numeric, Symbol, String].any? { _1 === version } or
287+
raise ArgumentError, "expected number or symbol, got %p" % [version]
288+
config = Config[version]
289+
defaults = config.to_h.reject {|k,v| DEFAULT_TO_INHERIT.include?(k) }
290+
update(**defaults)
291+
end
292+
273293
# :call-seq: to_h -> hash
274294
#
275295
# Returns all config attributes in a hash.

test/net/imap/test_config.rb

Lines changed: 23 additions & 0 deletions
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)