Skip to content

Commit 853fd13

Browse files
committed
🔧 Add recursive Config#inherits_defaults?
Returns `true` when all attributes inherit from `Config.default`, the version number (as a Rational) when all attributes inherit from a versioned default, `nil` if any attributes inherit from `Config.global` overrides (but not from non-global ancestors), or `false` when any attributes have been overridden by `self` or an ancestor (besides global or default configs),
1 parent 094d616 commit 853fd13

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

lib/net/imap/config/attr_inheritance.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,36 @@ def inherited?(*attrs)
7373
attrs.all? { data[_1] == INHERITED }
7474
end
7575

76+
# :call-seq:
77+
# inherits_defaults?(*attrs) -> true | Rational | nil | false
78+
#
79+
# Returns whether all +attrs+ are inherited from a default config.
80+
# When no +attrs+ are given, returns whether *all* attributes are
81+
# inherited from a default config.
82+
#
83+
# Returns +true+ when all attributes inherit from Config.default, the
84+
# version number (as a Rational) when all attributes inherit from a
85+
# versioned default (see Config@Versioned+defaults), +nil+ if any
86+
# attributes inherit from Config.global overrides (but not from
87+
# non-global ancestors), or +false+ when any attributes have been
88+
# overridden by +self+ or an ancestor (besides global or default
89+
# configs),
90+
#
91+
# Related: #overrides?
92+
def inherits_defaults?(*attrs)
93+
if equal?(Config.default)
94+
true
95+
elsif equal?(Config.global)
96+
true if inherited?(*attrs)
97+
elsif (v = AttrVersionDefaults::VERSIONS.find { equal? Config[_1] })
98+
attrs = DEFAULT_TO_INHERIT if attrs.empty?
99+
attrs &= DEFAULT_TO_INHERIT
100+
(attrs.empty? || parent.inherits_defaults?(*attrs)) && v
101+
else
102+
inherited?(*attrs) && parent.inherits_defaults?(*attrs)
103+
end
104+
end
105+
76106
# :call-seq:
77107
# overrides?(attr) -> true or false
78108
# overrides?(*attrs) -> true or false

test/net/imap/test_config.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,56 @@ def duck.to_r = 1/11111
349349
assert child.inherited?(:debug, :idle_response_timeout, :open_timeout)
350350
end
351351

352+
test "#inherits_defaults?" do
353+
assert_same true, Config.default.inherits_defaults?
354+
assert_same true, Config.default.inherits_defaults?(:sasl_ir, :open_timeout)
355+
356+
assert_equal true, Config.global.inherits_defaults?
357+
Config.version_defaults.each do |name, config|
358+
version = config.inherits_defaults?
359+
assert_kind_of Rational, version
360+
assert_same Config[version], config
361+
assert_same version, config.inherits_defaults?(:sasl_ir, :open_timeout)
362+
end
363+
364+
Config.global.debug = false
365+
assert_equal nil, Config.global.inherits_defaults?
366+
assert_equal true, Config.global.inherits_defaults?(:sasl_ir, :open_timeout)
367+
Config.version_defaults.each do |name, config|
368+
assert_equal nil, config.inherits_defaults?
369+
version = config.inherits_defaults?(:sasl_ir, :open_timeout)
370+
assert_kind_of Rational, version
371+
assert_same Config[version], config
372+
end
373+
374+
config = Config.new 0.5
375+
assert_equal nil, config.inherits_defaults?
376+
Config.global.reset
377+
assert_equal 0.5r, config.inherits_defaults?
378+
379+
config.debug = false
380+
assert_equal false, config.inherits_defaults?
381+
assert_equal 0.5r, config.inherits_defaults?(:sasl_ir, :open_timeout)
382+
383+
child = config.new debug: true, sasl_ir: true
384+
assert_equal false, child.inherits_defaults?
385+
assert_equal false, child.inherits_defaults?(:sasl_ir, :open_timeout)
386+
assert_equal 0.5r, child.inherits_defaults?(:open_timeout)
387+
388+
Config.global.sasl_ir = true
389+
Config.global.open_timeout = 111
390+
# inherits defaults from 0.5
391+
assert_equal false, config.inherits_defaults?
392+
assert_equal 0.5r, config.inherits_defaults?(:sasl_ir, :open_timeout)
393+
config.reset
394+
assert_equal 0.5r, config.inherits_defaults?
395+
# inherits overrides from global
396+
config = Config.new
397+
assert_equal nil, config.inherits_defaults?
398+
assert_equal nil, config.inherits_defaults?(:sasl_ir, :open_timeout)
399+
assert_equal true, config.inherits_defaults?(:debug)
400+
end
401+
352402
test "#overrides?" do
353403
base = Config.new debug: false, open_timeout: 99, idle_response_timeout: 15
354404
child = base.new debug: true, open_timeout: 15, idle_response_timeout: 10

0 commit comments

Comments
 (0)