Created
January 1, 2011 04:12
-
-
Save seeflanigan/761546 to your computer and use it in GitHub Desktop.
A helper method to simplify deprecations.
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 DeprecationHelpers | |
def deprecated_in_favor_of(new_method_name) | |
warning = "[DEPRECATION] '##{deprecated_method_name}' " | |
warning << "has been deprecated.\n" | |
warning << "Please use '##{new_method_name.to_s}' instead." | |
logger.warn(warning) | |
end | |
protected | |
def deprecated_method_name | |
/`(.*)'/.match(caller[1]).captures[0].to_sym rescue nil | |
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
require File.dirname(__FILE__) + '/../../test_helper' | |
require 'deprecation_helpers' | |
class DeprecationHelpersTest < ActiveSupport::TestCase | |
include DeprecationHelpers | |
context "#deprecated_in_favor_of" do | |
setup do | |
def foo | |
deprecated_in_favor_of(:bar) | |
end | |
end | |
should "log a deprecation warning" do | |
logger.expects(:warn).with(includes("[DEPRECATION]")) | |
foo | |
end | |
should "include the name of the deprecated method" do | |
logger.expects(:warn).with(includes("foo")) | |
foo | |
end | |
should "include the name of the new method if one exists" do | |
logger.expects(:warn).with(includes("bar")) | |
foo | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment