Skip to content

Commit

Permalink
🔀 Merge pull request #301 from nevans/config-load_defaults
Browse files Browse the repository at this point in the history
🔧 Add `Config#load_defaults`
  • Loading branch information
nevans authored Jun 22, 2024
2 parents 394452e + 4aa0f5f commit 958e872
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
31 changes: 28 additions & 3 deletions lib/net/imap/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
#
Expand Down Expand Up @@ -270,11 +276,32 @@ 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]
update(**Config[version].defaults_hash)
end

# :call-seq: to_h -> hash
#
# 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,
Expand All @@ -285,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,
Expand Down
23 changes: 23 additions & 0 deletions test/net/imap/test_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 958e872

Please sign in to comment.