Module: Memoist
- Defined in:
- lib/memoist.rb,
lib/memoist/version.rb
Defined Under Namespace
Modules: InstanceMethods
Classes: AlreadyMemoizedError
Constant Summary
collapse
- VERSION =
"0.10.0"
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.escape_punctuation(string) ⇒ Object
21
22
23
|
# File 'lib/memoist.rb', line 21
def self.escape_punctuation(string)
string.sub(/\?\Z/, '_query').sub(/!\Z/, '_bang')
end
|
33
34
35
36
37
38
|
# File 'lib/memoist.rb', line 33
def self.(method, args)
if args.length == method.arity + 1 && (args.last == true || args.last == :reload)
reload = args.pop
end
reload
end
|
.memoist_eval(klass, *args, &block) ⇒ Object
25
26
27
28
29
30
31
|
# File 'lib/memoist.rb', line 25
def self.memoist_eval(klass, *args, &block)
if klass.respond_to?(:class_eval)
klass.class_eval(*args, &block)
else
klass.singleton_class.class_eval(*args, &block)
end
end
|
.memoized_ivar_for(method_name, identifier = nil) ⇒ Object
5
6
7
|
# File 'lib/memoist.rb', line 5
def self.memoized_ivar_for(method_name, identifier=nil)
["@#{memoized_prefix(identifier)}", escape_punctuation(method_name.to_s)].join("_")
end
|
.memoized_prefix(identifier = nil) ⇒ Object
13
14
15
|
# File 'lib/memoist.rb', line 13
def self.memoized_prefix(identifier=nil)
["_memoized", identifier].compact.join("_")
end
|
.unmemoized_method_for(method_name, identifier = nil) ⇒ Object
9
10
11
|
# File 'lib/memoist.rb', line 9
def self.unmemoized_method_for(method_name, identifier=nil)
[unmemoized_prefix(identifier), method_name].join("_").to_sym
end
|
.unmemoized_prefix(identifier = nil) ⇒ Object
17
18
19
|
# File 'lib/memoist.rb', line 17
def self.unmemoized_prefix(identifier=nil)
["_unmemoized", identifier].compact.join("_")
end
|
Instance Method Details
#memoize(*method_names) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
# File 'lib/memoist.rb', line 86
def memoize(*method_names)
if method_names.last.is_a?(Hash)
identifier = method_names.pop[:identifier]
end
method_names.each do |method_name|
unmemoized_method = Memoist.unmemoized_method_for(method_name, identifier)
memoized_ivar = Memoist.memoized_ivar_for(method_name, identifier)
Memoist.memoist_eval(self) do
include InstanceMethods
if method_defined?(unmemoized_method)
raise AlreadyMemoizedError.new("Already memoized #{method_name}")
end
alias_method unmemoized_method, method_name
if instance_method(method_name).arity == 0
module_eval <<-EOS, __FILE__, __LINE__ + 1
def #{method_name}(reload = false)
skip_cache = reload || !instance_variable_defined?("#{memoized_ivar}")
set_cache = skip_cache && !frozen?
if skip_cache
value = #{unmemoized_method}
else
value = #{memoized_ivar}
end
if set_cache
#{memoized_ivar} = value
end
value
end
EOS
else
module_eval <<-EOS, __FILE__, __LINE__ + 1
def #{method_name}(*args)
reload = Memoist.extract_reload!(method(#{unmemoized_method.inspect}), args)
skip_cache = reload || !(instance_variable_defined?(#{memoized_ivar.inspect}) && #{memoized_ivar} && #{memoized_ivar}.has_key?(args))
set_cache = skip_cache && !frozen?
if skip_cache
value = #{unmemoized_method}(*args)
else
value = #{memoized_ivar}[args]
end
if set_cache
#{memoized_ivar} ||= {}
#{memoized_ivar}[args] = value
end
value
end
EOS
end
if private_method_defined?(unmemoized_method)
private method_name
elsif protected_method_defined?(unmemoized_method)
protected method_name
end
end
end
method_names.length == 1 ? method_names.first : method_names
end
|