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
|
# File 'lib/memoized.rb', line 20
def memoize(*method_names)
method_names.each do |method_name|
memoized_ivar_name = Memoized.ivar_name(method_name)
unmemoized_method = "_unmemoized_#{method_name}"
alias_method unmemoized_method, method_name
parameters = Parameters.new(instance_method(unmemoized_method).parameters)
module_eval(" def \#{method_name}(\#{parameters.signature})\n \#{parameters.cache_key}\n\n \#{memoized_ivar_name} ||= {}\n \n if \#{memoized_ivar_name}.key?(cache_key)\n \#{memoized_ivar_name}[cache_key]\n else\n live_result = if all_kwargs.empty?\n \#{unmemoized_method}(*all_args)\n else \n \#{unmemoized_method}(*all_args, **all_kwargs)\n end\n \#{memoized_ivar_name}[cache_key] = live_result\n live_result\n end\n end\n RUBY\n\n if self.private_method_defined?(unmemoized_method)\n private method_name\n elsif self.protected_method_defined?(unmemoized_method)\n protected method_name\n end\n end\nend\n")
|