Skip to content

Commit

Permalink
Escaping symbol passed into Memoizable's flush_cache for query method…
Browse files Browse the repository at this point in the history
…s to allow them to be cleared

Signed-off-by: Michael Koziarski <michael@koziarski.com>

[#3138 state:committed]
  • Loading branch information
Jay Pignata authored and NZKoz committed Sep 28, 2009
1 parent e01f997 commit d48ebea
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
4 changes: 2 additions & 2 deletions activesupport/lib/active_support/memoizable.rb
Expand Up @@ -57,10 +57,10 @@ def prime_cache(*syms)
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
Expand Down
11 changes: 10 additions & 1 deletion activesupport/test/memoizable_test.rb
Expand Up @@ -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
Expand All @@ -18,6 +19,7 @@ def name
end

def name?
@name_query_calls += 1
true
end
memoize :name?
Expand Down Expand Up @@ -116,6 +118,13 @@ def test_memoization_with_punctuation
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
Expand Down

0 comments on commit d48ebea

Please sign in to comment.