wolverine 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +6 -3
- data/Rakefile +7 -0
- data/TODO +2 -1
- data/lib/wolverine.rb +8 -34
- data/lib/wolverine/path_component.rb +53 -0
- data/lib/wolverine/script.rb +2 -0
- data/lib/wolverine/version.rb +1 -1
- data/test/integration/lua/util/mexists.lua +6 -0
- data/test/integration/wolverine_integration_test.rb +25 -0
- data/test/wolverine/path_component_test.rb +30 -0
- data/test/wolverine_test.rb +3 -22
- metadata +14 -7
data/README.md
CHANGED
@@ -26,10 +26,10 @@ cd redis && make
|
|
26
26
|
gem 'wolverine'
|
27
27
|
```
|
28
28
|
|
29
|
-
3) Add your lua scripts to `app/
|
29
|
+
3) Add your lua scripts to `app/wolverine`:
|
30
30
|
|
31
31
|
```lua
|
32
|
-
-- app/
|
32
|
+
-- app/wolverine/util/mexists.lua
|
33
33
|
local exists = {}
|
34
34
|
local existence
|
35
35
|
for _, key in ipairs(KEYS) do
|
@@ -41,9 +41,12 @@ return exists
|
|
41
41
|
4) Call wolverine from your code:
|
42
42
|
|
43
43
|
```ruby
|
44
|
-
Wolverine.
|
44
|
+
Wolverine.util.mexists('key1', 'key2', 'key3') #=> [0, 1, 0]
|
45
45
|
```
|
46
46
|
|
47
|
+
Methods are available on `Wolverine` paralleling the directory structure
|
48
|
+
of wolverine's `script_path`.
|
49
|
+
|
47
50
|
## Configuration
|
48
51
|
|
49
52
|
Available configuration options:
|
data/Rakefile
CHANGED
data/TODO
CHANGED
@@ -1 +1,2 @@
|
|
1
|
-
|
1
|
+
- Tests for Wolverine::PathComponent
|
2
|
+
- Think of more TODO items
|
data/lib/wolverine.rb
CHANGED
@@ -1,32 +1,12 @@
|
|
1
1
|
require 'redis'
|
2
|
+
require 'pathname'
|
2
3
|
|
3
4
|
require 'wolverine/version'
|
4
5
|
require 'wolverine/configuration'
|
5
6
|
require 'wolverine/script'
|
7
|
+
require 'wolverine/path_component'
|
6
8
|
|
7
9
|
module Wolverine
|
8
|
-
|
9
|
-
class Directory
|
10
|
-
class MissingTemplate < StandardError ; end
|
11
|
-
def initialize path
|
12
|
-
@path = path
|
13
|
-
end
|
14
|
-
|
15
|
-
def method_missing sym, *args
|
16
|
-
resolve sym, *args
|
17
|
-
end
|
18
|
-
|
19
|
-
def resolve sym, *args
|
20
|
-
if File.directory?(path = @path + sym.to_s)
|
21
|
-
Directory.new(path)
|
22
|
-
elsif File.exists?(path = @path + "#{sym}.lua")
|
23
|
-
Wolverine.call path, *args
|
24
|
-
else
|
25
|
-
raise MissingTemplate
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
10
|
def self.config
|
31
11
|
@config ||= Configuration.new
|
32
12
|
end
|
@@ -35,20 +15,14 @@ module Wolverine
|
|
35
15
|
config.redis
|
36
16
|
end
|
37
17
|
|
38
|
-
def self.
|
39
|
-
|
40
|
-
rescue Directory::MissingTemplate
|
41
|
-
super
|
42
|
-
end
|
43
|
-
|
44
|
-
def self.call(file, *args)
|
45
|
-
pathname = file.kind_of?(Pathname) ? file : full_path(file)
|
46
|
-
Script.new(pathname).call(redis, *args)
|
18
|
+
def self.root_directory
|
19
|
+
@root_directory ||= PathComponent.new(config.script_path)
|
47
20
|
end
|
48
21
|
|
49
|
-
def self.
|
50
|
-
|
51
|
-
|
22
|
+
def self.method_missing sym, *args
|
23
|
+
root_directory.send(sym, *args)
|
24
|
+
rescue PathComponent::MissingTemplate
|
25
|
+
super
|
52
26
|
end
|
53
27
|
|
54
28
|
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Wolverine
|
2
|
+
class PathComponent
|
3
|
+
class MissingTemplate < StandardError ; end
|
4
|
+
|
5
|
+
def initialize path
|
6
|
+
@path = path
|
7
|
+
end
|
8
|
+
|
9
|
+
def method_missing sym, *args
|
10
|
+
create_method sym, *args
|
11
|
+
send sym, *args
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def create_method sym, *args
|
17
|
+
if directory?(path = @path + sym.to_s)
|
18
|
+
define_directory_method path, sym
|
19
|
+
elsif file?(path = @path + "#{sym}.lua")
|
20
|
+
define_script_method path, sym, *args
|
21
|
+
else
|
22
|
+
raise MissingTemplate
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def directory?(path)
|
27
|
+
File.directory?(path)
|
28
|
+
end
|
29
|
+
|
30
|
+
def file?(path)
|
31
|
+
File.exists?(path) && !File.directory?(path)
|
32
|
+
end
|
33
|
+
|
34
|
+
def define_directory_method path, sym
|
35
|
+
dir = PathComponent.new(path)
|
36
|
+
define_metaclass_method(sym) { dir }
|
37
|
+
end
|
38
|
+
|
39
|
+
def define_script_method path, sym, *args
|
40
|
+
script = Wolverine::Script.new(path)
|
41
|
+
define_metaclass_method(sym) { |*args|
|
42
|
+
script.call(Wolverine.redis, *args)
|
43
|
+
}
|
44
|
+
end
|
45
|
+
|
46
|
+
def define_metaclass_method sym, &block
|
47
|
+
metaclass = class << self; self; end
|
48
|
+
metaclass.send(:define_method, sym, &block)
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
data/lib/wolverine/script.rb
CHANGED
data/lib/wolverine/version.rb
CHANGED
@@ -0,0 +1,25 @@
|
|
1
|
+
require_relative '../test_helper'
|
2
|
+
|
3
|
+
class WolverineIntegrationTest < MiniTest::Unit::TestCase
|
4
|
+
|
5
|
+
CONTENT = File.read(File.expand_path('../lua/util/mexists.lua', __FILE__))
|
6
|
+
|
7
|
+
def mock_redis
|
8
|
+
stub.tap do |redis|
|
9
|
+
redis.expects(:evalsha).
|
10
|
+
with('fe24f4dd4ba7881608cca4b846f009195e06d79a', 2, :a, :b).
|
11
|
+
raises("NOSCRIPT")
|
12
|
+
redis.expects(:eval).
|
13
|
+
with(CONTENT, 2, :a, :b).
|
14
|
+
returns([1, 0])
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_everything
|
19
|
+
Wolverine.config.redis = mock_redis
|
20
|
+
Wolverine.config.script_path = Pathname.new(File.expand_path('../lua', __FILE__))
|
21
|
+
|
22
|
+
assert_equal [1, 0], Wolverine.util.mexists(:a, :b)
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require_relative '../test_helper'
|
2
|
+
|
3
|
+
module Wolverine
|
4
|
+
class PathComponentTest < MiniTest::Unit::TestCase
|
5
|
+
|
6
|
+
def root
|
7
|
+
@root ||= Pathname.new('.')
|
8
|
+
end
|
9
|
+
|
10
|
+
def pc
|
11
|
+
@pc ||= Wolverine::PathComponent.new(root)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_directory_caching
|
15
|
+
pc.expects(:directory?).with(root + 'bar').returns(true)
|
16
|
+
assert_equal pc.bar.object_id, pc.bar.object_id
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_script_caching
|
20
|
+
pc.expects(:directory?).with(root + 'bar').returns(false)
|
21
|
+
pc.expects(:file?).with(root + 'bar.lua').returns(true)
|
22
|
+
script = stub
|
23
|
+
Wolverine::Script.expects(:new).once.returns(script)
|
24
|
+
script.expects(:call).twice.returns(:success)
|
25
|
+
|
26
|
+
assert_equal pc.bar, pc.bar
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
data/test/wolverine_test.rb
CHANGED
@@ -1,29 +1,10 @@
|
|
1
1
|
require_relative 'test_helper'
|
2
2
|
|
3
3
|
class WolverineTest < MiniTest::Unit::TestCase
|
4
|
-
|
5
|
-
def setup
|
6
|
-
Wolverine.config.redis = :redis
|
7
|
-
Wolverine.config.script_path = Pathname.new('foo')
|
8
|
-
end
|
9
|
-
|
10
|
-
def test_adds_extension_if_none_present
|
11
|
-
assert_equal Pathname.new('foo/bar.lua'), Wolverine.full_path('bar')
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_finding_a_file
|
15
|
-
assert_equal 3, Wolverine.three
|
16
|
-
end
|
17
|
-
|
4
|
+
|
18
5
|
def test_redis
|
6
|
+
Wolverine.config.redis = :redis
|
19
7
|
assert_equal :redis, Wolverine.redis
|
20
8
|
end
|
21
9
|
|
22
|
-
|
23
|
-
script = stub
|
24
|
-
script.expects(:call).with(:redis, :a, :b).returns(:return)
|
25
|
-
Wolverine::Script.expects(:new).with(Pathname.new('foo/bar.lua')).returns(script)
|
26
|
-
assert_equal :return, Wolverine.call('bar', :a, :b)
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
10
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wolverine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-03-02 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: redis
|
16
|
-
requirement: &
|
16
|
+
requirement: &70166214008280 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70166214008280
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: mocha
|
27
|
-
requirement: &
|
27
|
+
requirement: &70166214007720 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70166214007720
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: minitest
|
38
|
-
requirement: &
|
38
|
+
requirement: &70166214007120 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70166214007120
|
47
47
|
description: Wolverine provides a simple way to run server-side redis scripts from
|
48
48
|
a rails app
|
49
49
|
email:
|
@@ -61,10 +61,14 @@ files:
|
|
61
61
|
- examples/mexists.lua
|
62
62
|
- lib/wolverine.rb
|
63
63
|
- lib/wolverine/configuration.rb
|
64
|
+
- lib/wolverine/path_component.rb
|
64
65
|
- lib/wolverine/script.rb
|
65
66
|
- lib/wolverine/version.rb
|
67
|
+
- test/integration/lua/util/mexists.lua
|
68
|
+
- test/integration/wolverine_integration_test.rb
|
66
69
|
- test/test_helper.rb
|
67
70
|
- test/wolverine/configuration_test.rb
|
71
|
+
- test/wolverine/path_component_test.rb
|
68
72
|
- test/wolverine/script_test.rb
|
69
73
|
- test/wolverine_test.rb
|
70
74
|
- wolverine.gemspec
|
@@ -94,7 +98,10 @@ specification_version: 3
|
|
94
98
|
summary: Wolverine provides a simple way to run server-side redis scripts from a rails
|
95
99
|
app
|
96
100
|
test_files:
|
101
|
+
- test/integration/lua/util/mexists.lua
|
102
|
+
- test/integration/wolverine_integration_test.rb
|
97
103
|
- test/test_helper.rb
|
98
104
|
- test/wolverine/configuration_test.rb
|
105
|
+
- test/wolverine/path_component_test.rb
|
99
106
|
- test/wolverine/script_test.rb
|
100
107
|
- test/wolverine_test.rb
|