From a167a051b6ca17bad72c72686fada14ddef432bd Mon Sep 17 00:00:00 2001 From: Daniel Schierbeck Date: Sun, 14 Sep 2008 23:20:51 +0200 Subject: [PATCH] Added :prefix option to Module#delegate. --- .../active_support/core_ext/module/delegation.rb | 4 +++- activesupport/test/core_ext/module_test.rb | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/module/delegation.rb b/activesupport/lib/active_support/core_ext/module/delegation.rb index e0b5f3e..fee4454 100644 --- a/activesupport/lib/active_support/core_ext/module/delegation.rb +++ b/activesupport/lib/active_support/core_ext/module/delegation.rb @@ -53,9 +53,11 @@ 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}_" : "" + 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 diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb index ecdea38..071eefc 100644 --- a/activesupport/test/core_ext/module_test.rb +++ b/activesupport/test/core_ext/module_test.rb @@ -36,6 +36,10 @@ Someone = Struct.new(:name, :place) do 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 @@ class ModuleTest < Test::Unit::TestCase 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 -- 1.5.4.3 From 5fd47c30efba3a320570e7f0818a37477d696a1b Mon Sep 17 00:00:00 2001 From: Daniel Schierbeck Date: Sun, 21 Sep 2008 14:56:02 +0200 Subject: [PATCH] Made the :prefix option on Module#delegate accept a custom prefix. --- .../active_support/core_ext/module/delegation.rb | 2 +- activesupport/test/core_ext/module_test.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/module/delegation.rb b/activesupport/lib/active_support/core_ext/module/delegation.rb index fee4454..bd3afa1 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 071eefc..46d6374 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 -- 1.5.4.3 From 44486adaf7cdc33fdb5332ff41fa49caa139ca9f Mon Sep 17 00:00:00 2001 From: Daniel Schierbeck Date: Sun, 21 Sep 2008 14:59:31 +0200 Subject: [PATCH] Moved test object instantiation to a setup method. --- activesupport/test/core_ext/module_test.rb | 18 +++++++++--------- 1 files changed, 9 insertions(+), 9 deletions(-) diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb index 46d6374..7fe5d0f 100644 --- a/activesupport/test/core_ext/module_test.rb +++ b/activesupport/test/core_ext/module_test.rb @@ -62,6 +62,10 @@ end EOF class ModuleTest < Test::Unit::TestCase + def setup + @david = Someone.new("David", Somewhere.new("Paulina", "Chicago")) + end + def test_included_in_classes assert One.included_in_classes.include?(Ab) assert One.included_in_classes.include?(Xy::Bc) @@ -70,14 +74,12 @@ class ModuleTest < Test::Unit::TestCase end def test_delegation_to_methods - david = Someone.new("David", Somewhere.new("Paulina", "Chicago")) - assert_equal "Paulina", david.street - assert_equal "Chicago", david.city + assert_equal "Paulina", @david.street + assert_equal "Chicago", @david.city end def test_delegation_down_hierarchy - david = Someone.new("David", Somewhere.new("Paulina", "Chicago")) - assert_equal "CHICAGO", david.upcase + assert_equal "CHICAGO", @david.upcase end def test_delegation_to_instance_variable @@ -91,16 +93,14 @@ class ModuleTest < Test::Unit::TestCase end def test_delegation_prefix - david = Someone.new("David", Somewhere.new("Paulina", "Chicago")) - invoice = Invoice.new(david) + 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_delegation_custom_prefix - david = Someone.new("David", Somewhere.new("Paulina", "Chicago")) - invoice = Invoice.new(david) + invoice = Invoice.new(@david) assert_equal invoice.customer_name, "David" assert_equal invoice.customer_street, "Paulina" assert_equal invoice.customer_city, "Chicago" -- 1.5.4.3 From c3e7d80a089313e2b79cb2ade0240dd92e22794c Mon Sep 17 00:00:00 2001 From: Daniel Schierbeck Date: Sun, 21 Sep 2008 15:27:50 +0200 Subject: [PATCH] Added documentation of the new :prefix option. --- .../active_support/core_ext/module/delegation.rb | 25 ++++++++++++++++++++ 1 files changed, 25 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 bd3afa1..011fcbc 100644 --- a/activesupport/lib/active_support/core_ext/module/delegation.rb +++ b/activesupport/lib/active_support/core_ext/module/delegation.rb @@ -47,6 +47,31 @@ class Module # Foo.new.min # => 4 # Foo.new.max # => 11 # + # Delegates can optionally be prefixed using the :prefix option. If the value + # is true, the delegate methods are prefixed with the name of the object being + # delegated to. + # + # Person = Struct.new(:name, :address) + # + # class Invoice < Struct.new(:client) + # delegate :name, :address, :to => :client, :prefix => true + # end + # + # john_doe = Person.new("John Doe", "Vimmersvej 13") + # invoice = Invoice.new(john_doe) + # invoice.client_name # => "John Doe" + # invoice.client_address # => "Vimmersvej 13" + # + # It is also possible to supply a custom prefix. + # + # class Invoice < Struct.new(:client) + # delegate :name, :address, :to => :client, :prefix => :customer + # end + # + # invoice = Invoice.new(john_doe) + # invoice.customer_name # => "John Doe" + # invoice.customer_address # => "Vimmersvej 13" + # def delegate(*methods) options = methods.pop unless options.is_a?(Hash) && to = options[:to] -- 1.5.4.3 From eae9733e424b532134afe4e215c37d36941e74cd Mon Sep 17 00:00:00 2001 From: Daniel Schierbeck Date: Sun, 21 Sep 2008 15:29:32 +0200 Subject: [PATCH] Simplified the implementation of the :prefix option. --- .../active_support/core_ext/module/delegation.rb | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/module/delegation.rb b/activesupport/lib/active_support/core_ext/module/delegation.rb index 011fcbc..2a11e17 100644 --- a/activesupport/lib/active_support/core_ext/module/delegation.rb +++ b/activesupport/lib/active_support/core_ext/module/delegation.rb @@ -78,7 +78,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] && (options[:prefix] == true ? "#{to}_" : "#{options[:prefix]}_") + prefix = options[:prefix] && "#{options[:prefix] == true ? to : options[:prefix]}_" methods.each do |method| module_eval(<<-EOS, "(__DELEGATION__)", 1) -- 1.5.4.3