From 731c63f8ebc93e703b1a114d86f4f9f694ae48bf Mon Sep 17 00:00:00 2001 From: Daniel Schierbeck Date: Sun, 14 Sep 2008 23:20:51 +0200 Subject: Added :prefix option to Module#delegate. Signed-off-by: Michael Koziarski --- activesupport/lib/active_support/core_ext/module/delegation.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/module/delegation.rb b/activesupport/lib/active_support/core_ext/module/delegation.rb index e0b5f3e379..fee4454cb2 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 -- cgit v1.2.3 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 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport/lib/active_support/core_ext') 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) -- cgit v1.2.3 From 32a58d2afc18c1ab6cb5a8b9fbd0ff981d7a714a Mon Sep 17 00:00:00 2001 From: Daniel Schierbeck Date: Sun, 21 Sep 2008 15:27:50 +0200 Subject: Added documentation of the new :prefix option. Signed-off-by: Michael Koziarski --- .../active_support/core_ext/module/delegation.rb | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/module/delegation.rb b/activesupport/lib/active_support/core_ext/module/delegation.rb index bd3afa1961..c457569b9a 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] -- cgit v1.2.3 From de0ed534f6055c365d05c685582edeceef1eafa6 Mon Sep 17 00:00:00 2001 From: Daniel Schierbeck Date: Sun, 21 Sep 2008 15:29:32 +0200 Subject: Simplified the implementation of the :prefix option. Signed-off-by: Michael Koziarski [#984 state:committed] --- activesupport/lib/active_support/core_ext/module/delegation.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/module/delegation.rb b/activesupport/lib/active_support/core_ext/module/delegation.rb index c457569b9a..1ad18402e5 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) -- cgit v1.2.3