mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
DeprecatedInstanceVariableProxy stand-in for @request, @attributes, and friends.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4666 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
6c50597489
commit
0e73a92c04
2 changed files with 39 additions and 4 deletions
|
@ -48,12 +48,11 @@ module ActiveSupport
|
|||
end
|
||||
|
||||
module Assertions
|
||||
def assert_deprecated(regexp = nil, &block)
|
||||
def assert_deprecated(match = nil, &block)
|
||||
last = with_last_message_tracking_deprecation_behavior(&block)
|
||||
assert last, "Expected a deprecation warning within the block but received none"
|
||||
if regexp
|
||||
assert_match regexp, last, "Deprecation warning didn't match #{regexp}: #{last}"
|
||||
end
|
||||
match = Regexp.new(match) unless match.is_a?(Regexp)
|
||||
assert_match match, last, "Deprecation warning didn't match #{match}: #{last}"
|
||||
end
|
||||
|
||||
def assert_not_deprecated(&block)
|
||||
|
@ -72,6 +71,26 @@ module ActiveSupport
|
|||
ActiveSupport::Deprecation.behavior = old_behavior
|
||||
end
|
||||
end
|
||||
|
||||
# Stand-in for @request, @attributes, etc.
|
||||
class DeprecatedInstanceVariableProxy
|
||||
instance_methods.each { |m| undef_method m unless m =~ /^__/ }
|
||||
|
||||
def initialize(instance, method, var = "@#{method}")
|
||||
@instance, @method, @var = instance, method, var
|
||||
deprecation_warning :initialize, caller
|
||||
end
|
||||
|
||||
private
|
||||
def deprecation_warning(called, callstack)
|
||||
ActiveSupport::Deprecation.warn("Using #{@var} directly is deprecated - call #{@method} instead.", callstack)
|
||||
end
|
||||
|
||||
def method_missing(called, *args, &block)
|
||||
deprecation_warning called, caller
|
||||
@instance.__send__(@method).__send__(called, *args, &block)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,6 +1,13 @@
|
|||
require File.dirname(__FILE__) + '/abstract_unit'
|
||||
|
||||
class Deprecatee
|
||||
def initialize
|
||||
@request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request)
|
||||
@_request = 'there we go'
|
||||
end
|
||||
def request; @_request end
|
||||
def old_request; @request end
|
||||
|
||||
def partially(foo = nil)
|
||||
ActiveSupport::Deprecation.warn 'calling with foo=nil is out' if foo.nil?
|
||||
end
|
||||
|
@ -57,4 +64,13 @@ class DeprecationTest < Test::Unit::TestCase
|
|||
ActiveSupport::Deprecation.behavior = nil
|
||||
assert_deprecated(/foo=nil/) { @dtc.partially }
|
||||
end
|
||||
|
||||
def test_deprecated_instance_variable_proxy
|
||||
assert_not_deprecated { @dtc.request.size }
|
||||
|
||||
assert_deprecated('Using @request directly is deprecated - call request instead.') do
|
||||
assert_equal @dtc.request.size, @dtc.old_request.size
|
||||
assert_equal @dtc.request.to_s, @dtc.old_request.to_s
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue