Class: Wolflow::ExclusiveChoice

Inherits:
MultiChoice show all
Defined in:
lib/wolflow/exclusive_choice.rb

Direct Known Subclasses

StructuredLoop

Instance Attribute Summary collapse

Attributes inherited from MultiChoice

#condition_next_tasks

Attributes inherited from TaskSpec

#connects_to, #id, #name, #prev_tasks, #workflow_spec

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MultiChoice

#build_next_tasks, #connect

Methods inherited from TaskSpec

#child_of?, #each_ancestor, #each_child, #each_parent, #each_successor, inherited

Constructor Details

#initialize(else_tasks: [], **kwargs) ⇒ ExclusiveChoice

Returns a new instance of ExclusiveChoice.



7
8
9
10
11
12
13
14
15
16
# File 'lib/wolflow/exclusive_choice.rb', line 7

def initialize(else_tasks: [], **kwargs)
  super(**kwargs)
  @else_tasks = Array(else_tasks)

  @else_tasks.each do |task_spec|
    task_spec.prev_tasks << self

    @workflow_spec ||= task_spec.workflow_spec
  end
end

Instance Attribute Details

#else_tasksObject (readonly)

Returns the value of attribute else_tasks.



5
6
7
# File 'lib/wolflow/exclusive_choice.rb', line 5

def else_tasks
  @else_tasks
end

Class Method Details

.from_hash(hash) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/wolflow/exclusive_choice.rb', line 87

def from_hash(hash)
  case hash
  in {
      condition_next_tasks: [[{ type: String, **}, [*, String, *]]] => cond_tasks,
      else_tasks: [*, String, *] => else_tasks,
      **args
     }
    spec = super(**args)
    spec.connects_to = {
      condition_next_tasks: cond_tasks,
      else_tasks: else_tasks
    }
    spec
  else
    super
  end
end

Instance Method Details

#connect_else(*task_specs) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/wolflow/exclusive_choice.rb', line 34

def connect_else(*task_specs)
  task_specs.each do |task_spec|
    task_spec.prev_tasks << self
    @else_tasks << task_spec

    @workflow_spec ||= task_spec.workflow_spec
  end

  return task_specs.first if task_specs.size <= 1

  task_specs
end

#connects_with(tasks) ⇒ Object

private-ish



79
80
81
82
83
84
# File 'lib/wolflow/exclusive_choice.rb', line 79

def connects_with(tasks)
  return unless @connects_to

  connect_else(*@connects_to.fetch(:else_tasks).map { |id| tasks[id] })
  super
end

#inspectObject



61
62
63
64
65
66
# File 'lib/wolflow/exclusive_choice.rb', line 61

def inspect
  "<#{self.class}:#{hash} " \
    "@id=#{@id} " \
    "@condition_next_tasks=#{@condition_next_tasks.map { |k, t| [k, t.map(&:id)] }} " \
    "@else_tasks=#{@else_tasks.map(&:id)}>"
end

#next_tasksObject



18
19
20
# File 'lib/wolflow/exclusive_choice.rb', line 18

def next_tasks
  [*super, *@else_tasks]
end

#on_complete(task) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/wolflow/exclusive_choice.rb', line 22

def on_complete(task)
  task.mark_as_complete!

  @condition_next_tasks.each do |cond, next_tasks|
    next unless cond.call(task)

    predict(task, next_tasks)
    return # rubocop:disable Lint/NonLocalExitFromIterator
  end
  predict(task, @else_tasks)
end

#precedes?(task_spec) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/wolflow/exclusive_choice.rb', line 47

def precedes?(task_spec)
  @else_tasks.include?(task_spec) || super
end

#to_hashObject



51
52
53
54
55
# File 'lib/wolflow/exclusive_choice.rb', line 51

def to_hash
  super.merge(
    else_tasks: @else_tasks.map(&:id)
  )
end

#to_hash_treeObject



57
58
59
# File 'lib/wolflow/exclusive_choice.rb', line 57

def to_hash_tree
  [*super, *@else_tasks.flat_map(&:to_hash_tree)]
end

#workflow_spec=(workflow_spec) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/wolflow/exclusive_choice.rb', line 68

def workflow_spec=(workflow_spec)
  return if workflow_spec == @workflow_spec

  super

  @else_tasks.each do |task_spec|
    task_spec.workflow_spec = workflow_spec
  end
end