Class: Net::IMAP::Config
- Inherits:
-
Object
- Object
- Net::IMAP::Config
- Includes:
- AttrAccessors, AttrInheritance, AttrTypeCoercion
- 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
Net::IMAP::Config (available since v0.4.13) stores configuration options for Net::IMAP clients. The global configuration can be seen at either Net::IMAP.config or Net::IMAP::Config.global, and the client-specific configuration can be seen at Net::IMAP#config.
When creating a new client, all unhandled keyword arguments to Net::IMAP.new are delegated to Config.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 Config.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 Net::IMAP.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 # => :frozen_dup
The versioned default configs inherit certain specific config options from Config.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.yversion’s defaults. :next-
The planned config for the next
x.yversion. :future-
The planned eventual config for some future
x.yversion.
For example, to disable all currently deprecated behavior:
client = Net::IMAP.new(hostname, config: :future)
client.config.response_without_args # => :frozen_dup
client.responses.frozen? # => true
client.responses.values.all?(&:frozen?) # => true
Thread Safety
NOTE: Updates to config objects are not synchronized for thread-safety.
Defined Under Namespace
Modules: AttrAccessors, AttrInheritance, AttrTypeCoercion
Constant Summary
Constants included from AttrTypeCoercion
AttrTypeCoercion::Enum, AttrTypeCoercion::NilOrInteger, AttrTypeCoercion::Types
Instance Attribute Summary
Attributes included from AttrInheritance
Class Method Summary collapse
-
.[](config) ⇒ Object
:call-seq: Net::IMAP::Config -> versioned config Net::IMAP::Config -> named config Net::IMAP::Config -> new frozen config Net::IMAP::Config -> same config.
-
.default ⇒ Object
The default config, which is hardcoded and frozen.
-
.global ⇒ Object
The global config object.
-
.version_defaults ⇒ Object
A hash of hard-coded configurations, indexed by version number or name.
Instance Method Summary collapse
-
#initialize(parent = Config.global, **attrs) {|_self| ... } ⇒ Config
constructor
Creates a new config object and initialize its attribute with
attrs. -
#load_defaults(version) ⇒ Object
:call-seq: load_defaults(version) -> self.
-
#to_h ⇒ Object
:call-seq: to_h -> hash.
-
#update(**attrs) ⇒ Object
:call-seq: update(**attrs) -> self.
-
#with(**attrs) ⇒ Object
:call-seq: with(**attrs) -> config with(**attrs) {|config| } -> result.
Methods included from AttrTypeCoercion
Methods included from AttrInheritance
attr_accessor, #inherited?, #new, #reset
Methods included from AttrAccessors
attr_accessor, #freeze, struct
Constructor Details
#initialize(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.
409 410 411 412 413 |
# File 'lib/net/imap/config.rb', line 409 def initialize(parent = Config.global, **attrs) super(parent) update(**attrs) yield self if block_given? end |
Class Method Details
.[](config) ⇒ Object
:call-seq:
Net::IMAP::Config[number] -> versioned config
Net::IMAP::Config[symbol] -> named config
Net::IMAP::Config[hash] -> new frozen config
Net::IMAP::Config[config] -> same 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 Config.global. Use Config.new for an unfrozen config.
Given a config, returns that same config.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/net/imap/config.rb', line 170 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[config] or case config when Numeric raise RangeError, "unknown config version: %p" % [config] when String, Symbol raise KeyError, "unknown config name: %p" % [config] else raise TypeError, "no implicit conversion of %s to %s" % [ config.class, Config ] end end end |
.default ⇒ Object
The default config, which is hardcoded and frozen.
128 |
# File 'lib/net/imap/config.rb', line 128 def self.default; @default end |
.global ⇒ Object
The global config object. Also available from Net::IMAP.config.
131 |
# File 'lib/net/imap/config.rb', line 131 def self.global; @global if defined?(@global) end |
.version_defaults ⇒ Object
A hash of hard-coded configurations, indexed by version number or name. Values can be accessed with any object that responds to to_sym or to_r/to_f with a non-zero number.
Config::[] gets named or numbered versions from this hash.
For example:
Net::IMAP::Config.version_defaults[0.5] == Net::IMAP::Config[0.5]
Net::IMAP::Config[0.5] == Net::IMAP::Config[0.5r] # => true
Net::IMAP::Config["current"] == Net::IMAP::Config[:current] # => true
Net::IMAP::Config["0.5.6"] == Net::IMAP::Config[0.5r] # => true
144 |
# File 'lib/net/imap/config.rb', line 144 def self.version_defaults; @version_defaults end |
Instance Method Details
#load_defaults(version) ⇒ Object
:call-seq: 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 Config.global) and are left unchanged, for example: #debug.
See Config@Versioned+defaults and Config@Named+defaults.
460 461 462 463 464 |
# File 'lib/net/imap/config.rb', line 460 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 |
#to_h ⇒ Object
:call-seq: to_h -> hash
Returns all config attributes in a hash.
469 |
# File 'lib/net/imap/config.rb', line 469 def to_h; data.members.to_h { [_1, send(_1)] } end |
#update(**attrs) ⇒ Object
:call-seq: 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,
attrsmay be partially applied.
425 426 427 428 429 430 431 |
# File 'lib/net/imap/config.rb', line 425 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) ⇒ Object
:call-seq:
with(**attrs) -> 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.
443 444 445 446 447 448 449 |
# File 'lib/net/imap/config.rb', line 443 def with(**attrs) attrs.empty? and raise ArgumentError, "expected keyword arguments, none given" copy = new(**attrs) copy.freeze if frozen? block_given? ? yield(copy) : copy end |