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
|
# File 'lib/next-big-sound/memoize.rb', line 5
def remember(name)
original_method = instance_method(name)
define_method(name) do |*args|
if defined?(KEYSTORE)
key = args.collect {|c| c.to_s }.join("_").gsub(" ","_")
key+=name.to_s
key = Digest::MD5.hexdigest(key)
puts key
if KEYSTORE.get(key)!=nil
puts "it was memorized"
Marshal.restore(KEYSTORE.get(key))
else
puts "it had to be memorized"
bound_method = original_method.bind(self)
KEYSTORE.set(key,Marshal.dump(bound_method.call(*args)))
return Marshal.restore(KEYSTORE.get(key))
end
else
bound_method = original_method.bind(self)
bound_method.call(*args)
end
end
end
|