Skip to content

Commit 18fe6dd

Browse files
committed
🔧 Add very basic Config class (currently unused)
Nothing uses this new Config class yet. Currently, it holds only three config options: `debug`, `open_timeout`, and `idle_response_timeout`, but more will be added soon. This version does not have any support for inheritance. That will be added in one of the next commits.
1 parent 74130fb commit 18fe6dd

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

lib/net/imap.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,8 @@ class IMAP < Protocol
725725
"UTF8=ONLY" => "UTF8=ACCEPT",
726726
}.freeze
727727

728+
autoload :Config, File.expand_path("imap/config", __dir__)
729+
728730
autoload :SASL, File.expand_path("imap/sasl", __dir__)
729731
autoload :SASLAdapter, File.expand_path("imap/sasl_adapter", __dir__)
730732
autoload :StringPrep, File.expand_path("imap/stringprep", __dir__)

lib/net/imap/config.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
# :markup: markdown
3+
4+
module Net
5+
class IMAP
6+
7+
# Net::IMAP::Config stores configuration options for Net::IMAP clients.
8+
#
9+
# ## Thread Safety
10+
#
11+
# *NOTE:* Updates to config objects are not synchronized for thread-safety.
12+
#
13+
class Config
14+
15+
# The debug mode (boolean)
16+
attr_accessor :debug
17+
alias debug? debug
18+
19+
# Seconds to wait until a connection is opened.
20+
#
21+
# If the IMAP object cannot open a connection within this time,
22+
# it raises a Net::OpenTimeout exception. See Net::IMAP.new.
23+
attr_accessor :open_timeout
24+
25+
# Seconds to wait until an IDLE response is received, after
26+
# the client asks to leave the IDLE state. See Net::IMAP#idle_done.
27+
attr_accessor :idle_response_timeout
28+
29+
# Creates a new config object and initialize its attribute with +attrs+.
30+
#
31+
# If a block is given, the new config object is yielded to it.
32+
def initialize(**attrs)
33+
super()
34+
attrs.each do send(:"#{_1}=", _2) end
35+
yield self if block_given?
36+
end
37+
38+
end
39+
end
40+
end

test/net/imap/test_config.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
require "net/imap"
4+
require "test/unit"
5+
6+
class ConfigTest < Test::Unit::TestCase
7+
Config = Net::IMAP::Config
8+
9+
test "#debug" do
10+
assert Config.new(debug: true).debug
11+
refute Config.new(debug: false).debug
12+
assert Config.new(debug: true).debug?
13+
refute Config.new(debug: false).debug?
14+
config = Config.new do |c|
15+
c.debug = true
16+
end
17+
assert config.debug
18+
config = Config.new
19+
config.debug = true
20+
assert config.debug
21+
assert config.debug?
22+
config.debug = false
23+
refute config.debug
24+
refute config.debug?
25+
end
26+
27+
end

0 commit comments

Comments
 (0)