From 752e2201f9df63ccd4cd4e5d6c86fc95997997dc Mon Sep 17 00:00:00 2001 From: Jay Pignata Date: Fri, 4 Sep 2009 17:28:36 -0400 Subject: [PATCH] Escaping symbol passed into Memoizable's flush_cache for query methods to allow them to be cleared --- activesupport/lib/active_support/memoizable.rb | 4 ++-- activesupport/test/memoizable_test.rb | 11 ++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/activesupport/lib/active_support/memoizable.rb b/activesupport/lib/active_support/memoizable.rb index 7724b9d..b197fbc 100644 --- a/activesupport/lib/active_support/memoizable.rb +++ b/activesupport/lib/active_support/memoizable.rb @@ -57,10 +57,10 @@ module ActiveSupport end end - def flush_cache(*syms, &block) + def flush_cache(*syms) syms.each do |sym| (methods + private_methods + protected_methods).each do |m| - if m.to_s =~ /^_unmemoized_(#{sym})/ + if m.to_s =~ /^_unmemoized_(#{sym.to_s.gsub(/\?\Z/, '\?')})/ ivar = ActiveSupport::Memoizable.memoized_ivar_for($1) instance_variable_get(ivar).clear if instance_variable_defined?(ivar) end diff --git a/activesupport/test/memoizable_test.rb b/activesupport/test/memoizable_test.rb index 214e243..fb7a15e 100644 --- a/activesupport/test/memoizable_test.rb +++ b/activesupport/test/memoizable_test.rb @@ -4,12 +4,13 @@ class MemoizableTest < ActiveSupport::TestCase class Person extend ActiveSupport::Memoizable - attr_reader :name_calls, :age_calls, :is_developer_calls + attr_reader :name_calls, :age_calls, :is_developer_calls, :name_query_calls def initialize @name_calls = 0 @age_calls = 0 @is_developer_calls = 0 + @name_query_calls = 0 end def name @@ -18,6 +19,7 @@ class MemoizableTest < ActiveSupport::TestCase end def name? + @name_query_calls += 1 true end memoize :name? @@ -116,6 +118,13 @@ class MemoizableTest < ActiveSupport::TestCase end end + def test_memoization_flush_with_punctuation + assert_equal true, @person.name? + @person.flush_cache(:name?) + 3.times { assert_equal true, @person.name? } + assert_equal 2, @person.name_query_calls + end + def test_memoization_with_nil_value assert_equal nil, @person.age assert_equal 1, @person.age_calls -- 1.6.4.2