-
-
Save hassox/638362 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BaseParticipant | |
include Ruote::LocalParticipant | |
def consume( workitem ) | |
@workitem = workitem | |
begin | |
_, method, *args = workitem.fields['command'].split('/') | |
if arg | |
send( method, *args ) | |
else | |
send( method ) | |
end | |
rescue => e | |
@workitem.fields['error'] = e | |
reply_to_engine( @workitem ) | |
end | |
end | |
def done( message ) | |
@workitem.fields['report'].push message | |
reply_to_engine( @workitem ) | |
end | |
def failed( message ) | |
@workitem.fields['report'].push message | |
@workitem.fields['error'] = true | |
reply_to_engine( @workitem ) | |
end | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe BaseParticipant do | |
subject { BaseParticipant } | |
before do | |
@base = subject.new | |
end | |
def workitem(opts = {}) | |
items = { | |
'report' => [] | |
}.merge(opts) | |
Ruote::Workitem.new('fields' => items) | |
end | |
it "should check dispatch to a method from a path" do | |
item = workitem('command' => '/test_one') | |
@base.should_receive(:test_one) | |
@base.consume(item) | |
end | |
it "should recieve the method with an argument" do | |
item = workitem('command' => '/test_two/foo') | |
@base.should_receive(:test_two).with('foo') | |
@base.consume(item) | |
end | |
it "should recieve the method with multiple arguments" do | |
item = workitem('command' => '/test_three/foo/bar') | |
@base.should_receive(:test_three).with('foo', 'bar') | |
@base.consume(item) | |
end | |
it "should reply to the engine when it's done" do | |
item = workitem('command' => '/foo') | |
@base.stub!(:foo) | |
@base.should_receive(:reply_to_engine).with(item) | |
@base.consume(item) | |
@base.done("foo") | |
item.fields['report'].should include('foo') | |
end | |
it "should register a fail" do | |
item = workitem('command' => '/foo') | |
@base.stub!(:foo) | |
@base.should_receive(:reply_to_engine).with(item) | |
@base.consume(item) | |
@base.fail('you fail') | |
item.fields['report'].should include('you fail') | |
item.fields['error'].should be_true | |
end | |
it 'should send an error back to the engine when an action raises' do | |
item = workitem('command' => '/die') | |
def @base.die | |
raise "dead" | |
end | |
@base.should_receive(:reply_to_engine).with(item) | |
@base.consume item | |
item.fields['error'].should be_present | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ruote.process_defition :name => 'Hello world' do | |
cursor :break_if => "${f:error}" do | |
foo :command => '/verify/something' | |
bar :command => '/report' | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo < BaseParticipant | |
def verify( something ) | |
# Do something | |
done "It worked" | |
end | |
end | |
class Bar < BaseParticipant | |
def report | |
# Oh well | |
failed "No way I'm doing this" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment