Class: Wolflow::MultiChoice

Inherits:
TaskSpec show all
Defined in:
lib/wolflow/multi_choice.rb

Direct Known Subclasses

ExclusiveChoice

Instance Attribute Summary collapse

Attributes inherited from TaskSpec

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TaskSpec

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

Constructor Details

#initialize(condition_next_tasks: [], **kwargs) ⇒ MultiChoice

Returns a new instance of MultiChoice.



7
8
9
10
# File 'lib/wolflow/multi_choice.rb', line 7

def initialize(condition_next_tasks: [], **kwargs)
  super(**kwargs)
  @condition_next_tasks = condition_next_tasks
end

Instance Attribute Details

#condition_next_tasksObject (readonly)

Returns the value of attribute condition_next_tasks.



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

def condition_next_tasks
  @condition_next_tasks
end

Class Method Details

.from_hash(hash) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/wolflow/multi_choice.rb', line 110

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

Instance Method Details

#build_next_tasks(task) ⇒ Object



40
# File 'lib/wolflow/multi_choice.rb', line 40

def build_next_tasks(task, **); end

#connect(condition, *task_specs) ⇒ Object

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/wolflow/multi_choice.rb', line 25

def connect(condition, *task_specs)
  raise ArgumentError, "condition must response to #call" unless condition.respond_to?(:call)

  connect_cond(condition, task_specs)

  if block_given?
    yield(*task_specs)
    return self
  end

  return task_specs.first if task_specs.size <= 1

  task_specs
end

#connects_with(tasks) ⇒ Object

private-ish



82
83
84
85
86
87
88
89
90
91
# File 'lib/wolflow/multi_choice.rb', line 82

def connects_with(tasks)
  return unless @connects_to

  @connects_to.fetch(:condition_next_tasks).each do |cond, ids|
    cond = Operators.from_hash(cond)
    connect(cond, *ids.map { |id| tasks[id] })
  end

  @connects_to = nil
end

#inspectObject



63
64
65
66
67
# File 'lib/wolflow/multi_choice.rb', line 63

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

#next_tasksObject



12
13
14
# File 'lib/wolflow/multi_choice.rb', line 12

def next_tasks
  @condition_next_tasks.flat_map { |_, tasks| tasks }
end

#on_complete(task) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/wolflow/multi_choice.rb', line 16

def on_complete(task)
  task.mark_as_complete!

  succ_next_tasks = @condition_next_tasks.filter_map do |cond, next_tasks|
    next_tasks if cond.call(task)
  end.flatten
  predict(task, succ_next_tasks)
end

#precedes?(task_spec) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/wolflow/multi_choice.rb', line 42

def precedes?(task_spec)
  @condition_next_tasks.one? { |_, tspecs| tspecs.include?(task_spec) }
end

#to_hashObject



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/wolflow/multi_choice.rb', line 46

def to_hash
  {
    id: @id,
    name: @name,
    condition_next_tasks: @condition_next_tasks.map do |condition, task_specs|
      condition_hash = Hash.try_convert(condition)
      raise Error, "can't convert #{condition} to a hash" unless condition_hash

      [condition_hash, task_specs.map(&:id)]
    end
  }
end

#to_hash_treeObject



59
60
61
# File 'lib/wolflow/multi_choice.rb', line 59

def to_hash_tree
  [to_hash, *@condition_next_tasks.flat_map { |_, ts| ts.flat_map(&:to_hash_tree) }]
end

#workflow_spec=(workflow_spec) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/wolflow/multi_choice.rb', line 69

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

  super

  @condition_next_tasks.each do |_, task_specs| # rubocop:disable Style/HashEachMethods
    task_specs.each do |task_spec|
      task_spec.workflow_spec = workflow_spec
    end
  end
end