Class: Ruote::Exp::GivenExpression

Inherits:
SequenceExpression show all
Defined in:
lib/ruote/exp/fe_given.rb

Overview

This expressions corresponds to a “case” statement in Ruby or a “switch” in other languages.

It accepts two variants “given that” and “given an x of”.

'given' works in conjunction with the 'that' / 'of' expression.

“given that”

given do
  that "${location} == paris" do
    subprocess "notify_and_wait_for_pickup"
  end
  that "${state} == ready" do
    subprocess "deliver"
  end
  # else...
  subprocess "do_something_else"
end

“given an x of”

given "${status}" do
  of "ordered" do
    participant "alpha"
  end
  of "delivered" do
    participant "alpha"
  end
  # else...
  subprocess "do_something_else"
end

This variant also accepts regular expressions :

given "${target}" do
  of "/-manager$/" do
    # ...
  end
  of /^user-/ do
    # ...
  end
end

mixing 'that' and 'of'

It's OK to use a “that” inside a “given an x” :

given '${target}' do
  that "${location} == paris" do
    subprocess "notify_and_wait_for_pickup"
  end
  of "home" do
    subprocess "return_procedure"
  end
end

the else part

Anything that comes after the serie of 'that' and 'of' is considered in the 'else' zone and is executed if none of the 'that' or 'of' triggered.

given '${target}' do
  that "${location} == paris" do
    subprocess "notify_and_wait_for_pickup"
  end
  of "home" do
    subprocess "return_procedure"
  end
  subprocess "do_this"
  subprocess "and_then_that"
end

Yes, two 'else' subprocesses will get executed one after the other (the 'given' acting like a 'sequence' for them.

Interestingly :

given '${target}' do
  of "home" do
    subprocess "return_procedure"
  end
  subprocess "do_this"
  of "office" do
    subprocess "go_to_work"
  end
  subprocess "and_then_that"
end

If the workitem field 'target' is set to 'home' only the 'return_procedure' subprocess will get called.

If the workitem field 'target' is set to 'office', the 'do_this' subprocess, then the 'go_to_work' one will get called.

Constant Summary

Constants inherited from FlowExpression

FlowExpression::COMMON_ATT_KEYS

Instance Attribute Summary

Attributes inherited from FlowExpression

#context, #error

Instance Method Summary collapse

Methods inherited from SequenceExpression

#apply

Methods inherited from FlowExpression

#ancestor?, #applied_workitem, #att, #att_text, #attribute, #attribute_text, #attributes, #await, #cancel, #cancel_flanks, #cfei_at, #child_id, #child_ids, #compile_atts, #compile_variables, #debug_id, #deflate, #do, do_action, #do_apply, #do_cancel, #do_fail, #do_pause, #do_persist, #do_reply, #do_reply_to_parent, #do_resume, #do_unpersist, dummy, #fei, fetch, from_h, #handle_on_error, #has_attribute, #initial_persist, #initialize, #is_concurrent?, #iterative_var_lookup, #launch_sub, #lookup_val, #lookup_val_prefix, #lookup_variable, #name, names, #parent, #parent_id, #pause_on_apply, #persist_or_raise, #reply_to_parent, #root, #root_id, #set_variable, #tree, #tree_children, #try_persist, #try_unpersist, #unpersist_or_raise, #unset_variable, #update_tree, #variables, #wfid

Methods included from WithMeta

#class_def, included

Methods included from WithH

#h, #h=, included, #to_h

Constructor Details

This class inherits a constructor from Ruote::Exp::FlowExpression

Instance Method Details

#reply(workitem) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/ruote/exp/fe_given.rb', line 133

def reply(workitem)

  if given = attribute(:t) || attribute_text
    workitem['fields']['__given__'] = given
  end

  # as soon as one child says true, reply to the parent expression

  if workitem['fields']['__result__'].to_s == 'true'

    workitem['fields'].delete('__given__')
    workitem['fields'].delete('__result__')
    reply_to_parent(workitem)

  else

    super
  end
end