Skip to content

Commit fd518a5

Browse files
committed
🔧 Add a global config object
Config.global inherits from Config.default
1 parent 3063847 commit fd518a5

File tree

9 files changed

+55
-3
lines changed

9 files changed

+55
-3
lines changed

lib/net/imap.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,9 @@ class IMAP < Protocol
737737
include SSL
738738
end
739739

740+
# Returns the global Config object
741+
def self.config; Config.global end
742+
740743
# Returns the debug mode.
741744
def self.debug
742745
return @@debug

lib/net/imap/config.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ module Net
88
class IMAP
99

1010
# Net::IMAP::Config stores configuration options for Net::IMAP clients.
11+
# The global configuration can be seen at either Net::IMAP.config or
12+
# Net::IMAP::Config.global.
1113
#
1214
# ## Inheritance
1315
#
1416
# Configs have a parent[rdoc-ref:Config::AttrInheritance#parent] config, and
1517
# any attributes which have not been set locally will inherit the parent's
16-
# value.
18+
# value. Config.global inherits from Config.default.
1719
#
1820
# See the following methods, defined by Config::AttrInheritance:
1921
# - {#new}[rdoc-ref:Config::AttrInheritance#reset] -- create a new config
@@ -31,6 +33,19 @@ class Config
3133
# The default config, which is hardcoded and frozen.
3234
def self.default; @default end
3335

36+
# The global config object.
37+
def self.global; @global end
38+
39+
def self.[](config) # :nodoc: unfinished API
40+
if config.is_a?(Config) || config.nil? && global.nil?
41+
config
42+
else
43+
raise TypeError, "no implicit conversion of %s to %s" % [
44+
config.class, Config
45+
]
46+
end
47+
end
48+
3449
include AttrAccessors
3550
include AttrInheritance
3651

@@ -65,7 +80,7 @@ def self.default; @default end
6580
# If +parent+ is not given, the global config is used by default.
6681
#
6782
# If a block is given, the new config object is yielded to it.
68-
def initialize(parent = nil, **attrs)
83+
def initialize(parent = Config.global, **attrs)
6984
super(parent)
7085
attrs.each do send(:"#{_1}=", _2) end
7186
yield self if block_given?
@@ -77,6 +92,8 @@ def initialize(parent = nil, **attrs)
7792
idle_response_timeout: 5,
7893
).freeze
7994

95+
@global = default.new
96+
8097
end
8198
end
8299
end

lib/net/imap/config/attr_inheritance.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def #{name}; (val = super) == INHERITED ? parent&.#{name} : val end
3232

3333
def initialize(parent = nil) # :notnew:
3434
super()
35-
@parent = parent
35+
@parent = Config[parent]
3636
reset
3737
end
3838

test/net/imap/test_config.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
class ConfigTest < Test::Unit::TestCase
77
Config = Net::IMAP::Config
88

9+
setup do
10+
Config.global.reset
11+
end
12+
913
test "#debug" do
1014
assert Config.new(debug: true).debug
1115
refute Config.new(debug: false).debug
@@ -32,6 +36,23 @@ class ConfigTest < Test::Unit::TestCase
3236
refute default.debug?
3337
end
3438

39+
test ".global" do
40+
global = Config.global
41+
assert global.equal?(Config.global)
42+
assert global.is_a?(Config)
43+
assert_same Config.default, global.parent
44+
assert_equal false, global.debug?
45+
global.debug = true
46+
assert_equal true, global.debug?
47+
global.reset(:debug)
48+
assert_equal false, global.debug?
49+
refute global.frozen?
50+
end
51+
52+
test "Net::IMAP.config" do
53+
assert Net::IMAP.config.equal?(Config.global)
54+
end
55+
3556
test ".new(parent, ...) and inheritance" do
3657
base = Config.new debug: false
3758
child = Config.new(base)
@@ -76,6 +97,12 @@ class ConfigTest < Test::Unit::TestCase
7697
assert_equal false, child.debug?
7798
end
7899

100+
test ".new always sets a parent" do
101+
assert_same Config.global, Config.new.parent
102+
assert_same Config.default, Config.new(Config.default).parent
103+
assert_same Config.global, Config.new(Config.global).parent
104+
end
105+
79106
test "#inherited? and #reset(attr)" do
80107
base = Config.new debug: false, open_timeout: 99, idle_response_timeout: 15
81108
child = base.new debug: true, open_timeout: 15, idle_response_timeout: 10

test/net/imap/test_deprecated_client_options.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class DeprecatedClientOptionsTest < Test::Unit::TestCase
88
include Net::IMAP::FakeServer::TestHelper
99

1010
def setup
11+
Net::IMAP.config.reset
1112
@do_not_reverse_lookup = Socket.do_not_reverse_lookup
1213
Socket.do_not_reverse_lookup = true
1314
@threads = []

test/net/imap/test_imap.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class IMAPTest < Test::Unit::TestCase
1212
include Net::IMAP::FakeServer::TestHelper
1313

1414
def setup
15+
Net::IMAP.config.reset
1516
@do_not_reverse_lookup = Socket.do_not_reverse_lookup
1617
Socket.do_not_reverse_lookup = true
1718
@threads = []

test/net/imap/test_imap_capabilities.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class IMAPCapabilitiesTest < Test::Unit::TestCase
99
include Net::IMAP::FakeServer::TestHelper
1010

1111
def setup
12+
Net::IMAP.config.reset
1213
@do_not_reverse_lookup = Socket.do_not_reverse_lookup
1314
Socket.do_not_reverse_lookup = true
1415
@threads = []

test/net/imap/test_imap_response_data.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
class IMAPResponseDataTest < Test::Unit::TestCase
77

88
def setup
9+
Net::IMAP.config.reset
910
@do_not_reverse_lookup = Socket.do_not_reverse_lookup
1011
Socket.do_not_reverse_lookup = true
1112
end

test/net/imap/test_imap_response_parser.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class IMAPResponseParserTest < Test::Unit::TestCase
1111
extend NetIMAPTestHelpers::TestFixtureGenerators
1212

1313
def setup
14+
Net::IMAP.config.reset
1415
@do_not_reverse_lookup = Socket.do_not_reverse_lookup
1516
Socket.do_not_reverse_lookup = true
1617
end

0 commit comments

Comments
 (0)