Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔧 Add Config#load_defaults #301

Merged
merged 2 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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