Class: Ductwork::DSL::DefinitionBuilder
- Inherits:
-
Object
- Object
- Ductwork::DSL::DefinitionBuilder
- Defined in:
- lib/ductwork/dsl/definition_builder.rb
Overview
rubocop:todo Metrics/ClassLength
Defined Under Namespace
Classes: CollapseError, CombineError, ConvergeError, DivertError, StartError
Instance Method Summary collapse
- #chain(klass = nil, to: nil) ⇒ Object
- #collapse(into:) ⇒ Object
-
#combine(into:) ⇒ Object
rubocop:todo Metrics/AbcSize.
- #complete ⇒ Object
-
#converge(into:) ⇒ Object
rubocop:todo Metrics/AbcSize.
-
#divert(to:) ⇒ Object
rubocop:todo Metrics/AbcSize.
-
#divide(to:) ⇒ Object
rubocop:todo Metrics/AbcSize.
- #expand(to:) ⇒ Object
-
#initialize ⇒ DefinitionBuilder
constructor
A new instance of DefinitionBuilder.
- #on_halt(klass) ⇒ Object
- #start(klass) ⇒ Object
Constructor Details
#initialize ⇒ DefinitionBuilder
Returns a new instance of DefinitionBuilder.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/ductwork/dsl/definition_builder.rb', line 12 def initialize @definition = { metadata: {}, nodes: [], edges: {}, } @divergences = [] @last_nodes = [] @expand_sources = [] end |
Instance Method Details
#chain(klass = nil, to: nil) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/ductwork/dsl/definition_builder.rb', line 36 def chain(klass = nil, to: nil) klass ||= to validate_classes!(klass) validate_definition_started!(action: "chaining") add_edge_to_last_nodes(klass, type: :chain) self end |
#collapse(into:) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/ductwork/dsl/definition_builder.rb', line 115 def collapse(into:) validate_classes!(into) validate_definition_started!(action: "collapsing steps") validate_can_collapse! collapse_sources = last_nodes.dup add_edge_to_last_nodes(into, type: :collapse) divergences.pop = collapse_sources.each do |source| definition[:edges][source][:barrier_node] = end self end |
#combine(into:) ⇒ Object
rubocop:todo Metrics/AbcSize
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/ductwork/dsl/definition_builder.rb', line 75 def combine(into:) # rubocop:todo Metrics/AbcSize validate_classes!(into) validate_definition_started!(action: "combining steps") validate_can_combine! divergences.pop into_node = node_name(into) definition[:edges][into_node] ||= { klass: into.name } last_nodes = definition[:nodes].reverse.select do |node| definition.dig(:edges, node, :to).blank? end last_nodes.each do |last_node| definition[:edges][last_node][:to] = [into_node] definition[:edges][last_node][:type] = :combine end @last_nodes = Array(into_node) definition[:nodes].push(into_node) self end |
#complete ⇒ Object
198 199 200 201 202 |
# File 'lib/ductwork/dsl/definition_builder.rb', line 198 def complete validate_definition_started!(action: "completing") definition end |
#converge(into:) ⇒ Object
rubocop:todo Metrics/AbcSize
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/ductwork/dsl/definition_builder.rb', line 166 def converge(into:) # rubocop:todo Metrics/AbcSize validate_classes!(into) validate_definition_started!(action: "converging steps") validate_can_converge! divergences.pop into_node = node_name(into) definition[:edges][into_node] ||= { klass: into.name } last_nodes = definition[:nodes].reverse.select do |node| definition.dig(:edges, node, :to).blank? end last_nodes.each do |last_node| definition[:edges][last_node][:to] = [into_node] definition[:edges][last_node][:type] = :converge end @last_nodes = Array(into_node) definition[:nodes].push(into_node) self end |
#divert(to:) ⇒ Object
rubocop:todo Metrics/AbcSize
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/ductwork/dsl/definition_builder.rb', line 130 def divert(to:) # rubocop:todo Metrics/AbcSize validate_classes!(to.values) validate_definition_started!(action: "diverting chain") validate_fallback_step!(to.keys) to_map = {} to_nodes = [] to.each do |key, klass| node = node_name(klass) definition[:edges][node] ||= { klass: klass.name } to_map[key.to_s] = node to_nodes << node end last_nodes.each do |last_node| definition[:edges][last_node][:to] = to_map definition[:edges][last_node][:type] = :divert end @last_nodes = to_nodes definition[:nodes].push(*to_nodes) divergences.push(:divert) if block_given? branches = last_nodes.map do |last_node| Ductwork::DSL::BranchBuilder .new(last_node:, definition:) end yield branches end self end |
#divide(to:) ⇒ Object
rubocop:todo Metrics/AbcSize
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/ductwork/dsl/definition_builder.rb', line 45 def divide(to:) # rubocop:todo Metrics/AbcSize validate_classes!(to) validate_definition_started!(action: "dividing chain") to_nodes = to.map do |to| node = node_name(to) definition[:edges][node] ||= { klass: to.name } node end last_nodes.each do |last_node| definition[:edges][last_node][:to] = to_nodes definition[:edges][last_node][:type] = :divide end @last_nodes = Array(to_nodes) definition[:nodes].push(*to_nodes) divergences.push(:divide) if block_given? branches = last_nodes.map do |last_node| Ductwork::DSL::BranchBuilder .new(last_node:, definition:) end yield branches end self end |
#expand(to:) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ductwork/dsl/definition_builder.rb', line 99 def (to:) validate_classes!(to) validate_definition_started!(action: "expanding chain") # NOTE: record the node(s) carrying this `expand` edge before # `add_edge_to_last_nodes` reassigns `@last_nodes`. The matching # `collapse` stamps this on its edge as `barrier_node` so the runtime # fan-in can find the expanding branch (which carries the barrier) no # matter how many transitions sit between the `expand` and `collapse`. .push(last_nodes.dup) add_edge_to_last_nodes(to, type: :expand) divergences.push(:expand) self end |
#on_halt(klass) ⇒ Object
190 191 192 193 194 195 196 |
# File 'lib/ductwork/dsl/definition_builder.rb', line 190 def on_halt(klass) validate_classes!(klass) definition[:metadata][:on_halt] = { klass: klass.name } self end |
#start(klass) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/ductwork/dsl/definition_builder.rb', line 23 def start(klass) validate_classes!(klass) validate_start_once! node = node_name(klass) definition[:nodes].push(node) definition[:edges][node] = { klass: klass.name } @last_nodes = [node] self end |