Last active
December 21, 2017 05:33
-
-
Save a2ikm/202d46a2bcff2faab100914a2faf509b to your computer and use it in GitHub Desktop.
Log deprecated methods in Rails app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DEPRECATE_LOGGER = Logger.new(Rails.root.join("log", "deprecate.log").to_s).tap do |logger| | |
logger.formatter = proc { |s, time, p, msg| "[#{time}] #{msg}\n" } | |
end | |
class Module | |
def __deprecate(meth) | |
aliased, punc = meth.to_s.sub(/([?!=])$/, ""), $1 | |
module_eval <<-RUBY, __FILE__, __LINE__ + 1 | |
def #{aliased}_with_deprecated#{punc}(*args, &block) | |
DEPRECATE_LOGGER.debug { "\#{self.class.name}##{meth} was called from \#{caller(1, 1).first}." } | |
__send__(:#{aliased}_without_deprecated#{punc}, *args, &block) | |
end | |
alias_method_chain :#{meth}, :deprecated | |
RUBY | |
end | |
def __deprecate_accessor(meth) | |
__deprecate(meth) | |
__deprecate("#{meth}=") | |
end | |
def __deprecate_reader(meth) | |
__deprecate(meth) | |
end | |
def __deprecate_writer(meth) | |
__deprecate("#{meth}=") | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__deprecate def old_method(a, b, &bkock) | |
: | |
end | |
attr_accessor :old_accessor | |
__deprecate_accessor :old_accessor | |
attr_reader :old_reader | |
__deprecate_reader :old_reader | |
attr_writer :old_writer | |
__deprecate_writer :old_writer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment