Permalink
Browse files

Add abstract Handler class and make Logger subclass it

  • Loading branch information...
1 parent 0dc8c0c commit 08ad4ae515f20c46dd0461dac2f58dbd01a73068 @txus committed Nov 21, 2010
View
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- hijacker (0.0.1)
+ hijacker (0.0.2)
trollop
GEM
View
@@ -1,7 +1,7 @@
require 'drb'
require 'trollop'
require 'hijacker/config'
-require 'hijacker/logger'
+require 'hijacker/handler'
module Hijacker
@@ -89,7 +89,7 @@ def register(method, args, retval, object, uri = nil)
object = {:inspect => object.inspect, :class => object.class.name}
server = DRbObject.new nil, (uri || self.drb_uri)
- server.log method, args, retval, object
+ server.handle method, args, retval, object
end
private
View
@@ -0,0 +1,34 @@
+module Hijacker
+ class Handler
+ # Make dRuby send Handler instances as dRuby references,
+ # not copies.
+ include DRb::DRbUndumped
+
+ attr_reader :opts
+
+ def initialize(opts)
+ @opts = opts
+ end
+
+ def handle(method, args, retval, object)
+ # Parameters received
+ #
+ # method :foo
+ #
+ # args [{:inspect => '3', :class => 'Fixnum'},
+ # {:inspect => '"string"', :class => 'String'}]
+ #
+ # retval [{:inspect => ':bar', :class => 'Symbol'}]
+ #
+ # object [{:inspect => '#<MyClass:0x003457>', :class => 'MyClass'}]
+ #
+ raise NotImplementedError.new("You are supposed to subclass Handler")
+ end
+
+ end
+end
+
+# Automatically load all handlers
+Dir[File.dirname(File.join(File.dirname(__FILE__), 'handlers', '**', '*.rb'))].entries.each do |handler|
+ require handler
+end
@@ -1,5 +1,5 @@
module Hijacker
- class Logger
+ class Logger < Handler
ANSI = {:RESET=>"\e[0m", :BOLD=>"\e[1m", :UNDERLINE=>"\e[4m",
:LGRAY=>"\e[0;37m", :GRAY=>"\e[1;30m",
@@ -10,18 +10,8 @@ class Logger
:PURPLE=>"\e[35m", :LPURPLE=>"\e[1;35m",
:CYAN=>"\e[36m", :LCYAN=>"\e[1;36m",
:WHITE=>"\e[37m"}
-
- # Make dRuby send Logger instances as dRuby references,
- # not copies.
- include DRb::DRbUndumped
- attr_reader :opts
-
- def initialize(opts)
- @opts = opts
- end
-
- def log(method, args, retval, object)
+ def handle(method, args, retval, object)
out = []
out << ANSI[:BOLD] + ANSI[:UNDERLINE] + "#{Time.now}" unless opts[:without_timestamps]
out << ANSI[:CYAN] + object[:inspect]
@@ -0,0 +1,32 @@
+require 'spec_helper'
+
+module Hijacker
+ describe Handler do
+
+ subject { Handler.new({:my => :option}) }
+
+ it "initializes with options" do
+ subject.opts.should == {:my => :option}
+ end
+
+ describe "#handle" do
+
+ let(:args) do
+ [:bar,
+ [
+ {:inspect => "2", :class => "Fixnum"},
+ {:inspect => "\"string\"", :class => "String"},
+ ],
+ {:inspect => "\"retval\"", :class => "String"},
+ {:inspect => "MyClass", :class => "Class"}]
+ end
+
+ it "is meant to be overriden by subclasses" do
+ expect {
+ subject.handle(*args)
+ }.to raise_error NotImplementedError
+ end
+ end
+
+ end
+end
@@ -9,7 +9,11 @@ module Hijacker
subject.opts.should == {:my => :option}
end
- describe "#log" do
+ it "inherits from Handler" do
+ subject.should be_kind_of(Handler)
+ end
+
+ describe "#handle" do
let(:args) do
[:bar,
@@ -27,7 +31,7 @@ module Hijacker
Time.stub(:now).and_return Time.parse('2010-11-20')
- subject.log(*args)
+ subject.handle(*args)
["00:00:00 +0100",
"MyClass",
@@ -54,7 +58,7 @@ module Hijacker
Time.stub(:now).and_return Time.parse('2010-11-20')
- logger.log(*args)
+ logger.handle(*args)
out.string.should_not include("2010-11-20")
end
@@ -68,7 +72,7 @@ module Hijacker
Time.stub(:now).and_return Time.parse('2010-11-20')
- logger.log(*args)
+ logger.handle(*args)
["(Class)", "(Fixnum)", "(String)"].each do |str|
out.string.should_not include(str)
View
@@ -137,13 +137,13 @@ def object.my_method_with_args(a,b)
]
DRbObject.should_receive(:new).with(nil, "druby://localhost:9999").and_return server
- server.should_receive(:log).with *expected_args
+ server.should_receive(:handle).with *expected_args
Hijacker.register(:bar, [2, "string"], "retval", MyClass)
end
context "when given a particular DRb uri" do
it "sends the call to that uri" do
- DRbObject.should_receive(:new).with(nil, "druby://localhost:1212").and_return mock('DRb server', :log => true)
+ DRbObject.should_receive(:new).with(nil, "druby://localhost:1212").and_return mock('DRb server', :handle => true)
Hijacker.register(:bar, [2, "string"], "retval", MyClass, "druby://localhost:1212")
end
end

0 comments on commit 08ad4ae

Please sign in to comment.