Skip to content
Browse files

Ensure ActiveSupport::Memoizable respects private methods. [#2138 sta…

…te:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information...
1 parent e97180c commit c67d25e3c4e636d7c94909a7398231a634accf46 @niels niels committed with lifo Mar 6, 2009
Showing with 26 additions and 1 deletion.
  1. +4 −0 activesupport/lib/active_support/memoizable.rb
  2. +22 −1 activesupport/test/memoizable_test.rb
View
4 activesupport/lib/active_support/memoizable.rb
@@ -89,6 +89,10 @@ def #{symbol}(*args) # def m
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
end
View
23 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 @@ def age
end
memoize :name, :age
+
+ private
+
+ def is_developer?
+ @is_developer_calls += 1
+ "Yes"
+ end
+ memoize :is_developer?
end
class Company
@@ -223,4 +233,15 @@ def test_double_memoization
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

0 comments on commit c67d25e

Please sign in to comment.
Something went wrong with that request. Please try again.