Class: Conductor::Workflow::Dsl::SwitchBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/conductor/workflow/dsl/switch_builder.rb

Overview

SwitchBuilder collects switch cases defined in a decide block. It provides on() and otherwise() methods for defining case branches.

Examples:

decide user[:country] do
  on 'US' do
    simple :us_flow
  end
  on 'UK' do
    simple :uk_flow
  end
  otherwise do
    simple :default_flow
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expression, parent_builder) ⇒ SwitchBuilder

Returns a new instance of SwitchBuilder.



25
26
27
28
29
30
# File 'lib/conductor/workflow/dsl/switch_builder.rb', line 25

def initialize(expression, parent_builder)
  @expression = expression
  @parent = parent_builder
  @cases = {}
  @default = []
end

Instance Attribute Details

#casesObject (readonly)

Returns the value of attribute cases.



23
24
25
# File 'lib/conductor/workflow/dsl/switch_builder.rb', line 23

def cases
  @cases
end

#defaultObject (readonly)

Returns the value of attribute default.



23
24
25
# File 'lib/conductor/workflow/dsl/switch_builder.rb', line 23

def default
  @default
end

#expressionObject (readonly)

Returns the value of attribute expression.



23
24
25
# File 'lib/conductor/workflow/dsl/switch_builder.rb', line 23

def expression
  @expression
end

Instance Method Details

#on(value) { ... } ⇒ Object

Define a case branch

Parameters:

  • value (String, Symbol)

    The case value to match

Yields:

  • Block containing tasks for this case



35
36
37
38
39
40
# File 'lib/conductor/workflow/dsl/switch_builder.rb', line 35

def on(value, &block)
  tasks = []
  collector = TaskCollector.new(@parent, tasks)
  collector.instance_eval(&block)
  @cases[value.to_s] = tasks
end

#otherwise { ... } ⇒ Object

Define the default case (executed if no cases match)

Yields:

  • Block containing tasks for the default case



44
45
46
47
# File 'lib/conductor/workflow/dsl/switch_builder.rb', line 44

def otherwise(&block)
  collector = TaskCollector.new(@parent, @default)
  collector.instance_eval(&block)
end