mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fixed memoize with punctuation and freezing memoized methods with arguments
Signed-off-by: Joshua Peek <josh@joshpeek.com>
This commit is contained in:
parent
105093f907
commit
a8057669ff
2 changed files with 26 additions and 4 deletions
|
@ -10,9 +10,16 @@ module ActiveSupport
|
|||
end
|
||||
|
||||
def freeze_with_memoizable
|
||||
methods.each do |method|
|
||||
__send__($1) if method.to_s =~ /^_unmemoized_(.*)/
|
||||
end unless frozen?
|
||||
unless frozen?
|
||||
methods.each do |method|
|
||||
if method.to_s =~ /^_unmemoized_(.*)/
|
||||
begin
|
||||
__send__($1)
|
||||
rescue ArgumentError
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
freeze_without_memoizable
|
||||
end
|
||||
|
@ -21,7 +28,7 @@ module ActiveSupport
|
|||
def memoize(*symbols)
|
||||
symbols.each do |symbol|
|
||||
original_method = "_unmemoized_#{symbol}"
|
||||
memoized_ivar = "@_memoized_#{symbol}"
|
||||
memoized_ivar = "@_memoized_#{symbol.to_s.sub(/\?\Z/, '_query').sub(/!\Z/, '_bang')}"
|
||||
|
||||
class_eval <<-EOS, __FILE__, __LINE__
|
||||
include Freezable
|
||||
|
|
|
@ -16,6 +16,16 @@ uses_mocha 'Memoizable' do
|
|||
"Josh"
|
||||
end
|
||||
|
||||
def name?
|
||||
true
|
||||
end
|
||||
memoize :name?
|
||||
|
||||
def update(name)
|
||||
"Joshua"
|
||||
end
|
||||
memoize :update
|
||||
|
||||
def age
|
||||
@age_calls += 1
|
||||
nil
|
||||
|
@ -88,6 +98,10 @@ uses_mocha 'Memoizable' do
|
|||
assert_equal 1, @person.name_calls
|
||||
end
|
||||
|
||||
def test_memoization_with_punctuation
|
||||
assert_equal true, @person.name?
|
||||
end
|
||||
|
||||
def test_memoization_with_nil_value
|
||||
assert_equal nil, @person.age
|
||||
assert_equal 1, @person.age_calls
|
||||
|
@ -114,6 +128,7 @@ uses_mocha 'Memoizable' do
|
|||
def test_memoized_is_not_affected_by_freeze
|
||||
@person.freeze
|
||||
assert_equal "Josh", @person.name
|
||||
assert_equal "Joshua", @person.update("Joshua")
|
||||
end
|
||||
|
||||
def test_memoization_with_args
|
||||
|
|
Loading…
Reference in a new issue