blob: 5af50df023b12c5217d7e2c6e529db982f1218c4 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
module ActiveSupport
module Memoizable
def self.included(base) #:nodoc:
base.extend(ClassMethods)
end
module ClassMethods
def memoize(symbol)
original_method = "_unmemoized_#{symbol}"
raise "Already memoized #{symbol}" if instance_methods.map(&:to_s).include?(original_method)
alias_method original_method, symbol
class_eval <<-EOS, __FILE__, __LINE__
def #{symbol}
if defined? @#{symbol}
@#{symbol}
else
@#{symbol} = #{original_method}
end
end
EOS
end
end
def freeze
methods.each do |method|
if m = method.to_s.match(/\A_unmemoized_(.*)/)
send(m[1]).freeze
end
end
super
end
end
end
|