diff --git a/lib/wolverine.rb b/lib/wolverine.rb index 5b7a111..8ff4718 100644 --- a/lib/wolverine.rb +++ b/lib/wolverine.rb @@ -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. # @@ -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 diff --git a/lib/wolverine/configuration.rb b/lib/wolverine/configuration.rb index e9f81b7..2fa1666 100644 --- a/lib/wolverine/configuration.rb +++ b/lib/wolverine/configuration.rb @@ -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 diff --git a/lib/wolverine/lua_error.rb b/lib/wolverine/lua_error.rb index 8c03c47..85f8215 100644 --- a/lib/wolverine/lua_error.rb +++ b/lib/wolverine/lua_error.rb @@ -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 diff --git a/lib/wolverine/path_component.rb b/lib/wolverine/path_component.rb index 2c46efe..ad0e05d 100644 --- a/lib/wolverine/path_component.rb +++ b/lib/wolverine/path_component.rb @@ -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 @@ -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 @@ -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 diff --git a/lib/wolverine/script.rb b/lib/wolverine/script.rb index 7721ecc..d3028fb 100644 --- a/lib/wolverine/script.rb +++ b/lib/wolverine/script.rb @@ -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}. diff --git a/lib/wolverine/version.rb b/lib/wolverine/version.rb index 3f20f06..0486192 100644 --- a/lib/wolverine/version.rb +++ b/lib/wolverine/version.rb @@ -1,3 +1,3 @@ -module Wolverine +class Wolverine VERSION = "0.2.3" end diff --git a/test/integration/wolverine_integration_test.rb b/test/integration/wolverine_integration_test.rb index ee2f57c..0a7cc8b 100644 --- a/test/integration/wolverine_integration_test.rb +++ b/test/integration/wolverine_integration_test.rb @@ -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 @@ -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 diff --git a/test/wolverine/configuration_test.rb b/test/wolverine/configuration_test.rb index f43612a..f80858b 100644 --- a/test/wolverine/configuration_test.rb +++ b/test/wolverine/configuration_test.rb @@ -7,7 +7,7 @@ def self.root end end -module Wolverine +class Wolverine class ConfigurationTest < MiniTest::Unit::TestCase def test_default_redis diff --git a/test/wolverine/path_component_test.rb b/test/wolverine/path_component_test.rb index 4cf9aa9..2bef256 100644 --- a/test/wolverine/path_component_test.rb +++ b/test/wolverine/path_component_test.rb @@ -1,6 +1,6 @@ require File.join(File.expand_path('../../test_helper', __FILE__)) -module Wolverine +class Wolverine class PathComponentTest < MiniTest::Unit::TestCase def root diff --git a/test/wolverine/script_test.rb b/test/wolverine/script_test.rb index fe193fb..11cd563 100644 --- a/test/wolverine/script_test.rb +++ b/test/wolverine/script_test.rb @@ -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) diff --git a/test/wolverine_test.rb b/test/wolverine_test.rb index c9b53dd..3028fb2 100644 --- a/test/wolverine_test.rb +++ b/test/wolverine_test.rb @@ -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