Module: Net::IMAP::Config::AttrInheritance
- Included in:
- Net::IMAP::Config
- Defined in:
- lib/net/imap/config/attr_inheritance.rb
Overview
NOTE: The public methods on this module are part of the stable
public API of Net::IMAP::Config. But the module itself is an internal implementation detail, with no guarantee of backward compatibility.
attr_accessor methods will delegate to their #parent when the local
value does not contain an override. Inheritance forms a singly linked
list, so lookup will be O(n) on the number of ancestors. In
practice, the ancestor chain is not expected to be long. Without
customization, it is only three deep:
IMAP#config → Config.global → Config.default
When creating a client with the config keyword, for example to use
the appropriate defaults for an application or a library while still
relying on global for configuration of debug or logger, most likely
the ancestor chain is still only four deep:
IMAP#config → alternate defaults → Config.global → Config.default
Instance Attribute Summary collapse
-
#parent ⇒ Object
readonly
The parent Config object.
Class Method Summary collapse
-
.attr_accessor(name) ⇒ Object
:nodoc: internal API.
Instance Method Summary collapse
-
#inherited?(*attrs) ⇒ Boolean
:call-seq: inherited?(attr) -> true or false inherited?(*attrs) -> true or false inherited? -> true or false.
-
#inherits_defaults?(*attrs) ⇒ Boolean
:call-seq: inherits_defaults?(*attrs) -> true | Rational | nil | false.
-
#initialize(parent = nil) ⇒ Object
:notnew:.
-
#new(**attrs) ⇒ Object
Creates a new config, which inherits from
self. -
#overrides?(*attrs) ⇒ Boolean
:call-seq: overrides?(attr) -> true or false overrides?(*attrs) -> true or false overrides? -> true or false.
-
#reset(attr = nil) ⇒ Object
:call-seq: reset -> self reset(attr) -> attribute value.
Instance Attribute Details
#parent ⇒ Object (readonly)
The parent Config object
46 47 48 |
# File 'lib/net/imap/config/attr_inheritance.rb', line 46 def parent @parent end |
Class Method Details
.attr_accessor(name) ⇒ Object
:nodoc: internal API
39 40 41 42 43 |
# File 'lib/net/imap/config/attr_inheritance.rb', line 39 def self.attr_accessor(name) # :nodoc: internal API module_eval <<~RUBY, __FILE__, __LINE__ + 1 def #{name}; (val = super) == INHERITED ? parent&.#{name} : val end RUBY end |
Instance Method Details
#inherited?(*attrs) ⇒ Boolean
: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.
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.
Related: #overrides?
71 72 73 74 |
# File 'lib/net/imap/config/attr_inheritance.rb', line 71 def inherited?(*attrs) attrs = data.members if attrs.empty? attrs.all? { data[_1] == INHERITED } end |
#inherits_defaults?(*attrs) ⇒ Boolean
:call-seq:
inherits_defaults?(*attrs) -> true | Rational | nil | false
Returns whether all attrs are inherited from a default config.
When no attrs are given, returns whether all attributes are
inherited from a default config.
Returns true when all attributes inherit from Config.default, the
version number (as a Rational) when all attributes inherit from a
versioned default (see Config@Versioned+defaults), 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),
Related: #overrides?
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/net/imap/config/attr_inheritance.rb', line 92 def inherits_defaults?(*attrs) if equal?(Config.default) true elsif equal?(Config.global) true if inherited?(*attrs) elsif (v = AttrVersionDefaults::VERSIONS.find { equal? Config[_1] }) attrs = DEFAULT_TO_INHERIT if attrs.empty? attrs &= DEFAULT_TO_INHERIT (attrs.empty? || parent.inherits_defaults?(*attrs)) && v else inherited?(*attrs) && parent.inherits_defaults?(*attrs) end end |
#initialize(parent = nil) ⇒ Object
:notnew:
48 49 50 51 52 |
# File 'lib/net/imap/config/attr_inheritance.rb', line 48 def initialize(parent = nil) # :notnew: super() @parent = Config[parent] reset end |
#new(**attrs) ⇒ Object
Creates a new config, which inherits from self.
55 |
# File 'lib/net/imap/config/attr_inheritance.rb', line 55 def new(**attrs) self.class.new(self, **attrs) end |
#overrides?(*attrs) ⇒ Boolean
:call-seq:
overrides?(attr) -> true or false
overrides?(*attrs) -> true or false
overrides? -> true or false
Returns true if attr is defined on this config and not inherited
from #parent.
When multiple attrs are given, returns true if
any of them are defined on self. When no attrs are given,
returns true if any attribute is overriden.
Related: #inherited?
119 120 121 122 |
# File 'lib/net/imap/config/attr_inheritance.rb', line 119 def overrides?(*attrs) attrs = data.members if attrs.empty? attrs.any? { data[_1] != INHERITED } end |
#reset(attr = nil) ⇒ Object
:call-seq:
reset -> self
reset(attr) -> attribute value
Resets an attr to inherit from the #parent config.
When attr is nil or not given, all attributes are reset.
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/net/imap/config/attr_inheritance.rb', line 131 def reset(attr = nil) if attr.nil? data.members.each do |attr| data[attr] = INHERITED end self elsif inherited?(attr) nil else old, data[attr] = data[attr], INHERITED old end end |