mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fix memoize_all for methods with punctuation [#1175 state:resolved]
Signed-off-by: Joshua Peek <josh@joshpeek.com>
This commit is contained in:
parent
1c75b4fd42
commit
8603813ac6
2 changed files with 12 additions and 3 deletions
|
@ -1,5 +1,9 @@
|
|||
module ActiveSupport
|
||||
module Memoizable
|
||||
MEMOIZED_IVAR = Proc.new do |symbol|
|
||||
"@_memoized_#{symbol.to_s.sub(/\?\Z/, '_query').sub(/!\Z/, '_bang')}".to_sym
|
||||
end
|
||||
|
||||
module Freezable
|
||||
def self.included(base)
|
||||
base.class_eval do
|
||||
|
@ -20,7 +24,7 @@ module ActiveSupport
|
|||
if method(m).arity == 0
|
||||
__send__($1)
|
||||
else
|
||||
ivar = :"@_memoized_#{$1}"
|
||||
ivar = MEMOIZED_IVAR.call($1)
|
||||
instance_variable_set(ivar, {})
|
||||
end
|
||||
end
|
||||
|
@ -30,7 +34,7 @@ module ActiveSupport
|
|||
def unmemoize_all
|
||||
methods.each do |m|
|
||||
if m.to_s =~ /^_unmemoized_(.*)/
|
||||
ivar = :"@_memoized_#{$1}"
|
||||
ivar = MEMOIZED_IVAR.call($1)
|
||||
instance_variable_get(ivar).clear if instance_variable_defined?(ivar)
|
||||
end
|
||||
end
|
||||
|
@ -40,7 +44,7 @@ module ActiveSupport
|
|||
def memoize(*symbols)
|
||||
symbols.each do |symbol|
|
||||
original_method = :"_unmemoized_#{symbol}"
|
||||
memoized_ivar = :"@_memoized_#{symbol.to_s.sub(/\?\Z/, '_query').sub(/!\Z/, '_bang')}"
|
||||
memoized_ivar = MEMOIZED_IVAR.call(symbol)
|
||||
|
||||
class_eval <<-EOS, __FILE__, __LINE__
|
||||
include Freezable
|
||||
|
|
|
@ -100,6 +100,11 @@ uses_mocha 'Memoizable' do
|
|||
|
||||
def test_memoization_with_punctuation
|
||||
assert_equal true, @person.name?
|
||||
|
||||
assert_nothing_raised(NameError) do
|
||||
@person.memoize_all
|
||||
@person.unmemoize_all
|
||||
end
|
||||
end
|
||||
|
||||
def test_memoization_with_nil_value
|
||||
|
|
Loading…
Reference in a new issue