lib/ruote/part/block_participant.rb in ruote-2.2.0 vs lib/ruote/part/block_participant.rb in ruote-2.3.0

- old
+ new

@@ -1,7 +1,7 @@ #-- -# Copyright (c) 2005-2011, John Mettraux, jmettraux@gmail.com +# Copyright (c) 2005-2012, John Mettraux, jmettraux@gmail.com # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell @@ -20,13 +20,11 @@ # THE SOFTWARE. # # Made in Japan. #++ -require 'ruote/part/local_participant' - module Ruote # # One of the simplest participants. Simply passes a workitem to a block # of ruby code. @@ -46,23 +44,24 @@ # # # == do_not_thread # # By default, this participant (like most other participants) is executed - # in its own thread (in a Ruby runtime where EventMachine is running, - # EM.next_tick is used instead of a new thread). + # in its own thread. # - # You can change that behaviour (beware block thats monopolises the whole - # engine !) by doing # - # alpha = engine.register_participant :alpha do |workitem| - # workitem.fields['time'] = Time.now - # end + # == context # - # alpha.do_not_thread = true + # As it includes Ruote::LocalParticipant, the block partitcipant has + # access to: # - # (you could also override do_not_thread, the method ...) + # * #context: the ruote context + # * #workitem: the current workitem (usually passed as arg to the block) + # * #fei: the current flow expression id + # * #fexp: the current flow expression + # * #flavour: only used in #on_cancel (nil or 'kill') + # * #lookup_variable(key): looks up a variable... # class BlockParticipant include LocalParticipant @@ -71,30 +70,18 @@ def initialize(opts) @opts = opts end - def do_not_thread + def on_workitem - @opts['do_not_thread'] - end + block = get_block('on_workitem', 'block') - def consume(workitem) - - block = @opts['block'] - - @context.treechecker.block_check(block) - # raises in case of 'security' violation - - #block = eval(block, @context.send(:binding)) - # doesn't work with ruby 1.9.2-p136 - block = eval(block, @context.instance_eval { binding }) - # works OK with ruby 1.8.7-249 and 1.9.2-p136 - r = if block.arity == 1 block.call(workitem) + else block.call( workitem, Ruote::Exp::FlowExpression.fetch(@context, workitem.h.fei)) end @@ -106,10 +93,60 @@ reply_to_engine(workitem) end def cancel(fei, flavour) - # do nothing + if block = get_block('on_cancel') + block.call(fei, flavour) + end + end + + def on_reply(workitem) + + if block = get_block('on_reply') + block.call(workitem) + end + end + + def accept?(workitem) + + if block = get_block('accept?') + block.call(workitem) + else + true + end + end + + def do_not_thread(workitem) + + dnt = @opts['do_not_thread'] + + return dnt unless dnt.is_a?(String) + + block = get_block('do_not_thread') + + block.call(workitem) + end + + protected + + def get_block(*keys) + + key = keys.find { |k| @opts[k] } + + return nil unless key + + block = @opts[key] + + @context.treechecker.block_check(block) + # raises in case of 'security' violation + + #eval(block, @context.send(:binding)) + # doesn't work with ruby 1.9.2-p136 + #eval(block, @context.instance_eval { binding }) + # works OK with ruby 1.8.7-249 and 1.9.2-p136 + eval(block, self.instance_eval { binding }) + # works OK with ruby 1.8.7-249 and 1.9.2-p136 end end end