From ab2b1570fdfcb6fa148f30ccd6edcff6428b4d7a Mon Sep 17 00:00:00 2001 From: Daniel Schierbeck Date: Sun, 21 Sep 2008 14:56:02 +0200 Subject: Made the :prefix option on Module#delegate accept a custom prefix. Signed-off-by: Michael Koziarski --- activesupport/lib/active_support/core_ext/module/delegation.rb | 2 +- activesupport/test/core_ext/module_test.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/core_ext/module/delegation.rb b/activesupport/lib/active_support/core_ext/module/delegation.rb index fee4454cb2..bd3afa1961 100644 --- a/activesupport/lib/active_support/core_ext/module/delegation.rb +++ b/activesupport/lib/active_support/core_ext/module/delegation.rb @@ -53,7 +53,7 @@ class Module 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}_" : "" + prefix = options[:prefix] && (options[:prefix] == true ? "#{to}_" : "#{options[:prefix]}_") methods.each do |method| module_eval(<<-EOS, "(__DELEGATION__)", 1) diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb index 071eefcb29..46d63748bc 100644 --- a/activesupport/test/core_ext/module_test.rb +++ b/activesupport/test/core_ext/module_test.rb @@ -38,6 +38,7 @@ end Invoice = Struct.new(:client) do delegate :street, :city, :name, :to => :client, :prefix => true + delegate :street, :city, :name, :to => :client, :prefix => :customer end class Name @@ -97,6 +98,14 @@ class ModuleTest < Test::Unit::TestCase assert_equal invoice.client_city, "Chicago" end + def test_delegation_custom_prefix + david = Someone.new("David", Somewhere.new("Paulina", "Chicago")) + invoice = Invoice.new(david) + assert_equal invoice.customer_name, "David" + assert_equal invoice.customer_street, "Paulina" + assert_equal invoice.customer_city, "Chicago" + end + def test_parent assert_equal Yz::Zy, Yz::Zy::Cd.parent assert_equal Yz, Yz::Zy.parent -- cgit v1.2.3