mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Append a "_" to memoized instance variables
This commit is contained in:
parent
911c2c3813
commit
7f0346237e
1 changed files with 4 additions and 3 deletions
|
@ -7,15 +7,16 @@ module ActiveSupport
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
def memoize(symbol)
|
def memoize(symbol)
|
||||||
original_method = "_unmemoized_#{symbol}"
|
original_method = "_unmemoized_#{symbol}"
|
||||||
|
memoized_ivar = "@_memoized_#{symbol}"
|
||||||
raise "Already memoized #{symbol}" if instance_methods.map(&:to_s).include?(original_method)
|
raise "Already memoized #{symbol}" if instance_methods.map(&:to_s).include?(original_method)
|
||||||
|
|
||||||
alias_method original_method, symbol
|
alias_method original_method, symbol
|
||||||
class_eval <<-EOS, __FILE__, __LINE__
|
class_eval <<-EOS, __FILE__, __LINE__
|
||||||
def #{symbol}
|
def #{symbol}
|
||||||
if defined? @#{symbol}
|
if defined? #{memoized_ivar}
|
||||||
@#{symbol}
|
#{memoized_ivar}
|
||||||
else
|
else
|
||||||
@#{symbol} = #{original_method}
|
#{memoized_ivar} = #{original_method}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
EOS
|
EOS
|
||||||
|
|
Loading…
Reference in a new issue