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

Changeset 4429

Show
Ignore:
Timestamp:
06/03/06 22:43:13 (2 years ago)
Author:
david
Message:

Fixed that Module#alias_method_chain should work with both foo? foo! and foo at the same time (closes #4954) [anna@wota.jp]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r4413 r4429  
    11*SVN* 
     2 
     3* Fixed that Module#alias_method_chain should work with both foo? foo! and foo at the same time #4954 [anna@wota.jp] 
    24 
    35* to_xml fixes, features, and speedup: introduce :dasherize option that converts updated_at to updated-at if true (the existing default); binary columns get encoding="base64" attribute; nil values get nil="true" attribute to distinguish empty values; add type information for float columns; allow arbitrarily deep :include; include SQL type information as the type attribute.  #4989 [Blair Zajac <blair@orcaware.com>] 
  • trunk/activesupport/lib/active_support/core_ext/module/aliasing.rb

    r4311 r4429  
    1010  # 
    1111  # And both aliases are set up for you. 
     12  # 
     13  # A punctuation is moved to the end on predicates or bang methods. 
     14  # 
     15  #   alias_method_chain :foo?, :feature 
     16  #  
     17  # generates "foo_without_feature?" method for old one, 
     18  # and expects "foo_with_feature?" method for new one. 
    1219  def alias_method_chain(target, feature) 
    13     # Strip out punctuation on predicates or bang methods since 
    14     # e.g. target?_without_feature is not a valid method name. 
     20    punctuation    = target.to_s.scan(/[?!]/).first 
    1521    aliased_target = target.to_s.sub(/[?!]/, '') 
    16     alias_method "#{aliased_target}_without_#{feature}", target 
    17     alias_method target, "#{aliased_target}_with_#{feature}
     22    alias_method "#{aliased_target}_without_#{feature}#{punctuation}", target 
     23    alias_method target, "#{aliased_target}_with_#{feature}#{punctuation}
    1824  end 
    1925end 
  • trunk/activesupport/test/core_ext/module_test.rb

    r4386 r4429  
    110110  end 
    111111 
    112   def quux_with_baz 
    113     quux_without_baz << '_with_baz' 
     112  def quux_with_baz! 
     113    quux_without_baz! << '_with_baz!' 
     114  end 
     115 
     116  def quux_with_baz? 
     117    false 
    114118  end 
    115119end 
     
    149153  def test_alias_method_chain_with_punctuation_method 
    150154    FooClassWithBarMethod.send(:define_method, 'quux!', Proc.new { 'quux' }) 
    151     assert !@instance.respond_to?(:quux_with_baz
     155    assert !@instance.respond_to?(:quux_with_baz!
    152156    FooClassWithBarMethod.send(:include, BarMethodAliaser) 
    153157    FooClassWithBarMethod.alias_method_chain :quux!, :baz 
    154     assert @instance.respond_to?(:quux_with_baz) 
     158    assert @instance.respond_to?(:quux_with_baz!) 
     159     
     160    assert_equal 'quux_with_baz!', @instance.quux! 
     161    assert_equal 'quux', @instance.quux_without_baz! 
     162  end 
    155163 
    156     assert_equal 'quux_with_baz', @instance.quux! 
    157     assert_equal 'quux', @instance.quux_without_baz 
     164  def test_alias_method_chain_with_same_names_between_predicates_and_bang_methods 
     165    FooClassWithBarMethod.send(:define_method, 'quux!', Proc.new { 'quux' }) 
     166    FooClassWithBarMethod.send(:define_method, 'quux?', Proc.new { true }) 
     167    assert !@instance.respond_to?(:quux_with_baz!) 
     168    assert !@instance.respond_to?(:quux_with_baz?) 
     169   
     170    FooClassWithBarMethod.send(:include, BarMethodAliaser) 
     171    FooClassWithBarMethod.alias_method_chain :quux!, :baz 
     172    FooClassWithBarMethod.alias_method_chain :quux?, :baz 
     173   
     174    assert @instance.respond_to?(:quux_with_baz!) 
     175    assert @instance.respond_to?(:quux_with_baz?) 
     176    assert_equal 'quux_with_baz!', @instance.quux! 
     177    assert_equal 'quux', @instance.quux_without_baz! 
     178    assert_equal false, @instance.quux? 
     179    assert_equal true,  @instance.quux_without_baz? 
    158180  end 
    159181end