Skip to content

Instantly share code, notes, and snippets.

@hassox
Forked from kennethkalmer/gist:638269
Created October 21, 2010 11:55
Show Gist options
  • Save hassox/638362 to your computer and use it in GitHub Desktop.
Save hassox/638362 to your computer and use it in GitHub Desktop.
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
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
Ruote.process_defition :name => 'Hello world' do
cursor :break_if => "${f:error}" do
foo :command => '/verify/something'
bar :command => '/report'
end
end
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