redis-scripted 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -17,7 +17,7 @@ class should look for scripts:
17
17
 
18
18
  require "redis/scripted"
19
19
 
20
- redis = Redis.connect(scripts_path: "./scripts")
20
+ redis = Redis::Scripted.connect(scripts_path: "./scripts")
21
21
 
22
22
  The scripts are defined as instance methods for convenience:
23
23
 
@@ -0,0 +1,8 @@
1
+ task default: :test
2
+
3
+ desc "Run suite"
4
+ task :test do
5
+ require "cutest"
6
+
7
+ Cutest.run(Dir["test/*.rb"])
8
+ end
@@ -0,0 +1,49 @@
1
+ require "redis"
2
+ require "digest/sha1"
3
+
4
+ class Redis::Scripted < Redis
5
+ def initialize(options = {})
6
+ path = options.delete(:scripts_path)
7
+
8
+ super(options)
9
+
10
+ if path
11
+ Dir["#{path}/*.lua"].each do |file|
12
+ name = File.basename(file, ".lua")
13
+ _define_script_method(name, File.read(file))
14
+ end
15
+ end
16
+ end
17
+
18
+ def evalsha(sha, keys = [], values = [])
19
+ synchronize do
20
+ @client.call(:evalsha, sha, keys.size, *keys, *values)
21
+ end
22
+ end
23
+
24
+ def eval(script, keys = [], values = [])
25
+ synchronize do
26
+ @client.call(:eval, script, keys.size, *keys, *values)
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def _define_script_method(name, script)
33
+ sha = Digest::SHA1.hexdigest(script)
34
+
35
+ self.class.send(:define_method, name) do |keys, values = []|
36
+ synchronize do
37
+ begin
38
+ evalsha(sha, keys, values)
39
+ rescue RuntimeError => e
40
+ if e.message =~ /^NOSCRIPT/
41
+ eval(script, keys, values)
42
+ else
43
+ raise e
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "redis-scripted"
3
- s.version = "0.0.2"
3
+ s.version = "0.0.3"
4
4
  s.summary = "A Ruby client that supports the experimental scripting feature of Redis"
5
5
  s.authors = ["Damian Janowski"]
6
6
  s.email = ["djanowski@dimaion.com"]
@@ -14,6 +14,7 @@ Gem::Specification.new do |s|
14
14
  "Rakefile",
15
15
  "bin/*",
16
16
  "*.gemspec",
17
- "test/*.*"
17
+ "test/*.*",
18
+ "lib/redis/*"
18
19
  ]
19
20
  end
@@ -0,0 +1,12 @@
1
+ require "cutest"
2
+ require "tmpdir"
3
+
4
+ test "gem" do
5
+ Dir.mktmpdir do |path|
6
+ file = `gem build redis-scripted.gemspec 2>/dev/null`[/File: (.*)$/, 1]
7
+
8
+ `env GEM_HOME=#{path} gem install #{file}`
9
+
10
+ assert_equal "Redis::Scripted", `env GEM_HOME=#{path} ruby -r redis/scripted -e 'puts Redis::Scripted'`.chomp
11
+ end
12
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: redis-scripted
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.2
5
+ version: 0.0.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Damian Janowski
@@ -35,9 +35,12 @@ extra_rdoc_files: []
35
35
 
36
36
  files:
37
37
  - README.md
38
+ - Rakefile
38
39
  - redis-scripted.gemspec
40
+ - test/gem.rb
39
41
  - test/redis.conf
40
42
  - test/rediscripted.rb
43
+ - lib/redis/scripted.rb
41
44
  has_rdoc: true
42
45
  homepage: http://github.com/djanowski/redis-scripted
43
46
  licenses: []