redis-roc 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,11 @@
1
+ module ROC
2
+ module Store
3
+ class ROCStore
4
+
5
+ def initialize
6
+ raise "ROCStore is an abstract base class"
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,42 @@
1
+ require 'rufus/lua'
2
+ module ROC
3
+ module Store
4
+ module TransientEval
5
+
6
+ protected
7
+
8
+ def eval(script, num_key_args, *args)
9
+ s = Rufus::Lua::State.new
10
+
11
+ s['KEYS'] = args.slice(0...num_key_args)
12
+ s['ARGV'] = args.slice(num_key_args...args.size)
13
+ s['redis'] = {}
14
+
15
+ s.function('redis.call') do |*args|
16
+ command = args.shift.to_sym
17
+ self.call command, *args
18
+ end
19
+ s.function('redis.log') do |*args|
20
+ puts args.map{|x| rec_to_r(x)}.join(' ')
21
+ end
22
+
23
+ res = s.eval(script)
24
+ rec_to_r(res)
25
+ end
26
+
27
+ def rec_to_r(obj)
28
+ if obj.is_a?(Rufus::Lua::Table)
29
+ robj = obj.to_ruby
30
+ if robj.is_a?(Array)
31
+ robj.map{|x| rec_to_r(x)}
32
+ elsif robj.is_a?(Hash)
33
+ Hash[robj.map{|pair| [pair[0], rec_to_r(pair[1])]}]
34
+ end
35
+ else
36
+ obj
37
+ end
38
+ end
39
+
40
+ end
41
+ end
42
+ end