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

View File

@@ -2,39 +2,39 @@
# Deprecated # Deprecated
#=============================================================================== #===============================================================================
class PlayerTrainer class PlayerTrainer
alias fullname full_name deprecated_method_alias :fullname, :full_name, removal_in: 'v20'
alias publicID public_ID deprecated_method_alias :publicID, :public_ID, removal_in: 'v20'
alias secretID secret_ID deprecated_method_alias :secretID, :secret_ID, removal_in: 'v20'
alias getForeignID make_foreign_ID deprecated_method_alias :getForeignID, :make_foreign_ID, removal_in: 'v20'
alias trainerTypeName trainer_type_name deprecated_method_alias :trainerTypeName, :trainer_type_name, removal_in: 'v20'
alias moneyEarned base_money deprecated_method_alias :moneyEarned, :base_money, removal_in: 'v20'
alias skill skill_level deprecated_method_alias :skill, :skill_level, removal_in: 'v20'
alias skillCode skill_code deprecated_method_alias :skillCode, :skill_code, removal_in: 'v20'
alias hasSkillCode has_skill_code? deprecated_method_alias :hasSkillCode, :has_skill_code?, removal_in: 'v20'
alias pokemonParty pokemon_party deprecated_method_alias :pokemonParty, :pokemon_party, removal_in: 'v20'
alias ablePokemonParty able_party deprecated_method_alias :ablePokemonParty, :able_party, removal_in: 'v20'
alias partyCount party_count deprecated_method_alias :partyCount, :party_count, removal_in: 'v20'
alias pokemonCount pokemon_count deprecated_method_alias :pokemonCount, :pokemon_count, removal_in: 'v20'
alias ablePokemonCount able_pokemon_count deprecated_method_alias :ablePokemonCount, :able_pokemon_count, removal_in: 'v20'
alias firstParty first_party deprecated_method_alias :firstParty, :first_party, removal_in: 'v20'
alias firstPokemon first_pokemon deprecated_method_alias :firstPokemon, :first_pokemon, removal_in: 'v20'
alias firstAblePokemon first_able_pokemon deprecated_method_alias :firstAblePokemon, :first_able_pokemon, removal_in: 'v20'
alias lastParty last_party deprecated_method_alias :lastParty, :last_party, removal_in: 'v20'
alias lastPokemon last_pokemon deprecated_method_alias :lastPokemon, :last_pokemon, removal_in: 'v20'
alias lastAblePokemon last_able_pokemon deprecated_method_alias :lastAblePokemon, :last_able_pokemon, removal_in: 'v20'
alias formseen seen_forms deprecated_method_alias :formseen, :seen_forms, removal_in: 'v20'
alias formlastseen last_seen_forms deprecated_method_alias :formlastseen, :last_seen_forms, removal_in: 'v20'
alias shadowcaught owned_shadow deprecated_method_alias :shadowcaught, :owned_shadow, removal_in: 'v20'
alias numbadges badge_count deprecated_method_alias :numbadges, :badge_count, removal_in: 'v20'
alias pokedexSeen seen_count deprecated_method_alias :pokedexSeen, :seen_count, removal_in: 'v20'
alias pokedexOwned owned_count deprecated_method_alias :pokedexOwned, :owned_count, removal_in: 'v20'
alias numFormsSeen seen_forms_count deprecated_method_alias :numFormsSeen, :seen_forms_count, removal_in: 'v20'
alias clearPokedex clear_pokedex deprecated_method_alias :clearPokedex, :clear_pokedex, removal_in: 'v20'
alias metaID character_ID deprecated_method_alias :metaID, :character_ID, removal_in: 'v20'
alias mysterygiftaccess mystery_gift_unlocked deprecated_method_alias :mysterygiftaccess, :mystery_gift_unlocked, removal_in: 'v20'
alias mysterygift mystery_gifts deprecated_method_alias :mysterygift, :mystery_gifts, removal_in: 'v20'
alias setSeen set_seen deprecated_method_alias :setSeen, :set_seen, removal_in: 'v20'
alias setOwned set_owned deprecated_method_alias :setOwned, :set_owned, removal_in: 'v20'
end end
class PokeBattle_Trainer class PokeBattle_Trainer

