Module#deprecated_method_alias (#101)

* Add Module#deprecated_method_alias
* Use deprecated_method_alias in 010_Pokemon_Deprecated.rb
* Use deprecated_method_alias on the rest of Pokemon methods
Also, use deprecate_constant on MAX_POKEMON_NAME_SIZE
* Use deprecated_method_alias on PlayerTrainer methods
* Ensure aliased method exists in deprecated_method_alias
This commit is contained in:
Joni Savolainen
2021-03-02 22:16:22 +02:00
committed by GitHub
parent 8b0796588f
commit 47b164f0ab
3 changed files with 71 additions and 88 deletions

View File

@@ -5,8 +5,8 @@ module Deprecation
# Sends a warning of a deprecated method into the debug console.
# @param method_name [String] name of the deprecated method
# @param removal_version [String] name of the version the method is removed in (optional)
# @param alternative [String] preferred alternative method (optional)
# @param removal_version [String] version the method is removed in
# @param alternative [String] preferred alternative method
def warn_method(method_name, removal_version = nil, alternative = nil)
text = _INTL('WARN: usage of deprecated method "{1}" or its alias.', method_name)
unless removal_version.nil?
@@ -24,29 +24,30 @@ end
class Module
private
# Creates a deprecated alias for an instance method.
# Creates a deprecated alias for a method.
# Using it sends a warning to the debug console.
# @param alias_name [Symbol] the name of the new alias
# @param aliased_method_name [Symbol] the name of the aliased method
# @param removal_version [String] name of the version the alias is removed in (optional)
def deprecated_instance_method_alias(alias_name, aliased_method_name, removal_version = nil)
define_method(alias_name) do |*args|
alias_full_name = format('%s#%s', self.class.name, alias_name.to_s)
aliased_method_full_name = format('%s#%s', self.class.name, aliased_method_name.to_s)
Deprecation.warn_method(alias_full_name, removal_version, aliased_method_full_name)
method(aliased_method_name).call(*args)
end
end
# @param name [Symbol] name of the new alias
# @param aliased_method [Symbol] name of the aliased method
# @param removal_in [String] version the alias is removed in
# @param class_method [Boolean] whether the method is a class method
def deprecated_method_alias(name, aliased_method, removal_in: nil, class_method: false)
validate name => Symbol, aliased_method => Symbol, removal_in => [NilClass, String],
class_method => [TrueClass, FalseClass]
# Creates a deprecated alias for a class method.
# Using it sends a warning to the debug console.
# @param (see #deprecated_instance_method_alias)
def deprecated_class_method_alias(alias_name, aliased_method_name, removal_version = nil)
self.class.send(:define_method, alias_name) do |*args|
alias_full_name = format('%s::%s', self.name, alias_name.to_s)
aliased_method_full_name = format('%s::%s', self.name, aliased_method_name.to_s)
Deprecation.warn_method(alias_full_name, removal_version, aliased_method_full_name)
method(aliased_method_name).call(*args)
target = class_method ? self.class : self
class_name = self.name
unless target.method_defined?(aliased_method)
raise ArgumentError, "#{class_name} does not have method #{aliased_method} defined"
end
delimiter = class_method ? '.' : '#'
target.define_method(name) do |*args, **kvargs|
alias_name = format('%s%s%s', class_name, delimiter, name)
aliased_method_name = format('%s%s%s', class_name, delimiter, aliased_method)
Deprecation.warn_method(alias_name, removal_in, aliased_method_name)
method(aliased_method).call(*args, **kvargs)
end
end
end