Skip to content
Browse files

Autorequire handlers from project and home folders

Halders are to be found inside these paths:
  ./.hijacker/<my_handler>.rb (Project folder)
  ~/.hijacker/<my_handler>.rb (Home folder)
  • Loading branch information...
1 parent 3def1e4 commit c77b4fb1c95c5f97a5f108ed0ba96e49ead4732b @txus committed Nov 28, 2010
Showing with 38 additions and 9 deletions.
  1. +11 −4 lib/hijacker/handler.rb
  2. +27 −5 spec/hijacker/handler_spec.rb
View
15 lib/hijacker/handler.rb
@@ -29,9 +29,9 @@ def handle(method, args, retval, object)
# args [{:inspect => '3', :class => 'Fixnum'},
# {:inspect => '"string"', :class => 'String'}]
#
- # retval [{:inspect => ':bar', :class => 'Symbol'}]
+ # retval {:inspect => ':bar', :class => 'Symbol'}
#
- # object [{:inspect => '#<MyClass:0x003457>', :class => 'MyClass'}]
+ # object {:inspect => '#<MyClass:0x003457>', :class => 'MyClass'}
#
raise NotImplementedError.new("You are supposed to subclass Handler")
end
@@ -52,7 +52,14 @@ def handlers
end
end
-# Automatically load all handlers
-Dir[File.dirname(File.join(File.dirname(__FILE__), 'handlers', '**', '*.rb'))].entries.each do |handler|
+# Automatically load all handlers in the following paths:
+#
+# ./.hijacker/**/*.rb
+# ~/.hijacker/**/*.rb
+# lib/handlers/**/*.rb
+#
+(Dir[File.dirname(File.join(Dir.pwd, '.hijacker', '**', '*.rb'))] + \
+Dir[File.dirname(File.expand_path(File.join('~', '.hijacker', '**', '*.rb')))] + \
+Dir[File.dirname(File.join(File.dirname(__FILE__), 'handlers', '**', '*.rb'))]).entries.each do |handler|
require(handler) && Hijacker::Handler.register_handler(handler)
end
View
32 spec/hijacker/handler_spec.rb
@@ -9,8 +9,13 @@ module Hijacker
subject.opts.should == {:my => :option}
end
- describe "#handle" do
+ it { should respond_to(:opts)}
+
+ it "includes DRb::DRbUndumpled" do
+ Handler.included_modules.should include(DRb::DRbUndumped)
+ end
+ describe "#handle" do
let(:args) do
[:bar,
[
@@ -31,12 +36,29 @@ module Hijacker
describe "class methods" do
describe "#register_handler" do
- it 'registers a loaded handler' do
- Hijacker::Handler.register_handler "/path/to/my/handlers/benchmark.rb"
- Hijacker::Handler.handlers.should include('benchmark')
+ context "when given a relative path" do
+ it 'registers that file as a handler' do
+ Hijacker::Handler.register_handler "/path/to/my/handlers/benchmark.rb"
+ Hijacker::Handler.handlers.should include('benchmark')
+ end
+ end
+ context "when given an absolute path" do
+ it 'registers that file as well' do
+ Hijacker::Handler.register_handler "~/.handlers/custom_handler.rb"
+ Hijacker::Handler.handlers.should include('custom_handler')
+ end
end
end
-
+
+ describe "#handlers" do
+ it 'is an accessor to the class variable' do
+ handlers = [double('handler'),
+ double('another handler')]
+ Handler.class_variable_set(:@@handlers, handlers)
+ Handler.handlers.should == handlers
+ end
+ end
+
end
end

0 comments on commit c77b4fb

Please sign in to comment.
Something went wrong with that request. Please try again.