Skip to content

Commit ad5cbfc

Browse files
committed
♻️ Simplify lazy-loaded SASL::{Name}Authenticator
In addition to selecting the constant based on the name, this also lazy loads the authenticator definitions. Most authenticators are never used, so this avoids the cost of loading them all.
1 parent 65e5cd6 commit ad5cbfc

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

lib/net/imap/sasl/authenticators.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ class Authenticators
3333
def initialize(use_defaults: false)
3434
@authenticators = {}
3535
if use_defaults
36-
add_authenticator "Plain", PlainAuthenticator
37-
add_authenticator "XOAuth2", XOAuth2Authenticator
38-
add_authenticator "Login", LoginAuthenticator # deprecated
39-
add_authenticator "Cram-MD5", CramMD5Authenticator # deprecated
40-
add_authenticator "Digest-MD5", DigestMD5Authenticator # deprecated
36+
add_authenticator "Plain"
37+
add_authenticator "XOAuth2"
38+
add_authenticator "Login" # deprecated
39+
add_authenticator "Cram-MD5" # deprecated
40+
add_authenticator "Digest-MD5" # deprecated
4141
end
4242
end
4343

@@ -60,8 +60,16 @@ def names; @authenticators.keys end
6060
# When only a single argument is given, the authenticator class will be
6161
# lazily loaded from <tt>Net::IMAP::SASL::#{name}Authenticator</tt> (case is
6262
# preserved and non-alphanumeric characters are removed..
63-
def add_authenticator(name, authenticator)
63+
def add_authenticator(name, authenticator = nil)
6464
key = name.upcase.to_sym
65+
authenticator ||= begin
66+
class_name = "#{name.gsub(/[^a-zA-Z0-9]/, "")}Authenticator".to_sym
67+
auth_class = nil
68+
->(*creds, **props, &block) {
69+
auth_class ||= Net::IMAP::SASL.const_get(class_name)
70+
auth_class.new(*creds, **props, &block)
71+
}
72+
end
6573
@authenticators[key] = authenticator
6674
end
6775

0 commit comments

Comments
 (0)