From 856c2d4d2c1693e0ad9a08b325a379cd245f3676 Mon Sep 17 00:00:00 2001 From: Daniel Schierbeck Date: Fri, 9 Jan 2009 19:01:27 +0100 Subject: [PATCH] Add :allow_override option to Module#delegate This enables one to override the delegation. class Name < Struct.new(:first, :last) def display_name "#{first} #{last}" end end class Person < Struct.new(:name) delegate :display_name, :to => :name, :allow_override => true end david = Person.new(:name => Name.new("David", "Hansson")) david.display_name #=> "David Hansson" david.display_name = "DHH" david.display_name #=> "DHH" --- .../active_support/core_ext/module/delegation.rb | 9 ++++++--- activesupport/test/core_ext/module_test.rb | 13 +++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/module/delegation.rb b/activesupport/lib/active_support/core_ext/module/delegation.rb index fb4b5f0..f842824 100644 --- a/activesupport/lib/active_support/core_ext/module/delegation.rb +++ b/activesupport/lib/active_support/core_ext/module/delegation.rb @@ -111,10 +111,13 @@ class Module allow_nil = options[:allow_nil] && "#{to} && " methods.each do |method| + name = "#{prefix}#{method}" + allow_override = options[:allow_override] && "@#{method} || " + attr_writer name if options[:allow_override] module_eval(<<-EOS, "(__DELEGATION__)", 1) - def #{prefix}#{method}(*args, &block) # def customer_name(*args, &block) - #{allow_nil}#{to}.__send__(#{method.inspect}, *args, &block) # client && client.__send__(:name, *args, &block) - end # end + def #{name}(*args, &block) # def customer_name(*args, &block) + #{allow_override}#{allow_nil}#{to}.__send__(#{method.inspect}, *args, &block) # @customer_name || client && client.__send__(:name, *args, &block) + end # end EOS end end diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb index a5d9850..2894c4b 100644 --- a/activesupport/test/core_ext/module_test.rb +++ b/activesupport/test/core_ext/module_test.rb @@ -34,6 +34,7 @@ Someone = Struct.new(:name, :place) do delegate :street, :city, :to => :place delegate :state, :to => :@place delegate :upcase, :to => "place.city" + delegate :display_name, :to => :name, :allow_override => true end Invoice = Struct.new(:client) do @@ -51,6 +52,10 @@ class Name def initialize(first, last) @full_name = "#{first} #{last}" end + + def display_name + @full_name + end end $nowhere = <<-EOF @@ -163,6 +168,14 @@ class ModuleTest < Test::Unit::TestCase assert_equal 'yz/zy', Yz::Zy.as_load_path assert_equal 'yz', Yz.as_load_path end + + def test_allow_override + david = Someone.new(Name.new("David", "Hansson")) + assert_equal "David Hansson", david.display_name + + david.display_name = "DHH" + assert_equal "DHH", david.display_name + end end module BarMethodAliaser -- 1.5.6.3