Changeset 4429
- Timestamp:
- 06/03/06 22:43:13 (2 years ago)
- Files:
-
- trunk/activesupport/CHANGELOG (modified) (1 diff)
- trunk/activesupport/lib/active_support/core_ext/module/aliasing.rb (modified) (1 diff)
- trunk/activesupport/test/core_ext/module_test.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activesupport/CHANGELOG
r4413 r4429 1 1 *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] 2 4 3 5 * 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 10 10 # 11 11 # 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. 12 19 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 15 21 aliased_target = target.to_s.sub(/[?!]/, '') 16 alias_method "#{aliased_target}_without_#{feature} ", target17 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}" 18 24 end 19 25 end trunk/activesupport/test/core_ext/module_test.rb
r4386 r4429 110 110 end 111 111 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 114 118 end 115 119 end … … 149 153 def test_alias_method_chain_with_punctuation_method 150 154 FooClassWithBarMethod.send(:define_method, 'quux!', Proc.new { 'quux' }) 151 assert !@instance.respond_to?(:quux_with_baz )155 assert !@instance.respond_to?(:quux_with_baz!) 152 156 FooClassWithBarMethod.send(:include, BarMethodAliaser) 153 157 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 155 163 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? 158 180 end 159 181 end