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

Changeset 5113

Show
Ignore:
Timestamp:
09/15/06 03:42:54 (2 years ago)
Author:
bitsweat
Message:

alias_method_chain yields method target and punctuation to simplify wrapper method definition. Used by the deprecate module method.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/lib/active_support/core_ext/module/aliasing.rb

    r5091 r5113  
    2525    # e.g. target?_without_feature is not a valid method name. 
    2626    aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1 
     27    yield(aliased_target, punctuation) if block_given? 
    2728    alias_method "#{aliased_target}_without_#{feature}#{punctuation}", target 
    2829    alias_method target, "#{aliased_target}_with_#{feature}#{punctuation}" 
  • trunk/activesupport/lib/active_support/deprecation.rb

    r4982 r5113  
    7373      def deprecate(*method_names) 
    7474        method_names.each do |method_name| 
    75           class_eval(<<-EOS, __FILE__, __LINE__) 
    76             def #{method_name}_with_deprecation(*args, &block) 
    77               ::ActiveSupport::Deprecation.warn("#{method_name} is deprecated and will be removed from Rails 2.0", caller) 
    78               #{method_name}_without_deprecation(*args, &block) 
    79             end 
    80           EOS 
    81           alias_method_chain(method_name, :deprecation) 
     75          alias_method_chain(method_name, :deprecation) do |target, punctuation| 
     76            class_eval(<<-EOS, __FILE__, __LINE__) 
     77              def #{target}_with_deprecation#{punctuation}(*args, &block) 
     78                ::ActiveSupport::Deprecation.warn("#{method_name} is deprecated and will be removed from Rails 2.0", caller) 
     79                #{target}_without_deprecation#{punctuation}(*args, &block) 
     80              end 
     81            EOS 
     82          end 
    8283        end 
    8384      end 
  • trunk/activesupport/test/core_ext/module_test.rb

    r5091 r5113  
    101101module BarMethodAliaser 
    102102  def self.included(foo_class) 
     103    foo_class.send :include, BarMethods 
    103104    foo_class.alias_method_chain :bar, :baz 
    104105  end 
    105  
     106end 
     107 
     108module BarMethods 
    106109  def bar_with_baz 
    107110    bar_without_baz << '_with_baz' 
     
    204207    end 
    205208  end 
    206 end 
     209 
     210  def test_alias_method_chain_yields_target_and_punctuation 
     211    FooClassWithBarMethod.send(:define_method, :quux?, Proc.new { }) 
     212    FooClassWithBarMethod.send :include, BarMethods 
     213    block_called = false 
     214    FooClassWithBarMethod.alias_method_chain :quux?, :baz do |target, punctuation| 
     215      block_called = true 
     216      assert_equal 'quux', target 
     217      assert_equal '?', punctuation 
     218    end 
     219    assert block_called 
     220  end 
     221end