Skip to content

Commit

Permalink
support Wolverine.new(config) syntax for multiple redis connections a…
Browse files Browse the repository at this point in the history
…nd paths
  • Loading branch information
ssoroka committed Aug 11, 2012
1 parent 32c3880 commit 47a9a38
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 14 deletions.
28 changes: 27 additions & 1 deletion lib/wolverine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
require 'wolverine/path_component'
require 'wolverine/lua_error'

module Wolverine
class Wolverine
# Returns the configuration object for reading and writing
# configuration values.
#
Expand Down Expand Up @@ -44,10 +44,36 @@ def self.method_missing sym, *args
super
end

def initialize(config = nil)
@config = config
end

def config
@config || self.class.config
end

def redis
config.redis
end

def reset!
@root_directory = nil
end

def method_missing sym, *args
root_directory.send(sym, *args)
rescue PathComponent::MissingTemplate
super
end

private

def self.root_directory
@root_directory ||= PathComponent.new(config.script_path)
end

def root_directory
@root_directory ||= PathComponent.new(config.script_path, redis)
end

end
2 changes: 1 addition & 1 deletion lib/wolverine/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Wolverine
class Wolverine
class Configuration < Struct.new(:redis, :script_path, :instrumentation)

# @return [Redis] the redis connection actively in use by Wolverine
Expand Down
2 changes: 1 addition & 1 deletion lib/wolverine/lua_error.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Wolverine
class Wolverine
# Reformats errors raised by redis representing failures while executing
# a lua script. The default errors have confusing messages and backtraces,
# and a type of +RuntimeError+. This class improves the message and
Expand Down
10 changes: 6 additions & 4 deletions lib/wolverine/path_component.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Wolverine
class Wolverine
# A {PathComponent} represents either the +Wolverine.config.script_path+
# directory, or a subdirectory of it. Calling (nearly) any method on it will
# cause it to look in the filesystem at the location it refers to for a file
Expand All @@ -14,8 +14,10 @@ class PathComponent
class MissingTemplate < StandardError ; end

# @param path [Pathname] full path to the current file or directory
def initialize path
# @param redis [Redis]
def initialize path, redis = nil
@path = path
@redis = redis || Wolverine.redis
end

# @param sym [Symbol] the file or directory to look up and execute
Expand Down Expand Up @@ -49,14 +51,14 @@ def file?(path)
end

def define_directory_method path, sym
dir = PathComponent.new(path)
dir = PathComponent.new(path, @redis)
define_metaclass_method(sym) { dir }
end

def define_script_method path, sym, *args
script = Wolverine::Script.new(path)
define_metaclass_method(sym) { |*args|
script.call(Wolverine.redis, *args)
script.call(@redis, *args)
}
end

Expand Down
2 changes: 1 addition & 1 deletion lib/wolverine/script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'benchmark'
require 'digest/sha1'

module Wolverine
class Wolverine
# {Script} represents a lua script in the filesystem. It loads the script
# from disk and handles talking to redis to execute it. Error handling
# is handled by {LuaError}.
Expand Down
2 changes: 1 addition & 1 deletion lib/wolverine/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Wolverine
class Wolverine
VERSION = "0.2.3"
end
12 changes: 10 additions & 2 deletions test/integration/wolverine_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ def mock_redis
stub.tap do |redis|
redis.expects(:evalsha).
with('fe24f4dd4ba7881608cca4b846f009195e06d79a', 2, :a, :b).
raises("NOSCRIPT")
raises("NOSCRIPT").once
redis.expects(:eval).
with(CONTENT, 2, :a, :b).
returns([1, 0])
returns([1, 0]).once
end
end

Expand All @@ -22,4 +22,12 @@ def test_everything
assert_equal [1, 0], Wolverine.util.mexists(:a, :b)
end

def test_everything_instantiated
script_path = Pathname.new(File.expand_path('../lua', __FILE__))
config = Wolverine::Configuration.new(mock_redis, script_path)

wolverine = Wolverine.new(config)
assert_equal [1, 0], wolverine.util.mexists(:a, :b)
end

end
2 changes: 1 addition & 1 deletion test/wolverine/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def self.root
end
end

module Wolverine
class Wolverine
class ConfigurationTest < MiniTest::Unit::TestCase

def test_default_redis
Expand Down
2 changes: 1 addition & 1 deletion test/wolverine/path_component_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require File.join(File.expand_path('../../test_helper', __FILE__))

module Wolverine
class Wolverine
class PathComponentTest < MiniTest::Unit::TestCase

def root
Expand Down
2 changes: 1 addition & 1 deletion test/wolverine/script_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require File.join(File.expand_path('../../test_helper', __FILE__))
require 'digest/sha1'

module Wolverine
class Wolverine
class ScriptTest < MiniTest::Unit::TestCase
CONTENT = "return 1"
DIGEST = Digest::SHA1.hexdigest(CONTENT)
Expand Down
10 changes: 10 additions & 0 deletions test/wolverine_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,14 @@ def test_reset!
refute_equal Wolverine.root_directory, dir
end

def test_instantiate_wolverine_with_config
r = Struct.new(:Redis)
config = Wolverine::Configuration.new(r, 'path')
wolverine = Wolverine.new(config)

assert_equal r, wolverine.config.redis
assert_equal r, wolverine.redis
assert_equal 'path', wolverine.config.script_path
end

end

0 comments on commit 47a9a38

Please sign in to comment.