View File

@@ -96,6 +96,7 @@ end
class Pokemon class Pokemon
# @deprecated Use {MAX_NAME_SIZE} instead. This alias is slated to be removed in v20. # @deprecated Use {MAX_NAME_SIZE} instead. This alias is slated to be removed in v20.
MAX_POKEMON_NAME_SIZE = MAX_NAME_SIZE MAX_POKEMON_NAME_SIZE = MAX_NAME_SIZE
deprecate_constant :MAX_POKEMON_NAME_SIZE
# @deprecated Use {Owner#public_id} instead. This alias is slated to be removed in v20. # @deprecated Use {Owner#public_id} instead. This alias is slated to be removed in v20.
def publicID def publicID
@@ -151,12 +152,6 @@ class Pokemon
@owner.language = value @owner.language = value
end end
# @deprecated Use {Pokemon#gender=} instead. This alias is slated to be removed in v20.
def setGender(value)
Deprecation.warn_method('Pokemon#setGender', 'v20', 'Pokemon#gender=')
self.gender = value
end
# @deprecated Use {Pokemon#shiny=} instead. This alias is slated to be removed in v20. # @deprecated Use {Pokemon#shiny=} instead. This alias is slated to be removed in v20.
def makeShiny def makeShiny
Deprecation.warn_method('Pokemon#makeShiny', 'v20', 'Pokemon#shiny=true') Deprecation.warn_method('Pokemon#makeShiny', 'v20', 'Pokemon#shiny=true')
@@ -169,33 +164,20 @@ class Pokemon
self.shiny = false self.shiny = false
end end
# @deprecated Use {Pokemon#ability_index=} instead. This alias is slated to be removed in v20. deprecated_method_alias :setGender, :gender=, removal_in: 'v20'
def setAbility(value) deprecated_method_alias :setAbility, :ability_index=, removal_in: 'v20'
Deprecation.warn_method('Pokemon#setAbility', 'v20', 'Pokemon#ability_index=') deprecated_method_alias :setNature, :nature=, removal_in: 'v20'
self.ability_index = value deprecated_method_alias :setItem, :item=, removal_in: 'v20'
end
# @deprecated Use {Pokemon#nature=} instead. This alias is slated to be removed in v20. deprecated_method_alias :healStatus, :heal_status, removal_in: 'v20'
def setNature(value) deprecated_method_alias :pbLearnMove, :learn_move, removal_in: 'v20'
Deprecation.warn_method('Pokemon#setNature', 'v20', 'Pokemon#nature=') deprecated_method_alias :pbDeleteMove, :forget_move, removal_in: 'v20'
self.nature = value deprecated_method_alias :pbDeleteMoveAtIndex, :forget_move_at_index, removal_in: 'v20'
end deprecated_method_alias :pbRecordFirstMoves, :record_first_moves, removal_in: 'v20'
deprecated_method_alias :pbAddFirstMove, :add_first_move, removal_in: 'v20'
# @deprecated Use {Pokemon#item=} instead. This alias is slated to be removed in v20. deprecated_method_alias :pbRemoveFirstMove, :remove_first_move, removal_in: 'v20'
def setItem(value) deprecated_method_alias :pbClearFirstMoves, :clear_first_moves, removal_in: 'v20'
Deprecation.warn_method('Pokemon#setItem', 'v20', 'Pokemon#item=') deprecated_method_alias :pbUpdateShadowMoves, :update_shadow_moves, removal_in: 'v20'
self.item = value
end
alias healStatus heal_status
alias pbLearnMove learn_move
alias pbDeleteMove forget_move
alias pbDeleteMoveAtIndex forget_move_at_index
alias pbRecordFirstMoves record_first_moves
alias pbAddFirstMove add_first_move
alias pbRemoveFirstMove remove_first_move
alias pbClearFirstMoves clear_first_moves
alias pbUpdateShadowMoves update_shadow_moves
end end
# (see Pokemon#initialize) # (see Pokemon#initialize)