From c67d25e3c4e636d7c94909a7398231a634accf46 Mon Sep 17 00:00:00 2001 From: Niels Ganser Date: Fri, 6 Mar 2009 14:40:54 +0000 Subject: Ensure ActiveSupport::Memoizable respects private methods. [#2138 state:resolved] Signed-off-by: Pratik Naik --- activesupport/lib/active_support/memoizable.rb | 4 ++++ activesupport/test/memoizable_test.rb | 23 ++++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/activesupport/lib/active_support/memoizable.rb b/activesupport/lib/active_support/memoizable.rb index 952b4d8063..71cfe61739 100644 --- a/activesupport/lib/active_support/memoizable.rb +++ b/activesupport/lib/active_support/memoizable.rb @@ -88,6 +88,10 @@ module ActiveSupport #{original_method}(*args) # _unmemoized_mime_type(*args) end # end end # end + end # end + # + if private_method_defined?(#{original_method.inspect}) # if private_method_defined?(:_unmemoized_mime_type) + private #{symbol.inspect} # private :mime_type end # end EOS end diff --git a/activesupport/test/memoizable_test.rb b/activesupport/test/memoizable_test.rb index 069ae27eb2..39420c5a3a 100644 --- a/activesupport/test/memoizable_test.rb +++ b/activesupport/test/memoizable_test.rb @@ -4,10 +4,12 @@ class MemoizableTest < Test::Unit::TestCase class Person extend ActiveSupport::Memoizable - attr_reader :name_calls, :age_calls + attr_reader :name_calls, :age_calls, :is_developer_calls + def initialize @name_calls = 0 @age_calls = 0 + @is_developer_calls = 0 end def name @@ -31,6 +33,14 @@ class MemoizableTest < Test::Unit::TestCase end memoize :name, :age + + private + + def is_developer? + @is_developer_calls += 1 + "Yes" + end + memoize :is_developer? end class Company @@ -223,4 +233,15 @@ class MemoizableTest < Test::Unit::TestCase company.memoize :name assert_raise(RuntimeError) { company.memoize :name } end + + def test_private_method_memoization + person = Person.new + + assert_raise(NoMethodError) { person.is_developer? } + assert_equal "Yes", person.send(:is_developer?) + assert_equal 1, person.is_developer_calls + assert_equal "Yes", person.send(:is_developer?) + assert_equal 1, person.is_developer_calls + end + end -- cgit v1.2.3