Class: Wolflow::Simple

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

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(next_tasks: [], **kwargs) ⇒ Simple

Returns a new instance of Simple.



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

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

Instance Attribute Details

#next_tasksObject (readonly)

Returns the value of attribute next_tasks.



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

def next_tasks
  @next_tasks
end

Class Method Details

.from_hash(hash) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/wolflow/simple.rb', line 122

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

Instance Method Details

#build_next_tasks(task, i: 0, next_tasks: @next_tasks) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/wolflow/simple.rb', line 91

def build_next_tasks(task, i: 0, next_tasks: @next_tasks)
  children = next_tasks.map do |next_task|
    id = next_task.id
    id = "#{id}_#{i}" unless i.zero?
    task.class.new(
      id: id,
      task_spec: next_task,
      workflow: task.workflow,
      root: task.root
    )
  end
  task.connect(*children)
  children.each do |child|
    child.task_spec.build_next_tasks(child, i: i)
  end
end

#choose(*else_task_specs, **args) ⇒ Object



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

def choose(*else_task_specs, **args)
  choice = ExclusiveChoice.new(workflow_spec: @workflow_spec, else_tasks: else_task_specs, **args)
  next_tasks << choice
  choice.prev_tasks << self

  if block_given?
    yield(choice, *else_task_specs)
    return self
  end

  [choice, *else_task_specs]
end

#connect(*task_specs) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/wolflow/simple.rb', line 18

def connect(*task_specs)
  task_specs.each do |task_spec|
    connect_one(task_spec)
  end

  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



81
82
83
84
85
86
87
88
89
# File 'lib/wolflow/simple.rb', line 81

def connects_with(tasks)
  return unless @connects_to

  @connects_to.each do |id|
    connect(tasks.fetch(id))
  end

  @connects_to = nil
end

#inspectObject



64
65
66
67
68
# File 'lib/wolflow/simple.rb', line 64

def inspect
  "#<#{self.class}:#{hash} " \
    "@id=#{@id} " \
    "@next_tasks=#{@next_tasks.map(&:id)}>"
end

#on_complete(task) ⇒ Object



12
13
14
15
16
# File 'lib/wolflow/simple.rb', line 12

def on_complete(task)
  task.mark_as_complete!

  predict(task, @next_tasks)
end

#precedes?(task_spec) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/wolflow/simple.rb', line 46

def precedes?(task_spec)
  @next_tasks.include?(task_spec)
end

#to_hashObject



50
51
52
53
54
55
56
# File 'lib/wolflow/simple.rb', line 50

def to_hash
  {
    id: @id,
    name: @name,
    next_tasks: @next_tasks.map(&:id)
  }
end

#to_hash_treeObject



58
59
60
61
62
# File 'lib/wolflow/simple.rb', line 58

def to_hash_tree
  hash_tree = @next_tasks.flat_map(&:to_hash_tree)
  hash_tree.unshift(to_hash)
  hash_tree
end

#workflow_spec=(workflow_spec) ⇒ Object



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

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

  super

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