mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Just do the method call directly in Module#delegate, if we can (we cannot for method names ending in '='). Two reasons: 1) it's faster, see https://gist.github.com/1089783 and 2) more importantly, delegate should not allow you to accidentally call private or protected methods.
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
*Rails 3.2.0 (unreleased)*
|
||||
|
||||
* Removed support for using Module#delegate to delegate to non-public methods [Jon Leighton]
|
||||
|
||||
* The definition of blank string for Ruby 1.9 has been extended to Unicode whitespace.
|
||||
Also, in 1.8 the ideographic space U+3000 is considered to be whitespace. [Akira Matsuda, Damien Mathieu]
|
||||
|
||||
* The inflector understands acronyms. [dlee]
|
||||
|
||||
* Deprecated ActiveSupport::Memoizable in favor of Ruby memoization pattern [José Valim]
|
||||
* Deprecated ActiveSupport::Memoizable in favor of Ruby memoization pattern [José Valim]
|
||||
|
||||
* Added Time#all_day/week/quarter/year as a way of generating ranges (example: Event.where(created_at: Time.now.all_week)) [DHH]
|
||||
|
||||
|
||||
@@ -123,6 +123,9 @@ class Module
|
||||
line = line.to_i
|
||||
|
||||
methods.each do |method|
|
||||
method = method.to_s
|
||||
call = (method[-1..-1] == '=') ? "public_send(:#{method}, " : "#{method}("
|
||||
|
||||
on_nil =
|
||||
if allow_nil
|
||||
'return'
|
||||
@@ -132,7 +135,7 @@ class Module
|
||||
|
||||
module_eval(<<-EOS, file, line - 5)
|
||||
def #{method_prefix}#{method}(*args, &block) # def customer_name(*args, &block)
|
||||
#{to}.__send__(#{method.inspect}, *args, &block) # client.__send__(:name, *args, &block)
|
||||
#{to}.#{call}*args, &block) # client.name(*args, &block)
|
||||
rescue NoMethodError # rescue NoMethodError
|
||||
if #{to}.nil? # if client.nil?
|
||||
#{on_nil} # return # depends on :allow_nil
|
||||
|
||||
@@ -26,10 +26,20 @@ module Yz
|
||||
end
|
||||
end
|
||||
|
||||
Somewhere = Struct.new(:street, :city)
|
||||
Somewhere = Struct.new(:street, :city) do
|
||||
protected
|
||||
|
||||
def protected_method
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def private_method
|
||||
end
|
||||
end
|
||||
|
||||
Someone = Struct.new(:name, :place) do
|
||||
delegate :street, :city, :to_f, :to => :place
|
||||
delegate :street, :city, :to_f, :protected_method, :private_method, :to => :place
|
||||
delegate :upcase, :to => "place.city"
|
||||
end
|
||||
|
||||
@@ -69,6 +79,14 @@ class ModuleTest < Test::Unit::TestCase
|
||||
assert_equal "Chicago", @david.city
|
||||
end
|
||||
|
||||
def test_delegation_to_protected_method
|
||||
assert_raise(NoMethodError) { @david.protected_method }
|
||||
end
|
||||
|
||||
def test_delegation_to_private_method
|
||||
assert_raise(NoMethodError) { @david.private_method }
|
||||
end
|
||||
|
||||
def test_delegation_down_hierarchy
|
||||
assert_equal "CHICAGO", @david.upcase
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user