Skip to content

Commit

Permalink
feat: Warn when loading deprecated files
Browse files Browse the repository at this point in the history
  • Loading branch information
micke committed Aug 31, 2024
1 parent 9df0bf8 commit 46634df
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
8 changes: 5 additions & 3 deletions lib/helpers/deprecation_helper.rb
Original file line number Diff line number Diff line change
@@ -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
end
22 changes: 17 additions & 5 deletions lib/valid_email2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions lib/valid_email2/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down

0 comments on commit 46634df

Please sign in to comment.