Class: Wolflow::WorkflowSpec

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: object_id.to_s, start: Start.new(workflow_spec: self)) ⇒ WorkflowSpec

Returns a new instance of WorkflowSpec.



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

def initialize(id: object_id.to_s, start: Start.new(workflow_spec: self))
  @id = id
  @start = start
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#startObject (readonly)

Returns the value of attribute start.



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

def start
  @start
end

Class Method Details

.from_hash(hash) ⇒ Object



53
54
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_spec.rb', line 53

def from_hash(hash)
  workflow_spec = new(id: hash[:id], start: nil)

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

  hash[:task_specs].each do |task_hash|
    raise Error, "no :name found" unless task_hash.key?(:name)

    cls = TaskSpec.spec_types[task_hash[:name]]
    task_hash[:workflow_spec] = workflow_spec
    task_spec = cls.from_hash(task_hash)
    tasks[task_spec.id] = task_spec
  end

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

  start_task = tasks.each_value.find do |spec|
    spec.prev_tasks.empty?
  end or raise WorkflowSpecError, "no start task found"

  workflow_spec.connect(start_task)

  workflow_spec
end

Instance Method Details

#connect(*task_specs) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/wolflow/workflow_spec.rb', line 12

def connect(*task_specs)
  if @start
    @start.connect(*task_specs)
  else
    start_task = task_specs.first
    if start_task.workflow_spec && start_task.workflow_spec != self
      raise TaskSpecError, "#{start_task} already defined in a workflow spec"
    end

    start_task.workflow_spec = self
    @start = start_task
  end

  if block_given?
    yield(*task_specs)
    return self
  end

  return task_specs.first if task_specs.size <= 1

  task_specs
end

#each(&blk) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/wolflow/workflow_spec.rb', line 35

def each(&blk)
  return unless @start

  return enum_for(__method__) unless blk

  blk.call(@start)

  @start.each_successor([self], &blk)
end

#to_hashObject



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

def to_hash
  {
    id: @id,
    task_specs: @start ? @start.to_hash_tree.uniq : []
  }
end