From 46634dff409f9a47f10684d629dc022b22daf362 Mon Sep 17 00:00:00 2001 From: Micke Lisinge Date: Sat, 31 Aug 2024 22:24:54 +0200 Subject: [PATCH] feat: Warn when loading deprecated files --- lib/helpers/deprecation_helper.rb | 8 +++++--- lib/valid_email2.rb | 22 +++++++++++++++++----- lib/valid_email2/address.rb | 2 -- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/helpers/deprecation_helper.rb b/lib/helpers/deprecation_helper.rb index a957870..85a419a 100644 --- a/lib/helpers/deprecation_helper.rb +++ b/lib/helpers/deprecation_helper.rb @@ -1,12 +1,14 @@ module DeprecationHelper def deprecate_method(old_method, new_method) define_method(old_method) do |*args, &block| - warn "Warning: `#{old_method}` is deprecated; use `#{new_method}` instead." + klass = is_a? Module + target = klass ? "#{self}." : "#{self.class}#" + warn "Warning: `#{target}#{old_method}` is deprecated and will be removed in version 6 of valid_email2; use `#{new_method}` instead." send(new_method, *args, &block) end end def deprecation_message(old_name, new_name) - warn "Warning: `#{old_name}` is deprecated; use `#{new_name}` instead." + warn "Warning: `#{old_name}` is deprecated and will be removed in version 6 of valid_email2; use `#{new_name}` instead." end -end \ No newline at end of file +end diff --git a/lib/valid_email2.rb b/lib/valid_email2.rb index d8b3dfe..90ed415 100644 --- a/lib/valid_email2.rb +++ b/lib/valid_email2.rb @@ -19,21 +19,33 @@ def disposable_emails end def deny_list - @deny_list ||= load_if_exists(DENY_LIST_FILE || BLACKLIST_FILE) + @deny_list ||= load_if_exists(DENY_LIST_FILE) || + load_deprecated_if_exists(BLACKLIST_FILE) || + Set.new end - alias_method :blacklist, :deny_list deprecate_method :blacklist, :deny_list def allow_list - @allow_list ||= load_if_exists(ALLOW_LIST_FILE || WHITELIST_FILE) + @allow_list ||= load_if_exists(ALLOW_LIST_FILE) || + load_deprecated_if_exists(WHITELIST_FILE) || + Set.new end - alias_method :whitelist, :allow_list deprecate_method :whitelist, :allow_list private def load_if_exists(path) - File.exist?(path) ? load_file(path) : Set.new + load_file(path) if File.exist?(path) + end + + def load_deprecated_if_exists(path) + if File.exist?(path) + warn <<~WARN + Warning: The file `#{path}` used by valid_email2 is deprecated and won't be read in version 6 of valid_email2; + Rename the file to `#{path.gsub("blacklisted", "deny_listed").gsub("whitelisted", "allow_listed")}` instead." + WARN + load_file(path) + end end def load_file(path) diff --git a/lib/valid_email2/address.rb b/lib/valid_email2/address.rb index 3d3ca24..3727f1d 100644 --- a/lib/valid_email2/address.rb +++ b/lib/valid_email2/address.rb @@ -90,13 +90,11 @@ def disposable_mx_server? def allow_listed? domain_is_in?(ValidEmail2.allow_list) end - alias_method :whitelisted?, :allow_listed? deprecate_method :whitelisted?, :allow_listed? def deny_listed? valid? && domain_is_in?(ValidEmail2.deny_list) end - alias_method :blacklisted?, :deny_listed? deprecate_method :blacklisted?, :deny_listed? def valid_mx?