Skip to content

Commit c1891ed

Browse files
committed
🔧 Allow Hash input to Config.[]
1 parent fa78037 commit c1891ed

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/net/imap/config.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,19 @@ def self.default; @default end
6666
# The global config object. Also available from Net::IMAP.config.
6767
def self.global; @global end
6868

69-
def self.[](config) # :nodoc: unfinished API
69+
# :call-seq:
70+
# Net::IMAP::Config[hash] -> new frozen config
71+
# Net::IMAP::Config[config] -> same config
72+
#
73+
# Given a Hash, creates a new _frozen_ config which inherits from
74+
# Config.global. Use Config.new for an unfrozen config.
75+
#
76+
# Given a config, returns that same config.
77+
def self.[](config)
7078
if config.is_a?(Config) || config.nil? && global.nil?
7179
config
80+
elsif config.respond_to?(:to_hash)
81+
new(global, **config).freeze
7282
else
7383
raise TypeError, "no implicit conversion of %s to %s" % [
7484
config.class, Config

test/net/imap/test_config.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,22 @@ class ConfigTest < Test::Unit::TestCase
135135
assert_equal false, child.debug?
136136
end
137137

138+
test ".[] with a hash" do
139+
config = Config[{responses_without_block: :raise, sasl_ir: false}]
140+
assert config.frozen?
141+
refute config.sasl_ir?
142+
assert config.inherited?(:debug)
143+
refute config.inherited?(:sasl_ir)
144+
assert_same Config.global, config.parent
145+
assert_same :raise, config.responses_without_block
146+
end
147+
138148
test ".new always sets a parent" do
139149
assert_same Config.global, Config.new.parent
140150
assert_same Config.default, Config.new(Config.default).parent
141151
assert_same Config.global, Config.new(Config.global).parent
152+
assert_equal true, Config.new({debug: true}, debug: false).parent.debug?
153+
assert_equal true, Config.new({debug: true}, debug: false).parent.frozen?
142154
end
143155

144156
test "#freeze" do

0 commit comments

Comments
 (0)