Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 4666

Show
Ignore:
Timestamp:
08/05/06 06:33:28 (2 years ago)
Author:
bitsweat
Message:

DeprecatedInstanceVariableProxy stand-in for @request, @attributes, and friends.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/lib/active_support/deprecation.rb

    r4647 r4666  
    4949 
    5050    module Assertions 
    51       def assert_deprecated(regexp = nil, &block) 
     51      def assert_deprecated(match = nil, &block) 
    5252        last = with_last_message_tracking_deprecation_behavior(&block) 
    5353        assert last, "Expected a deprecation warning within the block but received none" 
    54         if regexp 
    55           assert_match regexp, last, "Deprecation warning didn't match #{regexp}: #{last}" 
    56         end 
     54        match = Regexp.new(match) unless match.is_a?(Regexp) 
     55        assert_match match, last, "Deprecation warning didn't match #{match}: #{last}" 
    5756      end 
    5857 
     
    7372        end 
    7473    end 
     74 
     75    # Stand-in for @request, @attributes, etc. 
     76    class DeprecatedInstanceVariableProxy 
     77      instance_methods.each { |m| undef_method m unless m =~ /^__/ } 
     78 
     79      def initialize(instance, method, var = "@#{method}") 
     80        @instance, @method, @var = instance, method, var 
     81        deprecation_warning :initialize, caller 
     82      end 
     83 
     84      private 
     85        def deprecation_warning(called, callstack) 
     86          ActiveSupport::Deprecation.warn("Using #{@var} directly is deprecated - call #{@method} instead.", callstack) 
     87        end 
     88 
     89        def method_missing(called, *args, &block) 
     90          deprecation_warning called, caller 
     91          @instance.__send__(@method).__send__(called, *args, &block) 
     92        end 
     93    end 
    7594  end 
    7695end 
  • trunk/activesupport/test/deprecation_test.rb

    r4647 r4666  
    22 
    33class Deprecatee 
     4  def initialize 
     5    @request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request) 
     6    @_request = 'there we go' 
     7  end 
     8  def request; @_request end 
     9  def old_request; @request end 
     10 
    411  def partially(foo = nil) 
    512    ActiveSupport::Deprecation.warn 'calling with foo=nil is out' if foo.nil? 
     
    5865    assert_deprecated(/foo=nil/) { @dtc.partially } 
    5966  end 
     67 
     68  def test_deprecated_instance_variable_proxy 
     69    assert_not_deprecated { @dtc.request.size } 
     70 
     71    assert_deprecated('Using @request directly is deprecated - call request instead.') do 
     72      assert_equal @dtc.request.size, @dtc.old_request.size 
     73      assert_equal @dtc.request.to_s, @dtc.old_request.to_s 
     74    end 
     75  end 
    6076end