Class: Ruote::BlockParticipant

Inherits:
Object
  • Object
show all
Includes:
LocalParticipant
Defined in:
lib/ruote/part/block_participant.rb

Overview

One of the simplest participants. Simply passes a workitem to a block of ruby code.

engine.register_participant :alpha do |workitem|
  workitem.fields['time'] = Time.now
end

This participant implicitely replies to the engine when the block execution is over.

You can pass the flow_expression (participant expression) as well.

engine.register_participant :alpha do |workitem, flow_exp|
  workitem.fields['amount'] = flow_exp.lookup_variable('amount')
end

do_not_thread

By default, this participant (like most other participants) is executed in its own thread.

context

As it includes Ruote::LocalParticipant, the block partitcipant has access to:

  • #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‚Ķ

Instance Attribute Summary collapse

Attributes included from LocalParticipant

#error, #fei, #flavour, #msg, #workitem

Instance Method Summary collapse

Methods included from LocalParticipant

#_accept?, #_dont_thread?, #_on_cancel, #_on_reply, #_on_workitem, #_rtimeout, #applied_workitem, #fexp, #is_cancelled?, #is_gone?, #lookup_variable, #participant_name, #re_dispatch, #reply_to_engine, #unschedule_re_dispatch

Methods included from ReceiverMixin

#fetch_flow_expression, #fetch_workitem, #flunk, #launch, #receive, #reply, #sign

Constructor Details

#initialize(opts) ⇒ BlockParticipant

Returns a new instance of BlockParticipant


70
71
72
73
# File 'lib/ruote/part/block_participant.rb', line 70

def initialize(opts)

  @opts = opts
end

Instance Attribute Details

#contextObject

Returns the value of attribute context


68
69
70
# File 'lib/ruote/part/block_participant.rb', line 68

def context
  @context
end

Instance Method Details

#accept?(workitem) ⇒ Boolean

Returns:

  • (Boolean)

110
111
112
113
114
115
116
117
# File 'lib/ruote/part/block_participant.rb', line 110

def accept?(workitem)

  if block = get_block('accept?')
    block.call(workitem)
  else
    true
  end
end

#cancel(fei, flavour) ⇒ Object


96
97
98
99
100
101
# File 'lib/ruote/part/block_participant.rb', line 96

def cancel(fei, flavour)

  if block = get_block('on_cancel')
    block.call(fei, flavour)
  end
end

#do_not_thread(workitem) ⇒ Object


119
120
121
122
123
124
125
126
127
128
# File 'lib/ruote/part/block_participant.rb', line 119

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

#on_reply(workitem) ⇒ Object


103
104
105
106
107
108
# File 'lib/ruote/part/block_participant.rb', line 103

def on_reply(workitem)

  if block = get_block('on_reply')
    block.call(workitem)
  end
end

#on_workitemObject


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ruote/part/block_participant.rb', line 75

def on_workitem

  block = get_block('on_workitem', 'block')

  r = if block.arity == 1

    block.call(workitem)

  else

    block.call(
      workitem, Ruote::Exp::FlowExpression.fetch(@context, workitem.h.fei))
  end

  if r != nil && r != workitem
    workitem.result = (Rufus::Json.dup(r) rescue nil)
  end

  reply_to_engine(workitem)
end