16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/memoizer.rb', line 16
def memoize(*method_names)
method_names.each do |method_name|
memoized_ivar_name = Memoizer.ivar_name(method_name)
unmemoized_method = "_unmemoized_#{method_name}"
alias_method unmemoized_method, method_name
no_args = self.instance_method(unmemoized_method).arity == 0
define_method method_name do |*args|
if instance_variable_defined?(memoized_ivar_name)
memoized_value = self.instance_variable_get(memoized_ivar_name)
end
if no_args
if !memoized_value.is_a?(Array)
memoized_value = [self.send(unmemoized_method)]
self.instance_variable_set(memoized_ivar_name, memoized_value)
end
memoized_value.first
else
if !memoized_value.is_a?(Hash)
memoized_value = {args => self.send(unmemoized_method, *args)}
self.instance_variable_set(memoized_ivar_name, memoized_value)
elsif !memoized_value.has_key?(args)
memoized_value[args] = self.send(unmemoized_method, *args)
end
memoized_value[args]
end
end
if self.private_method_defined?(unmemoized_method)
private method_name
elsif self.protected_method_defined?(unmemoized_method)
protected method_name
end
end
end
|