Class: Wolflow::Workflow

Inherits:
Object
  • Object
show all
Defined in:
lib/wolflow/workflow.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workflow_spec:, id: object_id, root: Task.new(task_spec: workflow_spec.start, workflow: self), data: {}) ⇒ Workflow

Returns a new instance of Workflow.

Raises:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/wolflow/workflow.rb', line 7

def initialize(
  workflow_spec:,
  id: object_id,
  root: Task.new(task_spec: workflow_spec.start, workflow: self),
  data: {}
)
  @id = id
  @workflow_spec = workflow_spec
  @data = data

  raise Error, "#{root} has no task spec" unless root.task_spec

  if root.task_spec && @workflow_spec != root.task_spec.workflow_spec
    raise Error, "#{root} is from a different workflow spec"
  end

  @root = root
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#rootObject (readonly)

Returns the value of attribute root.



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

def root
  @root
end

#workflow_specObject (readonly)

Returns the value of attribute workflow_spec.



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

def workflow_spec
  @workflow_spec
end

Class Method Details

.from_hash(hash) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/wolflow/workflow.rb', line 55

def from_hash(hash)
  workflow_spec = WorkflowSpec.from_hash(hash[:workflow_spec])

  # @type var tasks: Hash[String, Task]
  tasks = {}

  hash[:tasks].each do |task_hash|
    task_spec_id = task_hash.delete(:task_spec_id)
    task_hash[:task_spec] = workflow_spec.each.find do |task_spec|
      task_spec.id == task_spec_id
    end || raise(Error, "no task spec found for `#{task_spec_id}`)")
    task = Task.from_hash(task_hash)
    tasks[task.id] = task
  end

  tasks.each_value do |task|
    task.connects_with(tasks)
  end

  start_task = tasks.each_value.find do |task|
    task.parents.empty?
  end or raise WorkflowError, "no start task found"

  new(id: hash[:id], root: start_task, workflow_spec: workflow_spec, data: hash[:data])
end

Instance Method Details

#complete_allObject



41
42
43
# File 'lib/wolflow/workflow.rb', line 41

def complete_all
  while complete_one do; end
end

#complete_oneObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/wolflow/workflow.rb', line 30

def complete_one
  each do |task|
    next if task.completed?

    task.complete!

    return task if task.completed?
  end
  nil
end

#eachObject



26
27
28
# File 'lib/wolflow/workflow.rb', line 26

def each(&)
  @root.each(&)
end

#to_hashObject



45
46
47
48
49
50
51
52
# File 'lib/wolflow/workflow.rb', line 45

def to_hash
  {
    id: @id,
    workflow_spec: @workflow_spec.to_hash,
    tasks: each.map(&:to_hash),
    data: @data.to_hash
  }
end