Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Made the :prefix option on Module#delegate accept a custom prefix.

Signed-off-by: Michael Koziarski <michael@koziarski.com>
  • Loading branch information...
commit ab2b1570fdfcb6fa148f30ccd6edcff6428b4d7a 1 parent 731c63f
Daniel Schierbeck dasch authored NZKoz committed
2  activesupport/lib/active_support/core_ext/module/delegation.rb
View
@@ -53,7 +53,7 @@ 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}_" : ""
+ prefix = options[:prefix] && (options[:prefix] == true ? "#{to}_" : "#{options[:prefix]}_")
methods.each do |method|
module_eval(<<-EOS, "(__DELEGATION__)", 1)
9 activesupport/test/core_ext/module_test.rb
View
@@ -38,6 +38,7 @@ class De
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 @@ def test_delegation_prefix
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
Please sign in to comment.
Something went wrong with that request. Please try again.