From c6bf773b6f715faf7639f6a1f1e7afa1dc9c7f3c Mon Sep 17 00:00:00 2001 From: Sergio Gil Date: Mon, 8 Dec 2008 00:53:38 +0100 Subject: [PATCH] Added :allow_nil option for delegate --- .../active_support/core_ext/module/delegation.rb | 28 +++++++++++++++++++- activesupport/test/core_ext/module_test.rb | 27 +++++++++++++++++++ 2 files changed, 54 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 2905eeb..a42f206 100644 --- a/activesupport/lib/active_support/core_ext/module/delegation.rb +++ b/activesupport/lib/active_support/core_ext/module/delegation.rb @@ -72,6 +72,30 @@ class Module # invoice.customer_name # => "John Doe" # invoice.customer_address # => "Vimmersvej 13" # + # If the object to which you delegate can be nil, you may want to use the + # :allow_nil option. In that case, it returns nil instead of raising a + # NoMethodError exception: + # + # class Foo + # attr_accessor :bar + # def initialize(bar = nil) + # @bar = bar + # end + # delegate :zoo, :to => :bar + # end + # + # Foo.new.zoo # raises NoMethodError exception (you called nil.zoo) + # + # class Foo + # attr_accessor :bar + # def initialize(bar = nil) + # @bar = bar + # end + # delegate :zoo, :to => :bar, :allow_nil => true + # end + # + # Foo.new.zoo # returns nil + # def delegate(*methods) options = methods.pop unless options.is_a?(Hash) && to = options[:to] @@ -83,11 +107,13 @@ class Module end prefix = options[:prefix] && "#{options[:prefix] == true ? to : options[:prefix]}_" + + allow_nil = options[:allow_nil] && "#{to} && " methods.each do |method| module_eval(<<-EOS, "(__DELEGATION__)", 1) def #{prefix}#{method}(*args, &block) - #{to}.__send__(#{method.inspect}, *args, &block) + #{allow_nil}#{to}.__send__(#{method.inspect}, *args, &block) end EOS end diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb index 886f692..9a14630 100644 --- a/activesupport/test/core_ext/module_test.rb +++ b/activesupport/test/core_ext/module_test.rb @@ -41,6 +41,10 @@ Invoice = Struct.new(:client) do delegate :street, :city, :name, :to => :client, :prefix => :customer end +Project = Struct.new(:description, :person) do + delegate :name, :to => :person, :allow_nil => true +end + class Name delegate :upcase, :to => :@full_name @@ -116,6 +120,29 @@ class ModuleTest < Test::Unit::TestCase end end end + + def test_delegation_with_allow_nil + rails = Project.new("Rails", Someone.new("David")) + assert_equal rails.name, "David" + end + + def test_delegation_with_allow_nil_and_nil_value + rails = Project.new("Rails") + assert_nil rails.name + end + + def test_delegation_with_allow_nil_and_nil_value_and_prefix + Project.class_eval do + delegate :name, :to => :person, :allow_nil => true, :prefix => true + end + rails = Project.new("Rails") + assert_nil rails.person_name + end + + def test_delegation_without_allow_nil_and_nil_value + david = Someone.new("David") + assert_raises(NoMethodError) { david.street } + end def test_parent assert_equal Yz::Zy, Yz::Zy::Cd.parent -- 1.5.3.7