Skip to content

Commit 679fa99

Browse files
committed
🔧 Update Config#inherited? for any number of args
When multiple attrs are given, returns `true` if *all* of them are inherited (or `false` if any is overriden). When no attrs are given, returns `true` if *all* attributes are inherited (or `false` if any is overriden).
1 parent 4379aea commit 679fa99

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

lib/net/imap/config/attr_inheritance.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,22 @@ def initialize(parent = nil) # :notnew:
5454
# Creates a new config, which inherits from +self+.
5555
def new(**attrs) self.class.new(self, **attrs) end
5656

57+
# :call-seq:
58+
# inherited?(attr) -> true or false
59+
# inherited?(*attrs) -> true or false
60+
# inherited? -> true or false
61+
#
5762
# Returns +true+ if +attr+ is inherited from #parent and not overridden
5863
# by this config.
59-
def inherited?(attr) data[attr] == INHERITED end
64+
#
65+
# When multiple +attrs+ are given, returns +true+ if *all* of them are
66+
# inherited, or +false+ if any of them are overriden. When no +attrs+
67+
# are given, returns +true+ if *all* attributes are inherited, or
68+
# +false+ if any attribute is overriden.
69+
def inherited?(*attrs)
70+
attrs = data.members if attrs.empty?
71+
attrs.all? { data[_1] == INHERITED }
72+
end
6073

6174
# :call-seq:
6275
# reset -> self

test/net/imap/test_config.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,21 +308,41 @@ def duck.to_r = 1/11111
308308
assert_equal 1, copy.open_timeout
309309
end
310310

311-
test "#inherited? and #reset(attr)" do
311+
test "#inherited? and #reset" do
312312
base = Config.new debug: false, open_timeout: 99, idle_response_timeout: 15
313313
child = base.new debug: true, open_timeout: 15, idle_response_timeout: 10
314+
refute child.inherited?
314315
refute child.inherited?(:idle_response_timeout)
316+
refute child.inherited?(:idle_response_timeout, :open_timeout)
317+
refute child.inherited?(:sasl_ir, :open_timeout)
318+
assert child.inherited?(:sasl_ir, :max_response_size)
319+
assert child.inherited?(:sasl_ir)
320+
315321
assert_equal 10, child.reset(:idle_response_timeout)
316-
assert child.inherited?(:idle_response_timeout)
317322
assert_equal 15, child.idle_response_timeout
323+
refute child.inherited?
324+
assert child.inherited?(:idle_response_timeout)
325+
refute child.inherited?(:idle_response_timeout, :open_timeout)
326+
refute child.inherited?(:sasl_ir, :open_timeout)
327+
assert child.inherited?(:sasl_ir, :max_response_size)
328+
assert child.inherited?(:sasl_ir)
318329
refute child.inherited?(:open_timeout)
319330
refute child.inherited?(:debug)
331+
320332
child.debug = false
333+
refute child.inherited?
321334
refute child.inherited?(:debug)
335+
322336
assert_equal false, child.reset(:debug)
337+
refute child.inherited?
323338
assert child.inherited?(:debug)
324339
assert_equal false, child.debug
340+
325341
assert_equal nil, child.reset(:debug)
342+
343+
assert_same child, child.reset
344+
assert child.inherited?
345+
assert child.inherited?(:debug, :idle_response_timeout, :open_timeout)
326346
end
327347

328348
test "#reset all attributes" do

0 commit comments

Comments
 (0)