Skip to content

Commit

Permalink
Merge commit 'rails/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
miloops committed Aug 10, 2009
2 parents 10af9fa + d15ddf0 commit 8c3a6f8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
13 changes: 9 additions & 4 deletions activesupport/lib/active_support/core_ext/module/delegation.rb
Expand Up @@ -120,10 +120,15 @@ def delegate(*methods)
end end


module_eval(<<-EOS, file, line) module_eval(<<-EOS, file, line)
def #{prefix}#{method}(*args, &block) # def customer_name(*args, &block) def #{prefix}#{method}(*args, &block) # def customer_name(*args, &block)
#{on_nil} if #{to}.nil? #{to}.__send__(#{method.inspect}, *args, &block) # client.__send__(:name, *args, &block)
#{to}.__send__(#{method.inspect}, *args, &block) # client && client.__send__(:name, *args, &block) rescue NoMethodError # rescue NoMethodError
end # end if #{to}.nil? # if client.nil?
#{on_nil}
else # else
raise # raise
end # end
end # end
EOS EOS
end end
end end
Expand Down
13 changes: 12 additions & 1 deletion activesupport/test/core_ext/module_test.rb
Expand Up @@ -32,7 +32,7 @@ class De
Somewhere = Struct.new(:street, :city) Somewhere = Struct.new(:street, :city)


Someone = Struct.new(:name, :place) do Someone = Struct.new(:name, :place) do
delegate :street, :city, :to => :place delegate :street, :city, :to_f, :to => :place
delegate :state, :to => :@place delegate :state, :to => :@place
delegate :upcase, :to => "place.city" delegate :upcase, :to => "place.city"
end end
Expand All @@ -44,6 +44,7 @@ class De


Project = Struct.new(:description, :person) do Project = Struct.new(:description, :person) do
delegate :name, :to => :person, :allow_nil => true delegate :name, :to => :person, :allow_nil => true
delegate :to_f, :to => :description, :allow_nil => true
end end


class Name class Name
Expand Down Expand Up @@ -145,6 +146,16 @@ def test_delegation_without_allow_nil_and_nil_value
assert_raise(RuntimeError) { david.street } assert_raise(RuntimeError) { david.street }
end end


def test_delegation_to_method_that_exists_on_nil
nil_person = Someone.new(nil)
assert_equal 0.0, nil_person.to_f
end

def test_delegation_to_method_that_exists_on_nil_when_allowing_nil
nil_project = Project.new(nil)
assert_equal 0.0, nil_project.to_f
end

def test_parent def test_parent
assert_equal Yz::Zy, Yz::Zy::Cd.parent assert_equal Yz::Zy, Yz::Zy::Cd.parent
assert_equal Yz, Yz::Zy.parent assert_equal Yz, Yz::Zy.parent
Expand Down

0 comments on commit 8c3a6f8

Please sign in to comment.