From 0147f1d4c4f8ac5987e99bc9c36cd09fcbf1f259 Mon Sep 17 00:00:00 2001 From: Daniel Schierbeck Date: Tue, 23 Sep 2008 22:42:36 +0200 Subject: [PATCH] Made sure only delegations to methods can have an automatic prefix. --- .../active_support/core_ext/module/delegation.rb | 4 ++++ activesupport/test/core_ext/module_test.rb | 11 +++++++++++ 2 files changed, 15 insertions(+), 0 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/module/delegation.rb b/activesupport/lib/active_support/core_ext/module/delegation.rb index 1ad1840..2905eeb 100644 --- a/activesupport/lib/active_support/core_ext/module/delegation.rb +++ b/activesupport/lib/active_support/core_ext/module/delegation.rb @@ -78,6 +78,10 @@ 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 + if options[:prefix] == true && options[:to].to_s =~ /^[^a-z_]/ + raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method." + end + prefix = options[:prefix] && "#{options[:prefix] == true ? to : options[:prefix]}_" methods.each do |method| diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb index 7fe5d0f..886f692 100644 --- a/activesupport/test/core_ext/module_test.rb +++ b/activesupport/test/core_ext/module_test.rb @@ -106,6 +106,17 @@ class ModuleTest < Test::Unit::TestCase assert_equal invoice.customer_city, "Chicago" end + def test_delegation_prefix_with_instance_variable + assert_raise ArgumentError do + Class.new do + def initialize(client) + @client = client + end + delegate :name, :address, :to => :@client, :prefix => true + end + end + end + def test_parent assert_equal Yz::Zy, Yz::Zy::Cd.parent assert_equal Yz, Yz::Zy.parent -- 1.5.4.3