Class: Net::IMAP::Config
Relationships & Source Files | |
Namespace Children | |
Modules:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
|
|
Inherits: | Object |
Defined in: | lib/net/imap/config.rb, lib/net/imap/config/attr_accessors.rb, lib/net/imap/config/attr_inheritance.rb, lib/net/imap/config/attr_type_coercion.rb |
Overview
Config
(available since v0.4.13
) stores configuration options for ::Net::IMAP
clients. The global configuration can be seen at either config or .global, and the client-specific configuration can be seen at #config.
When creating a new client, all unhandled keyword arguments to new are delegated to .new. Every client has its own config.
debug_client = Net::IMAP.new(hostname, debug: true)
quiet_client = Net::IMAP.new(hostname, debug: false)
debug_client.config.debug? # => true
quiet_client.config.debug? # => false
Inheritance
Configs have a parent config, and any attributes which have not been set locally will inherit the parent’s value. Every client creates its own specific config. By default, client configs inherit from .global.
plain_client = Net::IMAP.new(hostname)
debug_client = Net::IMAP.new(hostname, debug: true)
quiet_client = Net::IMAP.new(hostname, debug: false)
plain_client.config.inherited?(:debug) # => true
debug_client.config.inherited?(:debug) # => false
quiet_client.config.inherited?(:debug) # => false
plain_client.config.debug? # => false
debug_client.config.debug? # => true
quiet_client.config.debug? # => false
# Net::IMAP.debug is delegated to Net::IMAP::Config.global.debug
Net::IMAP.debug = true
plain_client.config.debug? # => true
debug_client.config.debug? # => true
quiet_client.config.debug? # => false
Net::IMAP.debug = false
plain_client.config.debug = true
plain_client.config.inherited?(:debug) # => false
plain_client.config.debug? # => true
plain_client.config.reset(:debug)
plain_client.config.inherited?(:debug) # => true
plain_client.config.debug? # => false
Versioned defaults
The effective default configuration for a specific x.y
version of net-imap
can be loaded with the config keyword argument to new. Requesting default configurations for previous versions enables extra backward compatibility with those versions:
client = Net::IMAP.new(hostname, config: 0.3)
client.config.sasl_ir # => false
client.config.responses_without_block # => :silence_deprecation_warning
client = Net::IMAP.new(hostname, config: 0.4)
client.config.sasl_ir # => true
client.config.responses_without_block # => :silence_deprecation_warning
client = Net::IMAP.new(hostname, config: 0.5)
client.config.sasl_ir # => true
client.config.responses_without_block # => :warn
client = Net::IMAP.new(hostname, config: :future)
client.config.sasl_ir # => true
client.config.responses_without_block # => :raise
The versioned default configs inherit certain specific config options from .global, for example #debug
:
client = Net::IMAP.new(hostname, config: 0.4)
Net::IMAP.debug = false
client.config.debug? # => false
Net::IMAP.debug = true
client.config.debug? # => true
Use #load_defaults to globally behave like a specific version:
client = Net::IMAP.new(hostname)
client.config.sasl_ir # => true
Net::IMAP.config.load_defaults 0.3
client.config.sasl_ir # => false
Named defaults
In addition to x.y
version numbers, the following aliases are supported:
:default
-
An alias for
:current
.NOTE: This is not the same as Config.default. It inherits some attributes from Config.global, for example: #debug.
:current
-
An alias for the current
x.y
version’s defaults. :next
-
The planned config for the next
x.y
version. :future
-
The planned eventual config for some future
x.y
version.
For example, to raise exceptions for all current deprecations:
client = Net::IMAP.new(hostname, config: :future)
client.responses # raises an ArgumentError
Thread Safety
NOTE: Updates to config objects are not synchronized for thread-safety.
Constant Summary
-
DEFAULT_TO_INHERIT =
private
Array of attribute names that are not loaded by #load_defaults.
%i[debug].freeze
AttrInheritance
- Included
Class Method Summary
-
Net::IMAP::Config ⇒ Config
Given a version number, returns the default configuration for the target version.
-
.default
The default config, which is hardcoded and frozen.
-
.global
The global config object.
-
.new(parent = Config.global, **attrs) {|_self| ... } ⇒ Config
constructor
Creates a new config object and initialize its attribute with
attrs
. -
.version_defaults
A hash of hard-coded configurations, indexed by version number or name.
Instance Attribute Summary
AttrInheritance
- Included
#parent | The parent |
AttrAccessors
- Included
#data | internal API. |
Instance Method Summary
-
#load_defaults(version) ⇒ self
Resets the current config to behave like the versioned default configuration for
version
. -
#to_h ⇒ Hash
Returns all config attributes in a hash.
-
#update(**attrs) ⇒ self
Assigns all of the provided
attrs
to this config, and returnsself
. -
#with(**attrs) ⇒ Config
Without a block, returns a new config which inherits from self.
- #defaults_hash protected
- #responses_without_args Internal use only
- #responses_without_args= Internal use only
AttrInheritance
- Included
#inherited? | Returns |
#initialize | :notnew: |
#new | Creates a new config, which inherits from |
#reset | Resets an |
#initialize_copy |
AttrAccessors
- Included
#freeze | Freezes the internal attributes struct, in addition to |
#initialize | :notnew: |
#initialize_clone, #initialize_dup |
Constructor Details
.new(parent = Config.global, **attrs) {|_self| ... } ⇒ Config
Creates a new config object and initialize its attribute with attrs
.
If parent
is not given, the global config is used by default.
If a block is given, the new config object is yielded to it.
# File 'lib/net/imap/config.rb', line 293
def initialize(parent = Config.global, **attrs) super(parent) update(**attrs) yield self if block_given? end
Class Method Details
Net::IMAP::Config ⇒ Config
Net::IMAP::Config ⇒ Config
Net::IMAP::Config ⇒ Config
Net::IMAP::Config ⇒ Config
Config
Net::IMAP::Config ⇒ Config
Net::IMAP::Config ⇒ Config
Net::IMAP::Config ⇒ Config
Given a version number, returns the default configuration for the target version. See Config@Versioned+defaults.
Given a version name, returns the default configuration for the target version. See Config@Named+defaults.
Given a Hash, creates a new frozen config which inherits from .global. Use .new for an unfrozen config.
Given a config, returns that same config.
# File 'lib/net/imap/config.rb', line 151
def self.[](config) if config.is_a?(Config) then config elsif config.nil? && global.nil? then nil elsif config.respond_to?(:to_hash) then new(global, **config).freeze else version_defaults.fetch(config) do case config when Numeric raise RangeError, "unknown config version: %p" % [config] when Symbol raise KeyError, "unknown config name: %p" % [config] else raise TypeError, "no implicit conversion of %s to %s" % [ config.class, Config ] end end end end
.default
The default config, which is hardcoded and frozen.
# File 'lib/net/imap/config.rb', line 126
def self.default; @default end
.global
The global config object. Also available from Net::IMAP.config.
# File 'lib/net/imap/config.rb', line 129
def self.global; @global if defined?(@global) end
.version_defaults
A hash of hard-coded configurations, indexed by version number or name.
# File 'lib/net/imap/config.rb', line 132
def self.version_defaults; @version_defaults end
Instance Method Details
#defaults_hash (protected)
[ GitHub ]# File 'lib/net/imap/config.rb', line 357
def defaults_hash to_h.reject {|k,v| DEFAULT_TO_INHERIT.include?(k) } end
#load_defaults(version) ⇒ self
Resets the current config to behave like the versioned default configuration for version
. #parent
will not be changed.
Some config attributes default to inheriting from their #parent
(which is usually .global) and are left unchanged, for example: #debug
.
See Config@Versioned+defaults and Config@Named+defaults.
# File 'lib/net/imap/config.rb', line 344
def load_defaults(version) [Numeric, Symbol, String].any? { _1 === version } or raise ArgumentError, "expected number or symbol, got %p" % [version] update(**Config[version].defaults_hash) end
#responses_without_args
# File 'lib/net/imap/config.rb', line 280
alias responses_without_args responses_without_block # :nodoc:
#responses_without_args=
# File 'lib/net/imap/config.rb', line 281
alias responses_without_args= responses_without_block= # :nodoc:
#to_h ⇒ Hash
Returns all config attributes in a hash.
# File 'lib/net/imap/config.rb', line 353
def to_h; data.members.to_h { [_1, send(_1)] } end
#update(**attrs) ⇒ self
Assigns all of the provided attrs
to this config, and returns self
.
An ArgumentError is raised unless every key in attrs
matches an assignment method on Config
.
NOTE: #update is not atomic. If an exception is raised due to an invalid attribute value,
attrs
may be partially applied.
# File 'lib/net/imap/config.rb', line 309
def update(**attrs) unless (bad = attrs.keys.reject { respond_to?(:"#{_1}=") }).empty? raise ArgumentError, "invalid config options: #{bad.join(", ")}" end attrs.each do send(:"#{_1}=", _2) end self end
#with(**attrs) ⇒ Config
#with(**attrs) {|config| ... } ⇒ result
Config
#with(**attrs) {|config| ... } ⇒ result
Without a block, returns a new config which inherits from self. With a block, yields the new config and returns the block’s result.
If no keyword arguments are given, an ArgumentError will be raised.
If self
is frozen, the copy will also be frozen.