提交 0a7b1319 编写于 作者: J Jeremy Kemper

Merge pull request #7613 from marcandre/delegate_to_class

Nice and easy delegation to the class
......@@ -123,6 +123,9 @@ def delegate(*methods)
file, line = caller.first.split(':', 2)
line = line.to_i
to = to.to_s
to = 'self.class' if to == 'class'
methods.each do |method|
# Attribute writer methods only accept one argument. Makes sure []=
# methods still accept two arguments.
......
......@@ -34,6 +34,12 @@ class Someone < Struct.new(:name, :place)
delegate :street, :city, :to_f, :to => :place
delegate :name=, :to => :place, :prefix => true
delegate :upcase, :to => "place.city"
delegate :table_name, :to => :class
delegate :table_name, :to => :class, :prefix => true
def self.table_name
'some_table'
end
FAILED_DELEGATE_LINE = __LINE__ + 1
delegate :foo, :to => :place
......@@ -111,6 +117,11 @@ def test_delegation_to_instance_variable
assert_equal "DAVID HANSSON", david.upcase
end
def test_delegation_to_class_method
assert_equal 'some_table', @david.table_name
assert_equal 'some_table', @david.class_table_name
end
def test_missing_delegation_target
assert_raise(ArgumentError) do
Name.send :delegate, :nowhere
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册