Class: Ductwork::DSL::BranchBuilder

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

Defined Under Namespace

Classes: CollapseError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(last_node:, definition:) ⇒ BranchBuilder

Returns a new instance of BranchBuilder.



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

def initialize(last_node:, definition:)
  @last_node = last_node
  @definition = definition
  @expansions = 0
  @expand_sources = []
end

Instance Attribute Details

#last_nodeObject (readonly)

Returns the value of attribute last_node.



8
9
10
# File 'lib/ductwork/dsl/branch_builder.rb', line 8

def last_node
  @last_node
end

Instance Method Details

#chain(next_klass) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/ductwork/dsl/branch_builder.rb', line 17

def chain(next_klass)
  next_klass_name = node_name(next_klass)
  definition[:edges][last_node][:to] = [next_klass_name]
  definition[:edges][last_node][:type] = :chain
  definition[:nodes].push(next_klass_name)
  definition[:edges][next_klass_name] ||= { klass: next_klass.name }
  @last_node = next_klass_name

  self
end

#collapse(into:) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/ductwork/dsl/branch_builder.rb', line 117

def collapse(into:)
  if expansions.zero?
    raise CollapseError,
          "Must expand pipeline definition before collapsing steps"
  end

  next_klass_name = node_name(into)
  definition[:edges][last_node][:to] = [next_klass_name]
  definition[:edges][last_node][:type] = :collapse
  definition[:edges][last_node][:barrier_node] = expand_sources.pop

  definition[:nodes].push(next_klass_name)
  definition[:edges][next_klass_name] ||= { klass: into.name }
  @last_node = next_klass_name
  @expansions -= 1

  self
end

#combine(*branch_builders, into:) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ductwork/dsl/branch_builder.rb', line 72

def combine(*branch_builders, into:)
  next_klass_name = node_name(into)
  definition[:edges][last_node][:to] = [next_klass_name]
  definition[:edges][last_node][:type] = :combine

  branch_builders.each do |branch|
    definition[:edges][branch.last_node][:to] = [next_klass_name]
    definition[:edges][branch.last_node][:type] = :combine
  end
  definition[:nodes].push(next_klass_name)
  definition[:edges][next_klass_name] ||= { klass: into.name }

  self
end

#converge(*branch_builders, into:) ⇒ Object



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

def converge(*branch_builders, into:)
  next_klass_name = node_name(into)
  definition[:edges][last_node][:to] = [next_klass_name]
  definition[:edges][last_node][:type] = :converge

  branch_builders.each do |branch|
    definition[:edges][branch.last_node][:to] = [next_klass_name]
    definition[:edges][branch.last_node][:type] = :converge
  end
  definition[:nodes].push(next_klass_name)
  definition[:edges][next_klass_name] ||= { klass: into.name }

  self
end

#divert(to:) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ductwork/dsl/branch_builder.rb', line 47

def divert(to:)
  to_map = {}
  next_nodes = []
  to.each do |key, klass|
    node = node_name(klass)
    definition[:edges][node] ||= { klass: klass.name }
    to_map[key.to_s] = node
    next_nodes << node
  end

  definition[:edges][last_node][:to] = to_map
  definition[:edges][last_node][:type] = :divert
  definition[:nodes].push(*next_nodes)

  if block_given?
    sub_branches = next_nodes.map do |last_node|
      Ductwork::DSL::BranchBuilder.new(last_node:, definition:)
    end

    yield sub_branches
  end

  self
end

#divide(to:) {|sub_branches| ... } ⇒ Object

Yields:

  • (sub_branches)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ductwork/dsl/branch_builder.rb', line 28

def divide(to:)
  next_nodes = to.map do |klass|
    node = node_name(klass)
    definition[:edges][node] ||= { klass: klass.name }
    node
  end
  definition[:edges][last_node][:to] = next_nodes
  definition[:edges][last_node][:type] = :divide
  definition[:nodes].push(*next_nodes)

  sub_branches = next_nodes.map do |last_node|
    Ductwork::DSL::BranchBuilder.new(last_node:, definition:)
  end

  yield sub_branches

  self
end

#expand(to:) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ductwork/dsl/branch_builder.rb', line 102

def expand(to:)
  next_klass_name = node_name(to)
  # NOTE: record the expanding node so the matching `collapse` can stamp it
  # as `barrier_node` (see `Ductwork::DSL::DefinitionBuilder#expand`).
  expand_sources.push(last_node)
  definition[:edges][last_node][:to] = [next_klass_name]
  definition[:edges][last_node][:type] = :expand
  definition[:nodes].push(next_klass_name)
  definition[:edges][next_klass_name] ||= { klass: to.name }
  @last_node = next_klass_name
  @expansions += 1

  self
end