Skip to content

Commit

Permalink
Added :prefix option to Module#delegate.
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Koziarski <michael@koziarski.com>
  • Loading branch information
dasch authored and NZKoz committed Oct 19, 2008
1 parent 6d1d48d commit 731c63f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Expand Up @@ -53,9 +53,11 @@ def delegate(*methods)
raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)."
end

prefix = options[:prefix] ? "#{to}_" : ""

methods.each do |method|
module_eval(<<-EOS, "(__DELEGATION__)", 1)
def #{method}(*args, &block)
def #{prefix}#{method}(*args, &block)
#{to}.__send__(#{method.inspect}, *args, &block)
end
EOS
Expand Down
12 changes: 12 additions & 0 deletions activesupport/test/core_ext/module_test.rb
Expand Up @@ -36,6 +36,10 @@ class De
delegate :upcase, :to => "place.city"
end

Invoice = Struct.new(:client) do
delegate :street, :city, :name, :to => :client, :prefix => true
end

class Name
delegate :upcase, :to => :@full_name

Expand Down Expand Up @@ -85,6 +89,14 @@ def test_missing_delegation_target
assert_raises(ArgumentError) { eval($noplace) }
end

def test_delegation_prefix
david = Someone.new("David", Somewhere.new("Paulina", "Chicago"))
invoice = Invoice.new(david)
assert_equal invoice.client_name, "David"
assert_equal invoice.client_street, "Paulina"
assert_equal invoice.client_city, "Chicago"
end

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

0 comments on commit 731c63f

Please sign in to comment.