Created
September 17, 2011 22:19
-
-
Save jaredonline/1224439 to your computer and use it in GitHub Desktop.
Method deprecation helper
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
module MethodDeprecator | |
def self.included(base) | |
base.extend(ClassMethods) | |
end | |
module ClassMethods | |
def deprecate_method(method, replacement) | |
class_eval <<-DEPRECATE_METHOD_END | |
def #{method.to_s}_with_deprecation | |
puts "WARNING: Deprecated method #{self.class}##{method.to_s} called, use #{replacement} instead. Called from #{Kernel.caller[0]}" | |
#{method.to_s}_without_deprecation | |
end | |
alias_method_chain :#{method.to_s}, :deprecation | |
DEPRECATE_METHOD_END | |
end | |
end | |
end |
I like the idea of making it a core extension. The original question was (not sure why it was deleted):
Because this method uses alias_method_chain
the results from Kernerl.caller[0]
are always the original method. For example, if you have a method foo
and you deprecate it with this helper, and you call foo
then foo
ends up calling foo_with_deprecation
, so Kernel.caller[0]
returns foo
which doesn't help you pinpoint the deprecation. Ideas?
Kernel.caller
is the whole execution stack, isn't it? So you can use Kernel.caller[1]
instead to get one level up.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hmm... one other idea: should we instead make this into a core extension for
Module
? This way you won't need to explicitlyinclude MethodDeprecator
into the class (which is extra clutter).