Skip to content

Commit 2caa59d

Browse files
committed
🔧 Add Config#to_h
1 parent 98a9620 commit 2caa59d

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/net/imap/config.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ def initialize(parent = Config.global, **attrs)
147147
yield self if block_given?
148148
end
149149

150+
# :call-seq: to_h -> hash
151+
#
152+
# Returns all config attributes in a hash.
153+
def to_h; data.members.to_h { [_1, send(_1)] } end
154+
150155
@default = new(
151156
debug: false,
152157
open_timeout: 30,

test/net/imap/test_config.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,17 @@ class ConfigTest < Test::Unit::TestCase
209209
assert child.inherited?(:idle_response_timeout)
210210
end
211211

212+
test "#to_h" do
213+
expected = {
214+
debug: false, open_timeout: 30, idle_response_timeout: 5, sasl_ir: true,
215+
}
216+
attributes = Config::AttrAccessors::Struct.members
217+
default_hash = Config.default.to_h
218+
assert_equal expected, default_hash.slice(*expected.keys)
219+
assert_equal attributes, default_hash.keys
220+
global_hash = Config.global.to_h
221+
assert_equal attributes, global_hash.keys
222+
assert_equal expected, global_hash.slice(*expected.keys)
223+
end
224+
212225
end

0 commit comments

Comments
 (0)