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

Changeset 5356

Show
Ignore:
Timestamp:
10/24/06 16:56:59 (2 years ago)
Author:
minam
Message:

Extend deprecate so that alternatives can be specified via the deprecation

Files:

Legend:

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

    r5161 r5356  
    7272      # Declare that a method has been deprecated. 
    7373      def deprecate(*method_names) 
     74        options = method_names.last.is_a?(Hash) ? method_names.pop : {} 
     75        method_names = method_names + options.keys 
    7476        method_names.each do |method_name| 
    7577          alias_method_chain(method_name, :deprecation) do |target, punctuation| 
    7678            class_eval(<<-EOS, __FILE__, __LINE__) 
    7779              def #{target}_with_deprecation#{punctuation}(*args, &block) 
    78                 ::ActiveSupport::Deprecation.warn(self.class.deprecated_method_warning(:#{method_name}), caller) 
     80                ::ActiveSupport::Deprecation.warn(self.class.deprecated_method_warning(:#{method_name}, #{options[method_name].inspect}), caller) 
    7981                #{target}_without_deprecation#{punctuation}(*args, &block) 
    8082              end 
     
    8486      end 
    8587 
    86       def deprecated_method_warning(method_name) 
    87         "#{method_name} is deprecated and will be removed from Rails #{deprecation_horizon}" 
     88      def deprecated_method_warning(method_name, message=nil) 
     89        warning = "#{method_name} is deprecated and will be removed from Rails #{deprecation_horizon}" 
     90        case message 
     91          when Symbol then "#{warning} (use #{message} instead)" 
     92          when String then "#{warning} (#{message})" 
     93          else warning 
     94        end 
    8895      end 
    8996 
  • trunk/activesupport/test/deprecation_test.rb

    r5115 r5356  
    1818  def multi(a,b,c) [a,b,c] end 
    1919  deprecate :none, :one, :multi 
     20 
     21  def a; end 
     22  def b; end 
     23  def c; end 
     24  def d; end 
     25  def e; end 
     26  deprecate :a, :b, :c => :e, :d => "you now need to do something extra for this one" 
    2027end 
    2128 
     
    109116    ActiveSupport::Deprecation.silenced = false 
    110117  end 
     118 
     119  def test_deprecation_without_explanation 
     120    assert_deprecated { @dtc.a } 
     121    assert_deprecated { @dtc.b } 
     122  end 
     123 
     124  def test_deprecation_with_alternate_method 
     125    assert_deprecated(/use e instead/) { @dtc.c } 
     126  end 
     127 
     128  def test_deprecation_with_explicit_message 
     129    assert_deprecated(/you now need to do something extra for this one/) { @dtc.d } 
     130  end 
    111131end