Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added :prefix option to Module#delegate.

Signed-off-by: Michael Koziarski <michael@koziarski.com>
  • Loading branch information...
commit 731c63f8ebc93e703b1a114d86f4f9f694ae48bf 1 parent 6d1d48d
@dasch dasch authored NZKoz committed
View
4 activesupport/lib/active_support/core_ext/module/delegation.rb
@@ -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
View
12 activesupport/test/core_ext/module_test.rb
@@ -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
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.