Class: Ductwork::DSL::DefinitionBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/ductwork/dsl/definition_builder.rb

Defined Under Namespace

Classes: CollapseError, CombineError, StartError

Instance Method Summary collapse

Constructor Details

#initializeDefinitionBuilder

Returns a new instance of DefinitionBuilder.



10
11
12
13
14
15
16
17
# File 'lib/ductwork/dsl/definition_builder.rb', line 10

def initialize
  @definition = {
    nodes: [],
    edges: {},
  }
  @divergences = []
  @last_nodes = []
end

Instance Method Details

#chain(klass) ⇒ Object

NOTE: there is a bug here that does not allow the user to reuse step classes in the same pipeline. i’ll fix this later



29
30
31
32
33
34
35
36
# File 'lib/ductwork/dsl/definition_builder.rb', line 29

def chain(klass)
  validate_classes!(klass)
  validate_definition_started!(action: "chaining")
  add_edge_to_last_nodes(klass, type: :chain)
  add_new_nodes(klass)

  self
end

#collapse(into:) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ductwork/dsl/definition_builder.rb', line 90

def collapse(into:)
  validate_classes!(into)
  validate_definition_started!(action: "collapsing steps")
  validate_can_collapse!
  add_edge_to_last_nodes(into, type: :collapse)
  add_new_nodes(into)

  divergences.pop

  self
end

#combine(into:) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ductwork/dsl/definition_builder.rb', line 58

def combine(into:)
  validate_classes!(into)
  validate_definition_started!(action: "combining steps")
  validate_can_combine!

  divergences.pop

  last_nodes = definition[:nodes].reverse.select do |node|
    definition[:edges][node].empty?
  end
  last_nodes.each do |node|
    definition[:edges][node] << {
      to: [into.name],
      type: :combine,
    }
  end
  add_new_nodes(into)

  self
end

#completeObject



112
113
114
115
116
# File 'lib/ductwork/dsl/definition_builder.rb', line 112

def complete
  validate_definition_started!(action: "completing")

  definition
end

#divide(to:) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ductwork/dsl/definition_builder.rb', line 38

def divide(to:)
  validate_classes!(to)
  validate_definition_started!(action: "dividing chain")
  add_edge_to_last_nodes(*to, type: :divide)
  add_new_nodes(*to)

  divergences.push(:divide)

  if block_given?
    branches = to.map do |klass|
      Ductwork::DSL::BranchBuilder
        .new(klass:, definition:)
    end

    yield branches
  end

  self
end

#expand(to:) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/ductwork/dsl/definition_builder.rb', line 79

def expand(to:)
  validate_classes!(to)
  validate_definition_started!(action: "expanding chain")
  add_edge_to_last_nodes(to, type: :expand)
  add_new_nodes(to)

  divergences.push(:expand)

  self
end

#on_halt(klass) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/ductwork/dsl/definition_builder.rb', line 102

def on_halt(klass)
  validate_classes!(klass)

  definition[:metadata] ||= {}
  definition[:metadata][:on_halt] = {}
  definition[:metadata][:on_halt][:klass] = klass.name

  self
end

#start(klass) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/ductwork/dsl/definition_builder.rb', line 19

def start(klass)
  validate_classes!(klass)
  validate_start_once!
  add_new_nodes(klass)

  self
end