Skip to content

Commit

Permalink
🔧 Allow Hash input to Config.[]
Browse files Browse the repository at this point in the history
  • Loading branch information
nevans committed Jun 21, 2024
1 parent 61dde33 commit a3e1385
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/net/imap/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,19 @@ def self.default; @default end
# The global config object. Also available from Net::IMAP.config.
def self.global; @global end

def self.[](config) # :nodoc: unfinished API
# :call-seq:
# Net::IMAP::Config[hash] -> new frozen config
# Net::IMAP::Config[config] -> same config
#
# Given a Hash, creates a new _frozen_ config which inherits from
# Config.global. Use Config.new for an unfrozen config.
#
# Given a config, returns that same config.
def self.[](config)
if config.is_a?(Config) || config.nil? && global.nil?
config
elsif config.respond_to?(:to_hash)
new(global, **config).freeze
else
raise TypeError, "no implicit conversion of %s to %s" % [
config.class, Config
Expand Down
12 changes: 12 additions & 0 deletions test/net/imap/test_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,22 @@ class ConfigTest < Test::Unit::TestCase
assert_equal false, child.debug?
end

test ".[] with a hash" do
config = Config[{responses_without_block: :raise, sasl_ir: false}]
assert config.frozen?
refute config.sasl_ir?
assert config.inherited?(:debug)
refute config.inherited?(:sasl_ir)
assert_same Config.global, config.parent
assert_same :raise, config.responses_without_block
end

test ".new always sets a parent" do
assert_same Config.global, Config.new.parent
assert_same Config.default, Config.new(Config.default).parent
assert_same Config.global, Config.new(Config.global).parent
assert_equal true, Config.new({debug: true}, debug: false).parent.debug?
assert_equal true, Config.new({debug: true}, debug: false).parent.frozen?
end

test "#inherited? and #reset(attr)" do
Expand Down

0 comments on commit a3e1385

Please sign in to comment.