blob: 59fecbecb16d8cd85a65cd29a47c1d0f3a6e08d7 (
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
35
36
37
38
|
module ActiveSupport
module Memoizable #:nodoc:
def self.extended(obj)
klass = obj.respond_to?(:class_eval) ? obj : obj.metaclass
klass.class_eval do
def freeze
methods.each do |method|
if m = method.to_s.match(/^unmemoized_(.*)/)
send(m[1])
end
end
super
end
end
end
def memoize(*symbols)
symbols.each do |symbol|
original_method = "unmemoized_#{symbol}"
memoized_ivar = "@#{symbol}"
klass = respond_to?(:class_eval) ? self : self.metaclass
raise "Already memoized #{symbol}" if klass.instance_methods.map(&:to_s).include?(original_method)
klass.class_eval <<-EOS, __FILE__, __LINE__
alias_method :#{original_method}, :#{symbol}
def #{symbol}(reload = false)
if !reload && defined? #{memoized_ivar}
#{memoized_ivar}
else
#{memoized_ivar} = #{original_method}.freeze
end
end
EOS
end
end
end
end
|