Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/net/imap/config/attr_inheritance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,22 @@ def initialize(parent = nil) # :notnew:
# Creates a new config, which inherits from +self+.
def new(**attrs) self.class.new(self, **attrs) end

# :call-seq:
# inherited?(attr) -> true or false
# inherited?(*attrs) -> true or false
# inherited? -> true or false
#
# Returns +true+ if +attr+ is inherited from #parent and not overridden
# by this config.
def inherited?(attr) data[attr] == INHERITED end
#
# When multiple +attrs+ are given, returns +true+ if *all* of them are
# inherited, or +false+ if any of them are overriden. When no +attrs+
# are given, returns +true+ if *all* attributes are inherited, or
# +false+ if any attribute is overriden.
def inherited?(*attrs)
attrs = data.members if attrs.empty?
attrs.all? { data[_1] == INHERITED }
end

# :call-seq:
# reset -> self
Expand Down
24 changes: 22 additions & 2 deletions test/net/imap/test_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,21 +308,41 @@ def duck.to_r = 1/11111
assert_equal 1, copy.open_timeout
end

test "#inherited? and #reset(attr)" do
test "#inherited? and #reset" do
base = Config.new debug: false, open_timeout: 99, idle_response_timeout: 15
child = base.new debug: true, open_timeout: 15, idle_response_timeout: 10
refute child.inherited?
refute child.inherited?(:idle_response_timeout)
refute child.inherited?(:idle_response_timeout, :open_timeout)
refute child.inherited?(:sasl_ir, :open_timeout)
assert child.inherited?(:sasl_ir, :max_response_size)
assert child.inherited?(:sasl_ir)

assert_equal 10, child.reset(:idle_response_timeout)
assert child.inherited?(:idle_response_timeout)
assert_equal 15, child.idle_response_timeout
refute child.inherited?
assert child.inherited?(:idle_response_timeout)
refute child.inherited?(:idle_response_timeout, :open_timeout)
refute child.inherited?(:sasl_ir, :open_timeout)
assert child.inherited?(:sasl_ir, :max_response_size)
assert child.inherited?(:sasl_ir)
refute child.inherited?(:open_timeout)
refute child.inherited?(:debug)

child.debug = false
refute child.inherited?
refute child.inherited?(:debug)

assert_equal false, child.reset(:debug)
refute child.inherited?
assert child.inherited?(:debug)
assert_equal false, child.debug

assert_equal nil, child.reset(:debug)

assert_same child, child.reset
assert child.inherited?
assert child.inherited?(:debug, :idle_response_timeout, :open_timeout)
end

test "#reset all attributes" do
Expand Down