easy_flow 0.53.0 → 0.55.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1f5f8658d15f77b5f5ffd3b5ab85d1e90daa8a2199d20d260f6b72823bc5d09c
4
- data.tar.gz: 4f824955ad8425066bf489b08ae48c4a60216ac5358aa96bebeaa56b8fb31647
3
+ metadata.gz: 84c894614f26742e63801ba7efc4d168740ad53aa6cbf36dd5df6ed581800e73
4
+ data.tar.gz: d39770774d54283df68d05e735e74f8f1548fa0ee8914e58bc3dc15745a8e3b4
5
5
  SHA512:
6
- metadata.gz: 3f20a4149225cfe7d1142822d7b29d507ca6eb2e801df27d6ddf881f6d6c09870952fe2c8c271cdd794df4cc207a072abdb3a83c8e98d0e5c82ee746d9500560
7
- data.tar.gz: e2efb559112c1821028b0e442e8c1050e7ba545310fc9fb48841f42db942a6be8628070ed7555ca028aaaa4ecc4db10c37601c200fc5f8f1ff8cd76b9f2294a5
6
+ metadata.gz: 85dcab8de7bfb5cd9761f7aa616bbb79f49174043a26415366ff6b83c133c9e0c73509d9ef9827919491dd9d6095237831bfab3df004478b6f2ab95c8859b168
7
+ data.tar.gz: 11f039f22f8368d0351afc87d0aa001510f606f746b3353c53f5241a91b096f67a1e013231f98bdb1b5493602606d861976ecd880a8a1349ba597f42668afb51
data/README.md CHANGED
@@ -1,2 +1,57 @@
1
1
  # easy_flow
2
2
  Guided, versioned, branching flows for Rails apps
3
+
4
+ ## For each
5
+
6
+ A For each step (`:for_each`) is drawn on the canvas as a dashed box. It runs the steps inside the box once for each item of a list an earlier step recorded, starting items in list order. It runs only in a flow that saves its runs, and a flow that keeps nothing tells the visitor so.
7
+
8
+ Its settings:
9
+
10
+ - `list` names the list as `<step>.<output>`, such as `outline.variants`, or as the step id alone when that step recorded the array as its whole value.
11
+ - `concurrency` is how many items may be unfinished at the same time. With none set, items run one at a time.
12
+ - `on_item_failure` is `continue` or `stop`. Under `continue`, a failed item ends only that item. Under `stop`, no item starts after an item has failed, and the items already running are allowed to end.
13
+ - `item_limit` is the most items the list may hold. With none set, the limit is 100.
14
+
15
+ Each visit of a held step is recorded under its item number, counting from 1, such as `write@item2`, and a further visit inside the same item adds the visit number, such as `write@item2@2`. A held step's input can tick `The current item` to receive that item's value from the list. An input naming another held step receives that step's output for the same item. An item ends when its steps leave the box, or when a held step routes to `failed`. An item counts as running once one of its held steps has a recorded visit.
16
+
17
+ ### Steps ready now
18
+
19
+ `run.next_step(run.recorded)` names one step, as it always has. `run.ready_steps` names every step that may run now. Outside a For each step it holds the next step alone. Inside one it holds the next held step of each unfinished item, never more than `concurrency` of them, in list order.
20
+
21
+ ```ruby
22
+ run.ready_steps.map(&:id)
23
+ # => ["write@item1", "write@item2", "write@item3"]
24
+ run.record("write@item2", { "result" => "passed" })
25
+ run.advance
26
+ run.ready_steps.map(&:id)
27
+ # => ["write@item1", "write@item3", "write@item4"]
28
+ ```
29
+
30
+ `run.advance` runs every acting step among the ready steps, not only the first.
31
+
32
+ ### What it records
33
+
34
+ Once every item has ended, the For each step records these outputs under its own id:
35
+
36
+ - `result` is `passed` when no item failed, and `failed` otherwise.
37
+ - `items` holds one entry per item that ran, mapping each held step the item reached to its latest value.
38
+ - `passed` and `failed` count the items.
39
+ - `reason` says why the step failed when its list could not be run.
40
+
41
+ A step after the box reads them through an input that names the For each step and one of those outputs. An empty list runs no items, records `items` as an empty list, and the run goes on to the step after the box.
42
+
43
+ The run leaves the box on the connection that leaves it from a held step. Under `stop`, once an item has failed and the running items have ended, the run leaves on the `failed` connection of the held step that failed.
44
+
45
+ ### A list that cannot be run
46
+
47
+ The step starts no item and records `result` as `failed` with a `reason` when:
48
+
49
+ - the earlier step recorded no value under the named output, such as `The step outline recorded no value under variants.`
50
+ - the value is not a list, such as `The list outline.variants is text, not a list.`, naming text, a number or a hash.
51
+ - the list holds more items than `item_limit`, such as `The list outline.variants holds 120 items, more than the limit of 100.`
52
+
53
+ The run then leaves on a `failed` connection that leaves the box from a held step. With none, the run stops there, as it does after any failed step with no `failed` connection.
54
+
55
+ ### Reaching it again
56
+
57
+ A run that comes back to a For each step, through a later step that loops back to it or to a step before it, runs the held steps again for each item of the list as it stands then. The second pass records its visits with a pass number after the item, such as `write@item2@pass2` and `write@item2@pass2@2`, and the third pass with `pass3`. The first pass's keys carry no pass number. The For each step records each pass's outputs under its own visit, `variants` for the first and `variants@2` for the second, so both stay readable, and a step after the box reads the latest pass. A held step's input naming another held step reads that step's output for the same item in the same pass.
@@ -1,5 +1,9 @@
1
1
  module EasyFlow
2
2
  class Digest
3
+ Path = Struct.new(:recorded, :latest, :latest_times)
4
+ ItemWalk = Struct.new(:held, :failed_at, :started)
5
+ ItemsWalk = Struct.new(:ready, :stopped_at, :problem)
6
+
3
7
  def initialize(document, registry: EasyFlow.registry)
4
8
  @document = document
5
9
  @registry = registry
@@ -72,10 +76,40 @@ module EasyFlow
72
76
  step_type(node)&.settings&.requirements_for(node.config).to_a
73
77
  end
74
78
 
79
+ def item_value(key, state)
80
+ visit = VisitKey.parse(key)
81
+ box = holder_of(visit.step)
82
+ items_of(box, latest_values(state))[visit.item - 1] if box && visit.item
83
+ end
84
+
85
+ def items_ended(id, state)
86
+ box = step(id)
87
+ problem = list_problem(box, latest_values(state))
88
+ return { "result" => "failed", "reason" => problem, "items" => [], "passed" => 0, "failed" => 0 } if problem
89
+
90
+ pass = VisitKey.parse(id).visit
91
+ ended = items_of(box, latest_values(state)).each_index.map { |index| item_values(box, index + 1, pass, state) }.reject(&:empty?)
92
+ failed = ended.count { |values| values.any? { |held, value| failed?(step(held), state.merge(held => value), {}) } }
93
+
94
+ { "result" => failed.zero? ? "passed" : "failed", "items" => ended, "passed" => ended.size - failed, "failed" => failed }
95
+ end
96
+
97
+ def repeats?(node)
98
+ step_type(node)&.repeats_held_steps? || false
99
+ end
100
+
101
+ def holder_of(key)
102
+ steps.find { |node| repeats?(node) && Array(node.config["holds"]).include?(VisitKey.parse(key).step) }
103
+ end
104
+
75
105
  def next_step(state, times = {})
76
106
  walk(state, times)[1]
77
107
  end
78
108
 
109
+ def ready_steps(state, times = {})
110
+ walk(state, times)[3].to_a
111
+ end
112
+
79
113
  def output(state, times = {})
80
114
  ended_setting(state, times, "output")
81
115
  end
@@ -136,7 +170,9 @@ module EasyFlow
136
170
  visits[cursor.id] += 1
137
171
  answers_at_visit[cursor.id] = answered
138
172
  key = visit_key(cursor.id, visits[cursor.id])
139
- return [ recorded, cursor.with(id: key) ] if pending?(cursor, key, state)
173
+ items = repeats?(cursor) ? walk_items(cursor, visits[cursor.id], state, times, Path.new(recorded, latest, latest_times)) : ItemsWalk.new([], nil, nil)
174
+ return [ recorded, items.ready.first, nil, items.ready ] if items.ready.any? && !state.key?(key)
175
+ return [ recorded, cursor.with(id: key), nil, [ cursor.with(id: key) ] ] if pending?(cursor, key, state)
140
176
 
141
177
  if state.key?(key)
142
178
  recorded << key
@@ -144,12 +180,127 @@ module EasyFlow
144
180
  latest_times[cursor.id] = times[key] if times.key?(key)
145
181
  answered += 1 if step_type(cursor)&.awaits_input?
146
182
  end
147
- cursor = successor(cursor, reached(state, recorded).merge(latest), latest_times)
183
+ cursor = repeats?(cursor) ? box_exit(cursor, items) : successor(cursor, reached(state, recorded).merge(latest), latest_times)
148
184
  end
149
185
 
150
186
  [ recorded, nil ]
151
187
  end
152
188
 
189
+ def walk_items(box, pass, state, times, path)
190
+ walked = items_of(box, path.latest).each_index.map { |index| walk_item(box, VisitKey.new(step: box.id, item: index + 1, pass: pass), state, times, path) }
191
+ stopped_at = walked.find(&:failed_at)&.failed_at if stops_on_failure?(box)
192
+ ready = walked.select(&:held).select { |item| item.started || !stopped_at }.first(concurrency_of(box)).map(&:held)
193
+
194
+ ItemsWalk.new(ready, stopped_at, list_problem(box, path.latest))
195
+ end
196
+
197
+ def stops_on_failure?(box)
198
+ box.config["on_item_failure"].to_s == "stop"
199
+ end
200
+
201
+ def concurrency_of(box)
202
+ [ box.config["concurrency"].to_i, 1 ].max
203
+ end
204
+
205
+ def walk_item(box, item, state, times, path)
206
+ visits = Hash.new(0)
207
+ recorded_at_visit = {}
208
+ cursor = first_held(box)
209
+
210
+ started = false
211
+
212
+ while cursor && held_by?(box, cursor)
213
+ return ItemWalk.new(nil, nil, started) if visits[cursor.id].positive? && recorded_at_visit[cursor.id] == path.recorded.size
214
+
215
+ visits[cursor.id] += 1
216
+ recorded_at_visit[cursor.id] = path.recorded.size
217
+ key = item.with(step: cursor.id, visit: visits[cursor.id]).to_s
218
+ return ItemWalk.new(cursor.with(id: key), nil, started) if pending?(cursor, key, state)
219
+
220
+ if state.key?(key)
221
+ started = true
222
+ path.recorded << key
223
+ path.latest[cursor.id] = state[key]
224
+ path.latest_times[cursor.id] = times[key] if times.key?(key)
225
+ end
226
+ routed = reached(state, path.recorded).merge(path.latest)
227
+ return ItemWalk.new(nil, cursor, started) if failed?(cursor, routed, path.latest_times)
228
+
229
+ cursor = successor(cursor, routed, path.latest_times)
230
+ end
231
+ ItemWalk.new(nil, nil, started)
232
+ end
233
+
234
+ def failed?(node, state, times)
235
+ step_type(node)&.route(node, state, times).to_s == "failed"
236
+ end
237
+
238
+ def items_of(box, latest)
239
+ list_problem(box, latest) ? [] : listed_value(box, latest)
240
+ end
241
+
242
+ def listed_value(box, latest)
243
+ step_id, output = step_type(box).list_named_by(box).split(".", 2)
244
+ listed = latest[step_id]
245
+ listed.is_a?(Hash) && output ? listed[output] : listed
246
+ end
247
+
248
+ def list_problem(box, latest)
249
+ named = step_type(box).list_named_by(box)
250
+ step_id, output = named.split(".", 2)
251
+ listed = listed_value(box, latest)
252
+ return "The step #{step_id} recorded no value#{" under #{output}" if output}." if listed.nil?
253
+
254
+ return "The list #{named} is #{kind_of_value(listed)}, not a list." unless listed.is_a?(Array)
255
+
256
+ "The list #{named} holds #{listed.size} items, more than the limit of #{item_limit_of(box)}." if listed.size > item_limit_of(box)
257
+ end
258
+
259
+ def item_limit_of(box)
260
+ box.config["item_limit"].presence&.to_i || ForEach::ITEM_LIMIT
261
+ end
262
+
263
+ def kind_of_value(value)
264
+ case value
265
+ when String then "text"
266
+ when Numeric then "a number"
267
+ when Hash then "a hash"
268
+ else "true or false"
269
+ end
270
+ end
271
+
272
+ def latest_values(state)
273
+ state.keys.map { |key| VisitKey.parse(key) }.reject(&:item).group_by(&:step)
274
+ .transform_values { |visits| state[visits.max_by(&:visit).to_s] }
275
+ end
276
+
277
+ def item_values(box, item, pass, state)
278
+ visits = state.keys.map { |key| VisitKey.parse(key) }.select { |visit| visit.item == item && visit.pass == pass }
279
+ Array(box.config["holds"]).filter_map do |held|
280
+ latest = visits.select { |visit| visit.step == held }.max_by(&:visit)
281
+ [ held, state[latest.to_s] ] if latest
282
+ end.to_h
283
+ end
284
+
285
+ def held_by?(box, node)
286
+ Array(box.config["holds"]).include?(node.id)
287
+ end
288
+
289
+ def first_held(box)
290
+ entering = @document.edges_from(box.id).first
291
+ entering && @document.node(entering.to)
292
+ end
293
+
294
+ def box_exit(box, items)
295
+ held = Array(box.config["holds"])
296
+ leaving = @document.edges.select { |edge| held.include?(edge.from) && !held.include?(edge.to) }.find do |edge|
297
+ next edge.from == items.stopped_at.id && edge.on.to_s == "failed" if items.stopped_at
298
+
299
+ items.problem ? edge.on.to_s == "failed" : edge.on.to_s != "failed"
300
+ end
301
+ leaving && @document.node(leaving.to)
302
+ end
303
+
153
304
  def reached(state, recorded)
154
305
  state.reject { |key, _value| step(key) && !recorded.include?(key) }
155
306
  end
@@ -172,7 +323,7 @@ module EasyFlow
172
323
  def pending?(node, key, state)
173
324
  return false if state.key?(key)
174
325
 
175
- step_type(node)&.awaits_input? || acts?(node) || step_type(node)&.waits? || false
326
+ step_type(node)&.awaits_input? || acts?(node) || step_type(node)&.waits? || repeats?(node)
176
327
  end
177
328
 
178
329
  def acts?(node)
@@ -0,0 +1,23 @@
1
+ module EasyFlow
2
+ class ForEach
3
+ include Step
4
+
5
+ ITEM_LIMIT = 100
6
+
7
+ step_name "For each"
8
+
9
+ holds_steps
10
+ repeats_held_steps_for :list
11
+ needs_a_saved_run "Steps can only be run for each item in a flow that saves its runs."
12
+
13
+ setting :list, type: :string
14
+ setting :concurrency, type: :integer
15
+ setting :on_item_failure, type: :select, options: %w[continue stop]
16
+ setting :item_limit, type: :integer, label: "Most items"
17
+
18
+ output :result, values: %w[passed failed]
19
+ output :items
20
+ output :passed, type: :integer
21
+ output :failed, type: :integer
22
+ end
23
+ end
@@ -9,28 +9,37 @@ module EasyFlow
9
9
  setting :step, type: :previous_step, label: "Earlier step"
10
10
  setting :output, outputs_of: :step, label: "Earlier step's output"
11
11
  setting :from_run_owner, type: :boolean, label: "The run's owner"
12
+ setting :current_item, type: :boolean, label: "The current item"
12
13
  end
13
14
  end
14
15
 
15
16
  def self.values_of(node, state, run: nil)
16
- Array(node.config["inputs"]).to_h { |input| [ input["name"], value_of(input, state, run) ] }
17
+ Array(node.config["inputs"]).to_h { |input| [ input["name"], value_of(node, input, state, run) ] }
17
18
  end
18
19
 
19
- def self.value_of(input, state, run)
20
+ def self.value_of(node, input, state, run)
20
21
  return run&.owner_id if input["from_run_owner"]
22
+ return run&.digest&.item_value(node.id, state) if input["current_item"]
21
23
 
22
- input["step"].present? ? latest_output(input, state, run) : input["value"]
24
+ input["step"].present? ? latest_output(node, input, state, run) : input["value"]
23
25
  end
24
26
  private_class_method :value_of
25
27
 
26
- def self.latest_output(input, state, run)
27
- latest_visit = state.keys.select { |key| key.split("@").first == input["step"] }.max_by { |key| key.split("@")[1].to_i }
28
+ def self.latest_output(node, input, state, run)
29
+ latest_visit = latest_visit_of(input["step"], VisitKey.parse(node.id), state)
28
30
  return if unreported?(input, state[latest_visit], run)
29
31
 
30
32
  CompletedAt.value_of(state, {}, latest_visit, input["output"])
31
33
  end
32
34
  private_class_method :latest_output
33
35
 
36
+ def self.latest_visit_of(step, reader, state)
37
+ visits = state.keys.to_h { |key| [ key, VisitKey.parse(key) ] }.select { |_key, visit| visit.step == step }
38
+ same_item = visits.select { |_key, visit| visit.item == reader.item && visit.pass == reader.pass }
39
+ (same_item.presence || visits.reject { |_key, visit| visit.item }).max_by { |_key, visit| visit.visit }&.first
40
+ end
41
+ private_class_method :latest_visit_of
42
+
34
43
  def self.unreported?(input, recorded, run)
35
44
  run && input["output"].present? && !recorded.is_a?(Hash) && !run.digest.plain_value_is?(input["step"], input["output"])
36
45
  end
@@ -40,6 +40,10 @@ module EasyFlow
40
40
  digest.next_step(state.transform_keys(&:to_s), completed_at.transform_keys(&:to_s))
41
41
  end
42
42
 
43
+ def ready_steps(state = recorded)
44
+ digest.ready_steps(state.transform_keys(&:to_s), completed_at.transform_keys(&:to_s))
45
+ end
46
+
43
47
  def ended_at_end?
44
48
  digest.ended_at_end?(recorded.transform_keys(&:to_s), completed_at.transform_keys(&:to_s))
45
49
  end
@@ -29,11 +29,12 @@ module EasyFlow
29
29
  end
30
30
 
31
31
  def run(progress)
32
- while (node = @digest.next_step(named(progress.recorded), named(progress.completed_at))) && goes_on?(node, named(progress.recorded), CompletedAt.latest(named(progress.completed_at))) && (progress.saved? || refusal_of(node).nil?)
33
- progress.record(node.id, result_of(@digest.step(node.id), named(progress.recorded), progress.run))
32
+ while (node = going_on(progress))
33
+ progress.record(node.id, result_of(node, named(progress.recorded), progress.run))
34
34
  end
35
- progress.start_inner(node) if node && starts_a_flow?(node)
36
- progress.ended unless node
35
+ waiting = @digest.next_step(named(progress.recorded), named(progress.completed_at))
36
+ progress.start_inner(waiting) if waiting && starts_a_flow?(waiting)
37
+ progress.ended unless waiting
37
38
  end
38
39
 
39
40
  def unsaved_refusal(state, times: {})
@@ -63,7 +64,18 @@ module EasyFlow
63
64
 
64
65
  private
65
66
 
67
+ def going_on(progress)
68
+ state = named(progress.recorded)
69
+ @digest.ready_steps(state, named(progress.completed_at)).find do |node|
70
+ goes_on?(node, state, CompletedAt.latest(named(progress.completed_at))) && (progress.saved? || refusal_of(node).nil?)
71
+ end
72
+ end
73
+
66
74
  def refusal_of(node)
75
+ own_refusal(node) || own_refusal(node && @digest.holder_of(node.id))
76
+ end
77
+
78
+ def own_refusal(node)
67
79
  @registry.fetch(node.type).unsaved_refusal if node && @registry.registered?(node.type)
68
80
  end
69
81
 
@@ -76,7 +88,7 @@ module EasyFlow
76
88
  end
77
89
 
78
90
  def goes_on?(node, state, times = {})
79
- acts?(node) || ready?(node, state, times) || decided?(node, state)
91
+ acts?(node) || @digest.repeats?(node) || ready?(node, state, times) || decided?(node, state)
80
92
  end
81
93
 
82
94
  def allowed(node, state)
@@ -99,8 +111,10 @@ module EasyFlow
99
111
  @registry.registered?(node.type) && @registry.fetch(node.type).waits? && @registry.fetch(node.type).ready?(node, state, times)
100
112
  end
101
113
 
102
- def result_of(node, state, run)
114
+ def result_of(visited, state, run)
115
+ node = @digest.step(visited.id)
103
116
  return allowed(node, state).first.to_s if decided?(node, state)
117
+ return @digest.items_ended(visited.id, state) if @digest.repeats?(node)
104
118
 
105
119
  acts?(node) ? @registry.fetch(node.type).process(node, state, run: run) : true
106
120
  end
@@ -103,6 +103,10 @@ module EasyFlow
103
103
  @holds_steps = true
104
104
  end
105
105
 
106
+ def repeats_held_steps_for(setting)
107
+ @repeats_held_steps_for = setting
108
+ end
109
+
106
110
  def chooses_flow_from_run(&chooser)
107
111
  @run_chooser = chooser
108
112
  end
@@ -179,7 +183,7 @@ module EasyFlow
179
183
 
180
184
  def to_step_type
181
185
  StepType.new(id: @id, step_name: @step_name, settings: settings, awaits_input: @awaits_input,
182
- ends_here: @ends_here, begins_here: @begins_here, starts_a_flow: @starts_a_flow, plays_the_picked_flow: @plays_the_picked_flow, holds_steps: @holds_steps, flow_chooser: @flow_chooser, run_chooser: @run_chooser, behaviour: @behaviour, routing: @routing,
186
+ ends_here: @ends_here, begins_here: @begins_here, starts_a_flow: @starts_a_flow, plays_the_picked_flow: @plays_the_picked_flow, holds_steps: @holds_steps, repeats_held_steps_for: @repeats_held_steps_for, flow_chooser: @flow_chooser, run_chooser: @run_chooser, behaviour: @behaviour, routing: @routing,
183
187
  display: @display, drawn_by: @drawn_by, naming_field: @naming_field, naming: @naming, summarizing: @summarizing, details: @details, linking: @linking,
184
188
  outputs: @declared_outputs, outputs_named_by: @outputs_named_by, answer_check: @answer_check, answer_labelling: @answer_labelling, readiness: @readiness, return_rule: @return_rule, unsaved_refusal: @unsaved_refusal)
185
189
  end
@@ -11,7 +11,7 @@ module EasyFlow
11
11
  attr_reader :id, :step_name, :settings, :naming_field, :naming, :outputs, :outputs_named_by, :drawn_by, :unsaved_refusal
12
12
 
13
13
  def initialize(id:, step_name:, settings:, awaits_input:, behaviour:, routing:,
14
- ends_here: false, begins_here: false, starts_a_flow: false, plays_the_picked_flow: false, holds_steps: false, flow_chooser: nil, run_chooser: nil, display: nil, drawn_by: nil,
14
+ ends_here: false, begins_here: false, starts_a_flow: false, plays_the_picked_flow: false, holds_steps: false, repeats_held_steps_for: nil, flow_chooser: nil, run_chooser: nil, display: nil, drawn_by: nil,
15
15
  naming_field: nil, naming: nil, summarizing: nil, details: [], linking: nil, outputs: [], outputs_named_by: nil, answer_check: nil, answer_labelling: nil, readiness: nil, return_rule: nil, unsaved_refusal: nil)
16
16
  @id = id
17
17
  @step_name = step_name
@@ -22,6 +22,7 @@ module EasyFlow
22
22
  @starts_a_flow = starts_a_flow
23
23
  @plays_the_picked_flow = plays_the_picked_flow
24
24
  @holds_steps = holds_steps
25
+ @repeats_held_steps_for = repeats_held_steps_for
25
26
  @flow_chooser = flow_chooser
26
27
  @run_chooser = run_chooser
27
28
  @behaviour = behaviour
@@ -134,6 +135,14 @@ module EasyFlow
134
135
  @holds_steps
135
136
  end
136
137
 
138
+ def repeats_held_steps?
139
+ @holds_steps && @repeats_held_steps_for.present?
140
+ end
141
+
142
+ def list_named_by(node)
143
+ node.config[@repeats_held_steps_for.to_s].to_s if repeats_held_steps?
144
+ end
145
+
137
146
  def chooses_flow_from_run?
138
147
  !@run_chooser.nil?
139
148
  end
@@ -0,0 +1,18 @@
1
+ module EasyFlow
2
+ VisitKey = Data.define(:step, :item, :visit, :pass) do
3
+ def self.parse(key)
4
+ step, *rest = key.to_s.split("@")
5
+ item = rest.shift.delete_prefix("item").to_i if rest.first.to_s.start_with?("item")
6
+ pass = rest.shift.delete_prefix("pass").to_i if rest.first.to_s.start_with?("pass")
7
+ new(step: step, item: item, visit: (rest.first || 1).to_i, pass: pass || 1)
8
+ end
9
+
10
+ def initialize(step:, item: nil, visit: 1, pass: 1)
11
+ super
12
+ end
13
+
14
+ def to_s
15
+ [ step, ("item#{item}" if item), ("pass#{pass}" if pass > 1), (visit if visit > 1) ].compact.join("@")
16
+ end
17
+ end
18
+ end
@@ -31,6 +31,7 @@ module EasyFlow
31
31
  EasyFlow::CompareTwoValues.register
32
32
  EasyFlow::AllOrAny.register
33
33
  EasyFlow::Count.register
34
+ EasyFlow::ForEach.register
34
35
  EasyFlow::ReadData.register
35
36
  EasyFlow::WriteData.register
36
37
  EasyFlow::Wait.register
@@ -1,3 +1,3 @@
1
1
  module EasyFlow
2
- VERSION = "0.53.0"
2
+ VERSION = "0.55.0"
3
3
  end
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: easy_flow-develop
3
- description: Use PROACTIVELY for adding a step type to easy_flow flows (a step that asks the visitor something, takes several answers at once, computes a value from earlier answers, records several values under names an admin enters in the step's settings, counts how many times an earlier step was answered so a loop can give up after a number of tries, picks the next branch, or holds the visitor until something outside the flow has happened), adding a step type an admin draws as a box that holds other steps on the canvas, such as a "for each" step, branching on whether all or any of several comparisons of earlier answers hold, holding a run until a number of minutes after an earlier answer, the next time of day after it, or a date, branching on the hour, weekday or time an earlier step was completed or on the minutes between two completed steps, reading when each step was completed and when a waiting run will move on, showing a step differently depending on the visitor's stored run, branching on what a visitor ticked on a checklist, moving a paused run on once what it waits for has happened, refusing a blank or invalid answer to a step with a message, offering an admin a step setting whose options are read from the app's own records, letting an admin pick another flow of the same host in a step's settings, adding a step type that starts another flow and works out which flow and version to run from its own settings, adding a step type that starts another flow chosen from the visitor's run, such as its owner or its answers, offering a visitor only the answers to a step that their earlier answers allow, filling in a step that has only one possible answer without asking it, skipping a step that does not apply to a visitor, letting an admin edit a value stored on one of the app's own records from a step's settings on the canvas, serving a host's flows from the app's own controller and routes, starting a visitor's run on a published version of a flow other than the live one, acting when a visitor finishes a flow, acting on the output a visitor's flow ended with, telling whether a run ended at an End step or was stopped by a loop that came back with nothing new answered and at which step, keeping a flow from being published while a step that starts another flow names a flow or version that cannot run or leads back to the flow it sits in, finding the run of another flow that a Flow step started, the run a waiting run is waiting on, and the run that started it, sending a visitor back to the run that started a flow once that flow ends, handling a run whose starting run was removed with its flow, handling a Flow step that cannot start its flow, whose flow ended with no output, or whose flow was stopped by a loop, taking one file or several from a visitor at a step, showing a one-line summary of a step's settings on its canvas card, showing a step's recorded answer by a label the step type works out, and reading a run's recorded answers and question labels, including every answer ticked on a checklist and every visit's answer when a flow loops back to a question already asked, and showing on the app's own page where a run stands, with each step's state, status line and link — MUST BE USED instead of hand-rolling questionnaire steps, checkbox lists, file upload steps, branching logic, answer lists filtered by earlier answers, skipped questions, number comparisons, comparisons of two earlier answers, and/or conditions over several comparisons, hard-coded setting options, copying a record's value into a flow and back, answer validation, retry counters, timers or scheduled delays, step completion timestamps, polling or "come back later" pages, flow controllers, answer lookups or hand-drawn run diagrams.
3
+ description: Use PROACTIVELY for adding a step type to easy_flow flows (a step that asks the visitor something, takes several answers at once, computes a value from earlier answers, records several values under names an admin enters in the step's settings, counts how many times an earlier step was answered so a loop can give up after a number of tries, picks the next branch, or holds the visitor until something outside the flow has happened), adding a step type an admin draws as a box that holds other steps on the canvas, running the steps a For each box holds once per item of a list an earlier step recorded and reading each item's values and the passed and failed counts after the box, branching on whether all or any of several comparisons of earlier answers hold, holding a run until a number of minutes after an earlier answer, the next time of day after it, or a date, branching on the hour, weekday or time an earlier step was completed or on the minutes between two completed steps, reading when each step was completed and when a waiting run will move on, showing a step differently depending on the visitor's stored run, branching on what a visitor ticked on a checklist, moving a paused run on once what it waits for has happened, refusing a blank or invalid answer to a step with a message, offering an admin a step setting whose options are read from the app's own records, letting an admin pick another flow of the same host in a step's settings, adding a step type that starts another flow and works out which flow and version to run from its own settings, adding a step type that starts another flow chosen from the visitor's run, such as its owner or its answers, offering a visitor only the answers to a step that their earlier answers allow, filling in a step that has only one possible answer without asking it, skipping a step that does not apply to a visitor, letting an admin edit a value stored on one of the app's own records from a step's settings on the canvas, serving a host's flows from the app's own controller and routes, starting a visitor's run on a published version of a flow other than the live one, acting when a visitor finishes a flow, acting on the output a visitor's flow ended with, telling whether a run ended at an End step or was stopped by a loop that came back with nothing new answered and at which step, keeping a flow from being published while a step that starts another flow names a flow or version that cannot run or leads back to the flow it sits in, finding the run of another flow that a Flow step started, the run a waiting run is waiting on, and the run that started it, sending a visitor back to the run that started a flow once that flow ends, handling a run whose starting run was removed with its flow, handling a Flow step that cannot start its flow, whose flow ended with no output, or whose flow was stopped by a loop, taking one file or several from a visitor at a step, showing a one-line summary of a step's settings on its canvas card, showing a step's recorded answer by a label the step type works out, and reading a run's recorded answers and question labels, including every answer ticked on a checklist and every visit's answer when a flow loops back to a question already asked, and showing on the app's own page where a run stands, with each step's state, status line and link — MUST BE USED instead of hand-rolling questionnaire steps, checkbox lists, file upload steps, branching logic, answer lists filtered by earlier answers, skipped questions, number comparisons, comparisons of two earlier answers, and/or conditions over several comparisons, hard-coded setting options, copying a record's value into a flow and back, answer validation, retry counters, timers or scheduled delays, step completion timestamps, polling or "come back later" pages, flow controllers, answer lookups or hand-drawn run diagrams.
4
4
  tools: Read, Write, Edit, Grep
5
5
  scope: guided flows — versioned documents of steps and the connections between them, drawn on a canvas by an admin and run by a visitor one step at a time, with step types the host registers
6
6
  ---
@@ -9,7 +9,7 @@ This local follows the steps below exactly and invents none. Where a step names
9
9
 
10
10
  ## What easy_flow is
11
11
 
12
- A Rails engine for flows an admin draws on a canvas and a visitor runs one step at a time. Each step is an instance of a step type. The engine ships seventeen: start (`:start`), end (`:terminal`, whose one setting is the output an admin writes for a flow that ends there), question (`:question`, a question with a list of answers of which the visitor picks one, which the admin can mark required so a blank answer is refused), checklist (`:checklist`, a question with a list of answers of which the visitor ticks any number, which the admin can mark required so a visitor who ticks nothing is refused), Multiple select (`:multiple_select`, a question with a category and a list of answers, each with a value, a label and a weight, of which the visitor ticks any number, recording the list of ticked values, which the admin can mark required so a visitor who ticks nothing is refused with `Pick at least one answer to go on.`), Fill in the blank (`:fill_in_the_blank`, a question with a category and a list of accepted answers, each with a value, which the visitor answers by typing into a text box, recording the text exactly as typed, whose accepted answers are never sent to the visitor's page, which the admin can mark required so a visitor who types nothing or only spaces is refused with `Type an answer to go on.`; easy_flow records the answer and does not mark it right or wrong), File upload (`:file_upload`, a question the visitor answers by choosing one file from a file upload control labelled with the question text, with an admin setting listing the accepted file types as comma-separated extensions such as `.pdf, .png`, which limits the visitor's file picker to those types and is shown under the control as `Accepts` followed by the list as the admin wrote it, such as `Accepts .pdf, .png`, which the admin can mark required so a visitor who chooses no file is refused, and whose recorded answer is the reference the host's file store returned for the file), five that pick a branch from earlier answers with no code — condition (`:condition`, is or is not a chosen value, where the admin must pick one of the earlier step's outputs and the answer is read from that output when the earlier step recorded several and whole otherwise, so a Condition cannot be completed on a step whose type declares no output), switch (`:switch`, follows the connection labelled with the answer), compare (`:compare`, reads the answer as a number, from the output the admin picks when the earlier step recorded several, and checks it by more than, less than, at least or at most against an amount) and Compare two values (`:compare_two_values`, reads the answers of two earlier steps the admin picks as numbers, reading each from the output the admin picks when that step recorded several, and checks the first by more than, less than, at least, at most or equal to against the second plus an amount the admin may give, a blank amount adding nothing, checking the first against the amount alone when the admin leaves the second step empty, following its `true` or `false` connection, and following `false` when either answer is missing, is an array or is not a number) and All of or Any of (`:all_or_any`, holds a list of comparisons the admin enters and a `Must hold` setting of `all of` or `any of`, where each comparison asks for the same things as a Compare two values step, a step, its output, a comparison, an amount, and an optional second step and its output, and is checked the same way, a comparison with no second step checking the first answer against the amount alone, following its `true` connection when every comparison holds for `all of` or at least one holds for `any of`, and its `false` connection otherwise, including when it holds no comparisons, a comparison whose answer is missing, is an array or is not a number counting as not holding) — Count (`:count`, which acts without the visitor and records a whole number, how many visits to an earlier step the admin picks the run has recorded, counting only visits that gave the answer the admin picks or every visit when the admin picks none, reading that answer from the output the admin picks when the earlier step recorded several, named on the canvas `Count` followed by the earlier step's id and the answer, such as `Count tests failed`, and recording a new count under its own visit key each time a loop reaches it, so a compare step after it can end a loop once the count reaches an amount), Read data and Write data (`:read_data` and `:write_data`, which ask the app to carry out one thing from what it says can be read or written, with named inputs, and follow a `passed` or `failed` connection; `easy_flow-install` sets what the app offers), Wait (`:wait`, which holds the run until a time worked out from an earlier step the admin picks as `Counting from`; see "Hold a run until a time"), and Flow (`:flow_step`, names another flow of the same host and the number of one of its versions, with one connection for each different output that version's End steps write, and named on the canvas by that flow's title and the version number, such as `Intake, version 2`). On a compare, Compare two values or Count step, and in each comparison of an All of or Any of step, once the admin picks an earlier step, the admin must also pick which of that step's outputs to read: until then the step's card shows a warning that the step `needs a value for Output`, `needs a value for Other output` for a Compare two values step's second step, or `needs a value for Output in Comparisons entry <n>` and `needs a value for Other output in Comparisons entry <n>` for the comparison at position `n` counting from 1, and saving the flow as a new version and publishing it are refused. When a stored run stops at a Flow step, a run of the named flow is started on the named version, and the first run waits on the Flow step. When the second run ends with an output, that output is recorded in the first run at the Flow step, and the first run moves on along the connection labelled with that output. When the second run ends with no output, nothing is recorded and the first run stays waiting at the Flow step. On a question and on a checklist the admin can give each answer info, a longer explanation the visitor opens with an info button beside that answer. An admin may connect a step back to an earlier one, so a flow can ask the same question more than once, and each visit's answer is kept. The time each step visit was completed is kept together with its recorded value; see "Read when steps were completed". A File upload step takes a file only in a flow the admin set to save each step. In a flow set to save on finish or to save nothing, a visitor who chooses a file is shown `A file can only be uploaded in a flow that saves its runs.` and cannot go on with it. Before writing a step type to ask for one answer or several, to take a file, to branch on an answer or a number, to branch on how two earlier answers compare, to branch on whether all or any of several comparisons hold, to branch on when an earlier step was completed, to count the visits to an earlier step, or to hold a run until a time, ask the developer whether an admin placing one of the built-in types on the canvas is enough. None of condition, switch, compare, Compare two values or All of or Any of reads a checklist's answer, so branching on what a visitor ticked takes a step type of the app's own that defines `route`. Use this local when the app needs a step type of its own, needs a host's flows on its own pages with its own behaviour when a visitor finishes, needs a visitor's earlier answers to decide which answers a later step offers, or needs to read what a visitor answered. It assumes easy_flow is already installed and a host is declared; if not, hand off to `easy_flow-install` first.
12
+ A Rails engine for flows an admin draws on a canvas and a visitor runs one step at a time. Each step is an instance of a step type. The engine ships eighteen: start (`:start`), end (`:terminal`, whose one setting is the output an admin writes for a flow that ends there), question (`:question`, a question with a list of answers of which the visitor picks one, which the admin can mark required so a blank answer is refused), checklist (`:checklist`, a question with a list of answers of which the visitor ticks any number, which the admin can mark required so a visitor who ticks nothing is refused), Multiple select (`:multiple_select`, a question with a category and a list of answers, each with a value, a label and a weight, of which the visitor ticks any number, recording the list of ticked values, which the admin can mark required so a visitor who ticks nothing is refused with `Pick at least one answer to go on.`), Fill in the blank (`:fill_in_the_blank`, a question with a category and a list of accepted answers, each with a value, which the visitor answers by typing into a text box, recording the text exactly as typed, whose accepted answers are never sent to the visitor's page, which the admin can mark required so a visitor who types nothing or only spaces is refused with `Type an answer to go on.`; easy_flow records the answer and does not mark it right or wrong), File upload (`:file_upload`, a question the visitor answers by choosing one file from a file upload control labelled with the question text, with an admin setting listing the accepted file types as comma-separated extensions such as `.pdf, .png`, which limits the visitor's file picker to those types and is shown under the control as `Accepts` followed by the list as the admin wrote it, such as `Accepts .pdf, .png`, which the admin can mark required so a visitor who chooses no file is refused, and whose recorded answer is the reference the host's file store returned for the file), five that pick a branch from earlier answers with no code — condition (`:condition`, is or is not a chosen value, where the admin must pick one of the earlier step's outputs and the answer is read from that output when the earlier step recorded several and whole otherwise, so a Condition cannot be completed on a step whose type declares no output), switch (`:switch`, follows the connection labelled with the answer), compare (`:compare`, reads the answer as a number, from the output the admin picks when the earlier step recorded several, and checks it by more than, less than, at least or at most against an amount) and Compare two values (`:compare_two_values`, reads the answers of two earlier steps the admin picks as numbers, reading each from the output the admin picks when that step recorded several, and checks the first by more than, less than, at least, at most or equal to against the second plus an amount the admin may give, a blank amount adding nothing, checking the first against the amount alone when the admin leaves the second step empty, following its `true` or `false` connection, and following `false` when either answer is missing, is an array or is not a number) and All of or Any of (`:all_or_any`, holds a list of comparisons the admin enters and a `Must hold` setting of `all of` or `any of`, where each comparison asks for the same things as a Compare two values step, a step, its output, a comparison, an amount, and an optional second step and its output, and is checked the same way, a comparison with no second step checking the first answer against the amount alone, following its `true` connection when every comparison holds for `all of` or at least one holds for `any of`, and its `false` connection otherwise, including when it holds no comparisons, a comparison whose answer is missing, is an array or is not a number counting as not holding) — Count (`:count`, which acts without the visitor and records a whole number, how many visits to an earlier step the admin picks the run has recorded, counting only visits that gave the answer the admin picks or every visit when the admin picks none, reading that answer from the output the admin picks when the earlier step recorded several, named on the canvas `Count` followed by the earlier step's id and the answer, such as `Count tests failed`, and recording a new count under its own visit key each time a loop reaches it, so a compare step after it can end a loop once the count reaches an amount), Read data and Write data (`:read_data` and `:write_data`, which ask the app to carry out one thing from what it says can be read or written, with named inputs, and follow a `passed` or `failed` connection; `easy_flow-install` sets what the app offers), Wait (`:wait`, which holds the run until a time worked out from an earlier step the admin picks as `Counting from`; see "Hold a run until a time"), and Flow (`:flow_step`, names another flow of the same host and the number of one of its versions, with one connection for each different output that version's End steps write, and named on the canvas by that flow's title and the version number, such as `Intake, version 2`), and For each (`:for_each`, a group step drawn as a dashed box that runs the steps it holds once for each item of a list an earlier step recorded; see "Run steps once per item with a for_each step"). On a compare, Compare two values or Count step, and in each comparison of an All of or Any of step, once the admin picks an earlier step, the admin must also pick which of that step's outputs to read: until then the step's card shows a warning that the step `needs a value for Output`, `needs a value for Other output` for a Compare two values step's second step, or `needs a value for Output in Comparisons entry <n>` and `needs a value for Other output in Comparisons entry <n>` for the comparison at position `n` counting from 1, and saving the flow as a new version and publishing it are refused. When a stored run stops at a Flow step, a run of the named flow is started on the named version, and the first run waits on the Flow step. When the second run ends with an output, that output is recorded in the first run at the Flow step, and the first run moves on along the connection labelled with that output. When the second run ends with no output, nothing is recorded and the first run stays waiting at the Flow step. On a question and on a checklist the admin can give each answer info, a longer explanation the visitor opens with an info button beside that answer. An admin may connect a step back to an earlier one, so a flow can ask the same question more than once, and each visit's answer is kept. The time each step visit was completed is kept together with its recorded value; see "Read when steps were completed". A File upload step takes a file only in a flow the admin set to save each step. In a flow set to save on finish or to save nothing, a visitor who chooses a file is shown `A file can only be uploaded in a flow that saves its runs.` and cannot go on with it. Before writing a step type to ask for one answer or several, to take a file, to branch on an answer or a number, to branch on how two earlier answers compare, to branch on whether all or any of several comparisons hold, to branch on when an earlier step was completed, to count the visits to an earlier step, or to hold a run until a time, ask the developer whether an admin placing one of the built-in types on the canvas is enough. None of condition, switch, compare, Compare two values or All of or Any of reads a checklist's answer, so branching on what a visitor ticked takes a step type of the app's own that defines `route`. Use this local when the app needs a step type of its own, needs a host's flows on its own pages with its own behaviour when a visitor finishes, needs a visitor's earlier answers to decide which answers a later step offers, or needs to read what a visitor answered. It assumes easy_flow is already installed and a host is declared; if not, hand off to `easy_flow-install` first.
13
13
 
14
14
  ## Interface
15
15
 
@@ -184,25 +184,25 @@ A Rails engine for flows an admin draws on a canvas and a visitor runs one step
184
184
  ```
185
185
 
186
186
  Here `PlanFlow.for_customer` is the app's own lookup, returning `{ flow: <id>, version: <number> }` or `nil`.
187
- - `holds_steps` — a step of the type holds other steps of the same flow, such as a "for each" step. Ask the developer what the step should do with the steps it holds when a run reaches it. Do not pick, since the engine does nothing with them at run time.
187
+ - `holds_steps` — a step of the type holds other steps of the same flow. Ask the developer what the step should do with the steps it holds when a run reaches it. Do not pick. To run the held steps once per item of a list, place the built-in For each step instead of declaring a type; see "Run steps once per item with a for_each step".
188
188
  - On the canvas, a step of the type is drawn as a dashed box around the steps it holds, with a header line at its top in place of a step card. The header is the type's id followed by each filled-in setting as `<name>: <value>`, or `<name> <value>` for an `integer` or `float` setting, joined by ` · `, leaving out a blank setting and a setting whose value is a list or an array. On a host that hides step type codes, set through `easy_flow-install`, the header uses the type's `step_name` and each setting's label instead.
189
189
  - Clicking the header opens the step's settings. The header shows none of the error counts a step card shows, and has none of a card's buttons for starting a connection.
190
190
  - An admin puts a step in the box by dragging it onto the box, and takes it out by dragging it onto the canvas outside the box. A step is held by one group step at a time, a group step cannot hold itself or be put inside another group step, and a step cannot be moved into a step whose type does not declare `holds_steps`. Moving a step in or out leaves every connection as it was.
191
191
  - The ids of the steps held are kept in the flow's document under `holds` on the group step, so `node.config["holds"]` is an array of step ids, in the order they were moved in, or is missing when no step was ever moved in. Read it with `Array(node.config["holds"])`. Removing a held step takes its id out of the list, and saving the group step's settings keeps the list as it was.
192
192
  - The type must not declare a setting named `holds`, since a value saved under that name is dropped.
193
193
  - A run follows connections only. A run reaching a step of a type that declares none of `awaits_input`, `process`, `route` or `waits_until` passes on along its connection, and the steps it holds run once, where their connections place them, like any other steps.
194
+ - Never give the type the id `for_each`, which the built-in For each step takes.
194
195
 
195
196
  ```ruby
196
197
  module FlowSteps
197
- class ForEach
198
+ class Section
198
199
  include EasyFlow::Step
199
200
 
200
- step_name "For each"
201
+ step_name "Section"
201
202
 
202
203
  holds_steps
203
204
 
204
- setting :list, type: :string
205
- setting :on_item_failure, type: :select, options: %w[continue stop]
205
+ setting :title, type: :string
206
206
  end
207
207
  end
208
208
  ```
@@ -331,6 +331,8 @@ A Rails engine for flows an admin draws on a canvas and a visitor runs one step
331
331
  - `run: nil` in `process` is optional and must be written with its default. With it the step is given the stored run, or `nil` in a flow that saves no runs. A `process` without it keeps working as `process(node, state)`.
332
332
  - `EasyFlow::Inputs.values_of(node, state)` keeps working with those two arguments, and an entry with `The run's owner` ticked then gives nothing. Code outside `process` that reads a stored run's inputs passes the run itself, as `EasyFlow::Inputs.values_of(step, run.recorded.stringify_keys, run: run)`.
333
333
  - An entry that names an earlier step reads that step's output, at its latest visit when a loop reached it more than once. Any other entry gives its fixed value.
334
+ - An entry with `The current item` ticked, on a step held by a For each step, gives the value of the item that step is running for. It is stored as `"current_item" => true`, and gives nothing when no run is passed or the step is not held by a For each step.
335
+ - On a step held by a For each step, an entry naming another held step reads that step's latest visit for the same item, and an entry naming a step outside the box reads it as usual.
334
336
  - `state` in `process` still holds every visit under its own key, so a step type that adds up every visit keeps working.
335
337
  - An earlier step that recorded a plain value gives that value for whichever output the entry names.
336
338
  - `process` is not given completion times, so an entry naming "Hour completed", "Weekday completed" or "Minutes since 1970 when completed" gives nothing.
@@ -585,6 +587,42 @@ Use this when a visitor's run should stop until a set time has passed, such as a
585
587
  3. The view lays the steps out left to right whatever the host's `canvas_direction`, as the same graph a left-to-right canvas draws: compact cards in columns by stage, every connection drawn as a line labelled with the answer it is taken on, and a group step's box holding its steps as their own small graph. A flow wider than its box scrolls sideways inside it. On a screen narrower than 640 pixels each step sits on a row of its own with its connections listed under it. The Start and End steps are drawn as cards with their name alone, as the editor draws them, with every line into an End step and its branch label. The Start card is done once any step the app supplied has a state other than pending, and pending before that. A Start or End card is never a link, even when the app gives it an address, since neither has a page of its own. On a phone they are listed one per row with the other steps. It has no buttons and nothing on it changes the flow.
586
588
  4. Ask the developer what each step's status line says and where each card links. Do not pick.
587
589
 
590
+ ### Run steps once per item with a for_each step
591
+
592
+ 1. Ask the developer which earlier step records the list, and under which of its outputs. The list must be recorded as an array, such as `{ "variants" => ["short", "long"] }`.
593
+ 2. On the canvas, place a For each step and fill in its settings:
594
+ - `List` — the earlier step and its output as text, `<step>.<output>`, such as `outline.variants`, or the step id alone when it records the array as its plain value.
595
+ - `Concurrency` — how many items may be unfinished at the same time. With none set, items run one at a time.
596
+ - `On item failure` — `continue` or `stop`. Under `continue` a failed item ends only that item. Under `stop` no item starts after an item has failed, the items already running are allowed to end, and the run then leaves on the `failed` connection of the held step that failed.
597
+ - `Most items` — stored as `item_limit`, the most items the list may hold. With none set, the limit is 100.
598
+ 3. Drag the steps to run per item into the box. Connect the For each step to the first held step, connect the held steps to each other, and connect the last held step to the step after the box.
599
+ 4. Publish the flow set to save each step. A For each step reached in a flow set to save on finish or to save nothing shows the visitor `Steps can only be run for each item in a flow that saves its runs.` and goes no further, at the For each step or at any step it holds.
600
+ 5. What then holds when a stored run reaches the For each step:
601
+ - The run starts items in list order and keeps no more than `concurrency` of them unfinished at once. An item counts as running once one of its held steps has a recorded visit.
602
+ - Each visit of a held step is recorded under `<step>@item<n>`, counting items from 1, such as `write@item2`. A held step visited again inside one item, such as after a `retry` connection back to itself, is recorded as `<step>@item<n>@<visit>`, such as `write@item2@2`, and other items' visits are left as they were.
603
+ - `run.next_step(run.recorded)` returns one held step to run next with that key as its `id`, so the app records its value with `run.record(step.id, value)` and reads its inputs with `EasyFlow::Inputs.values_of(step, run.recorded.stringify_keys, run: run)`.
604
+ - `run.ready_steps` returns every step that may run now: the next held step of each unfinished item, up to `concurrency` of them, and outside a For each step the next step alone. An app that hands out several steps at once reads this list, and an app that reads only `next_step` keeps working one item at a time. `run.advance` runs every acting step among them.
605
+ - An item ends when its walk leaves the box, or when a held step's `route` returns `"failed"`, and the next item starts.
606
+ - Once every item has ended, `run.advance` records the For each step's own value under its id: `result` is `"passed"` when no item failed and `"failed"` otherwise, `items` is an array with one hash per item that ran mapping each held step it reached to that step's latest value for the item, and `passed` and `failed` count the items.
607
+ - An empty list runs no items, records `items` as `[]`, and the run goes on to the step after the box.
608
+ - A list that is missing, is not an array, or holds more items than the limit starts no item and records `{ "result" => "failed", "reason" => ..., "items" => [], "passed" => 0, "failed" => 0 }`. The reason names the step and output, the value's kind (text, a number or a hash), or the limit. The run then leaves on a `failed` connection leaving the box from a held step, and with none it stops there, as after any failed step with no `failed` connection.
609
+ - A run that reaches the For each step again, through a later step looping back, runs the held steps again for each item of the list as it stands then. The second pass's visits are keyed `<step>@item<n>@pass2`, or `<step>@item<n>@pass2@<visit>` for a further visit, and the first pass's keys carry no pass. Each pass's outputs are recorded under the For each step's own visit, such as `variants` and `variants@2`, and a step after the box reads the latest pass.
610
+ - A step after the box reads `result`, `items`, `passed` or `failed` through an input naming the For each step and that output.
611
+
612
+ ```ruby
613
+ run.ready_steps.map(&:id)
614
+ # => ["write@item2", "write@item3", "write@item4"]
615
+ step = run.next_step(run.recorded)
616
+ step.id
617
+ # => "write@item2"
618
+ EasyFlow::Inputs.values_of(step, run.recorded.stringify_keys, run: run)
619
+ # => { "variant" => "long" }
620
+ run.record(step.id, { "result" => "passed", "title" => "Long copy" })
621
+ run.advance
622
+ run.recorded[:variants]
623
+ # => { "result" => "passed", "items" => [{ "write" => { ... } }, ...], "passed" => 3, "failed" => 0 }
624
+ ```
625
+
588
626
  ### Read a run and its answers
589
627
 
590
628
  1. Find runs scoped to one host so one host never sees another's: `EasyFlow::Run.joins(:flow).where(easy_flow_definitions: { host: "intake" })`.
@@ -625,7 +663,7 @@ Use this when a visitor's run should stop until a set time has passed, such as a
625
663
  ## Conventions
626
664
 
627
665
  - A step type's id comes from its class name or the id passed to `EasyFlow.step`, and it is stored in flow documents, so renaming the class or id breaks every flow that uses it.
628
- - Never give a step type one of the built-in ids: `start`, `terminal`, `question`, `checklist`, `multiple_select`, `fill_in_the_blank`, `file_upload`, `condition`, `switch`, `compare`, `compare_two_values`, `all_or_any`, `count`, `read_data`, `write_data`, `wait`, `flow_step`.
666
+ - Never give a step type one of the built-in ids: `start`, `terminal`, `question`, `checklist`, `multiple_select`, `fill_in_the_blank`, `file_upload`, `condition`, `switch`, `compare`, `compare_two_values`, `all_or_any`, `count`, `read_data`, `write_data`, `wait`, `flow_step`, `for_each`.
629
667
  - Register class-based step types inside `to_prepare`. A type registered anywhere else is lost on code reload in development.
630
668
  - Registering a type does not put it in the palette of a host that names a list of offered step types without the type's id. Which step types a host offers is set through `easy_flow-install`, not here.
631
669
  - A type that declares `ends_here` is in every host's palette, and a type that declares `begins_here` is in no host's palette.
@@ -646,12 +684,13 @@ Use this when a visitor's run should stop until a set time has passed, such as a
646
684
  - A `:previous_step` setting is required unless declared `optional: true`, and an `outputs_of:` setting must be filled once the step it reads is picked, or the flow cannot be saved as a version or published.
647
685
  - Only the canvas's Create version and Publish requests remove unconnected steps, and only when the request's `remove_unconnected` names exactly the steps unconnected at that moment. `Definition#publish`, `Definition#create_version` and `Validator#violations` never remove one, and `violations` lists each as `unreachable`, so app code that checks `violations` before publishing goes on refusing such a flow. A request that names none is answered with status 422, the refusal in `error`, and the steps in `unconnected`.
648
686
  - A type that routes declares the values its output takes, or the canvas offers no connections to label.
649
- - A step of a `holds_steps` type keeps the ids of the steps it holds in `node.config["holds"]`, never declares a setting named `holds`, and changes nothing about how a run moves, so what it does with the steps it holds is the type's own code.
687
+ - A step of a `holds_steps` type keeps the ids of the steps it holds in `node.config["holds"]`, never declares a setting named `holds`, and changes nothing about how a run moves, so what it does with the steps it holds is the type's own code, apart from the built-in For each step, which runs them once per item.
650
688
  - An output named by `outputs_named_by` takes its name from an entry's `name`, lists no values, and is never a connection leaving the step.
651
689
  - A run starts only on a version its own flow has published and not retired or withdrawn, and any other version raises `ActiveRecord::RecordInvalid`.
652
690
  - Always read a run against `run.pinned_definition`, never the flow's live version, since a run keeps the version it started on after a new one is published.
653
691
  - Always look flows and runs up through a host.
654
- - In a flow that loops, a later visit's answer is keyed `<id>@<n>`, so code that reads answers never assumes one answer per step id. A step id may not contain `@`, and a flow with one is refused when published.
692
+ - In a flow that loops, a later visit's answer is keyed `<id>@<n>`, and a step held by a For each step is keyed `<id>@item<n>`, `<id>@item<n>@<visit>`, or with `@pass<n>` after the item on a later pass, so code that reads answers never assumes one answer per step id, and takes the part of a key before the first `@` to find its step.
693
+ - A Count step and any code that reads every visit of a step together count a held step's visits across all items. A step id may not contain `@`, and a flow with one is refused when published.
655
694
  - A loop that comes back to a step with no answer given since the last visit to it ends the flow there and `run.stopped_on_loop` names that step, so every loop needs a step that awaits input.
656
695
  - `run.ended_at_end?` is `true` only for a run whose last step reached is of an `ends_here` type, so a run a loop stopped, or one that ended at a step with no connection to follow, is never counted as ended at an End step.
657
696
  - A step's input field is named `answers[<step.id>]` for one value or `answers[<step.id>][]` for several. Any other name is ignored, and a hash is treated as blank.
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: easy_flow-info
3
- description: Use to learn what easy_flow offers — guided flows an admin draws on a canvas and a visitor runs one step at a time, versioned documents of steps and connections, flows that loop back to ask a step again, a loop that stops a run when nothing new was answered and the step it came back to, whether a run ended at an End step, hosts and the step types each offers its admins, runs and the published version each starts on, each visit a run records with its value and the time the step was completed, and the output a flow gives when it ends, the built-in step types including questions and checklists that can require an answer and explain each answer behind an info button, file upload steps that hand a visitor's file to the app's file store and read it back by its name, and branching on an answer, including one named output of a step that recorded several, on comparing a number with an amount, or on comparing two earlier answers or one earlier answer with an amount alone, steps flagged when they name an earlier step without choosing which of its outputs to read, branching on a list of such comparisons that must all hold or of which any one must hold, branching on the hour, weekday or minute a step was completed, Wait steps that hold a run until a number of minutes or a time of day after an earlier step was completed, or until a date, and are named after what they wait for, the Check again button a visitor sees at a waiting step in a flow that keeps nothing, Count steps that count how often an earlier step was answered so a loop can give up after a number of tries, Flow steps that name another flow, open its canvas when their card is clicked with a button back to the flow it was opened from, start a run of it for the same owner when a stored run reaches them, are flagged when the flow or version they name cannot run or leads back to the flow they sit in, let the waiting run name that inner run, move that run on along the output it ends with, leave it waiting when the inner run is stopped by a loop, and fail with an error naming the step when its version was taken out of service or its inner run ended with no output, removing a flow and why it is refused while a waiting run depends on it, step types the host app registers, including ones whose outputs are named by the entries an admin lists in one of their settings, ones that start another flow and work out which flow and version to run from their own settings and are named after that flow, or choose it from the run they are in, the answers a host allows a step in a run and the steps it fills in or passes over, the checks they make on an answer, what a step shows a visitor and how that can depend on the visitor's run, and steps that hold a run until something outside the flow has happened, group steps drawn as a box an admin drags steps into and out of, and the settings an admin fills in, including picks whose options come from the app's data, choices drawn as radio cards with a hint under each, and settings kept on one of the app's own records.
3
+ description: Use to learn what easy_flow offers — guided flows an admin draws on a canvas and a visitor runs one step at a time, versioned documents of steps and connections, flows that loop back to ask a step again, a loop that stops a run when nothing new was answered and the step it came back to, whether a run ended at an End step, hosts and the step types each offers its admins, runs and the published version each starts on, each visit a run records with its value and the time the step was completed, and the output a flow gives when it ends, the built-in step types including questions and checklists that can require an answer and explain each answer behind an info button, file upload steps that hand a visitor's file to the app's file store and read it back by its name, and branching on an answer, including one named output of a step that recorded several, on comparing a number with an amount, or on comparing two earlier answers or one earlier answer with an amount alone, steps flagged when they name an earlier step without choosing which of its outputs to read, branching on a list of such comparisons that must all hold or of which any one must hold, branching on the hour, weekday or minute a step was completed, Wait steps that hold a run until a number of minutes or a time of day after an earlier step was completed, or until a date, and are named after what they wait for, the Check again button a visitor sees at a waiting step in a flow that keeps nothing, Count steps that count how often an earlier step was answered so a loop can give up after a number of tries, Flow steps that name another flow, open its canvas when their card is clicked with a button back to the flow it was opened from, start a run of it for the same owner when a stored run reaches them, are flagged when the flow or version they name cannot run or leads back to the flow they sit in, let the waiting run name that inner run, move that run on along the output it ends with, leave it waiting when the inner run is stopped by a loop, and fail with an error naming the step when its version was taken out of service or its inner run ended with no output, removing a flow and why it is refused while a waiting run depends on it, step types the host app registers, including ones whose outputs are named by the entries an admin lists in one of their settings, ones that start another flow and work out which flow and version to run from their own settings and are named after that flow, or choose it from the run they are in, the answers a host allows a step in a run and the steps it fills in or passes over, the checks they make on an answer, what a step shows a visitor and how that can depend on the visitor's run, and steps that hold a run until something outside the flow has happened, group steps drawn as a box an admin drags steps into and out of, the For each step that runs the steps its box holds once per item of a list and records each item's values and how many passed and failed, and the settings an admin fills in, including picks whose options come from the app's data, choices drawn as radio cards with a hint under each, and settings kept on one of the app's own records.
4
4
  tools: Read
5
5
  scope: guided flows — versioned documents of steps and the connections between them, drawn on a canvas by an admin and run by a visitor one step at a time, with step types the host registers
6
6
  ---
@@ -11,7 +11,7 @@ This local explains easy_flow and the words it uses. It makes no changes and giv
11
11
 
12
12
  easy_flow is a Rails engine for guided, branching flows. An admin builds a flow on a canvas by placing steps and connecting them, previews it, and publishes it. A visitor then walks the published flow one step at a time, and each answer decides which step comes next.
13
13
 
14
- Reach for it when an app needs a questionnaire, an intake form, or any decision path that an admin should be able to change without a deploy. The engine ships a start step, an end step, a question step, a checklist step, a multiple select step, a fill in the blank step, a file upload step, five steps that pick a branch from earlier answers, a Count step that counts earlier answers, a Wait step that holds a run until a time, a Flow step that names another flow, and a Play flows step that lets a visitor play several flows in the order they choose. Anything else a flow needs to ask, do, or wait for is a step type the app declares itself.
14
+ Reach for it when an app needs a questionnaire, an intake form, or any decision path that an admin should be able to change without a deploy. The engine ships a start step, an end step, a question step, a checklist step, a multiple select step, a fill in the blank step, a file upload step, five steps that pick a branch from earlier answers, a Count step that counts earlier answers, a Wait step that holds a run until a time, a Flow step that names another flow, a For each step that runs the steps it holds once per item of a list, and a Play flows step that lets a visitor play several flows in the order they choose. Anything else a flow needs to ask, do, or wait for is a step type the app declares itself.
15
15
 
16
16
  ## Interface
17
17
 
@@ -36,7 +36,8 @@ easy_flow declares no commands of its own for this local. Its surface is split b
36
36
  - To let a visitor tick several answers on one step, no code is needed: an admin places a checklist step on the canvas.
37
37
  - To explain an answer to the visitor, no code is needed: an admin fills in that answer's info on a question or checklist step.
38
38
  - To start another flow from inside a flow, no code is needed: an admin places a Flow step on the canvas and picks that flow and its version. The connections leaving the step are labelled with the ways that flow can end, and a stored run moves on along the one named after the output its inner run ended with. Clicking the step's card opens the canvas of the flow it runs, and that canvas has a button back to the flow it was opened from. A Flow step cannot start the flow it sits in, directly or through other flows.
39
- - To add a step type that holds other steps, such as a "for each" step, use **easy_flow-develop**. easy_flow ships no such step type of its own. Once one is offered, an admin places it on the canvas and drags steps into its box, with no further code.
39
+ - To run some steps once for each item of a list an earlier step recorded, no step type is needed: an admin places a For each step on the canvas, names the list as `<step>.<output>`, and drags the steps into its box. A stored run starts items in list order, keeps up to the step's concurrency of them unfinished at once and names every step ready now through `run.ready_steps`, records each visit under `<step>@item<n>`, and once every item has ended records the For each step's `result`, `items`, `passed` and `failed`, which later steps read as inputs. Under the `stop` rule no item starts after one fails. A list that is missing, is not a list or is longer than the step's item limit, 100 unless set, fails the step with a `reason`. A run that reaches the step again runs its items again as a new pass keyed `<step>@item<n>@pass<n>`. A flow that keeps nothing refuses to run it. Reading those keys from the app's own code is in **easy_flow-develop**.
40
+ - To add another step type that holds other steps, use **easy_flow-develop**. Once one is offered, an admin places it on the canvas and drags steps into its box, with no further code.
40
41
  - To ask a step again, such as repeating a question until the visitor says they are done, no code is needed: an admin connects a later step back to an earlier one on the canvas.
41
42
  - To end a loop after a number of tries, no code is needed: an admin places a Count step after the step being repeated and a Compare step after the Count step, and connects the Compare step's false connection back to the repeated step.
42
43
  - If the question is only what a word below means, this page is the answer.
@@ -57,7 +57,7 @@ A Rails engine for flows an admin draws on a canvas and a visitor runs one step
57
57
  - `visitor_authorization_method` — a method on the base controller, called with the flow, returning true when the visitor may run it. With none set, every visitor is refused.
58
58
  - `refusal_method` — a method on the base controller, called with the refusal error when a visitor is refused or a flow is unpublished or withdrawn. With none set, the response is `404 Not Found`.
59
59
  - `owner_method` — a method on the base controller, called with no arguments, returning the record whose flows the current request works with, such as the signed-in account. When set, the admin pages list, create and edit only that record's flows, a visitor runs only that record's flows, and two records may each hold a flow with the same slug. With none set, every flow in the host is shared by everyone who reaches it. Ask the developer whether each host's flows belong to one record or are shared.
60
- - `offers` — the step types this host's admins can add from the canvas palette, as a list of step type names. With none set, every registered step type is offered. The engine's own names are `question`, `checklist`, `multiple_select`, `fill_in_the_blank`, `file_upload`, `condition`, `switch`, `compare`, `compare_two_values`, `all_or_any`, `count`, `wait`, `flow_step` and `play_flows`, `file_upload` being the File upload step, which needs the file store from step 10, `compare_two_values` being the Compare two values step, which compares the outputs of two earlier steps, `all_or_any` being the All of or Any of step, which decides whether all or any of several such comparisons hold, `count` being the Count step, which counts how often an earlier step was answered, `wait` being the Wait step, which holds a run until a time counted from an earlier answer or until a date, `flow_step` being the Flow step, which names one of the same host's flows, and `play_flows` being the Play flows step, which lists several of the same host's flows for a visitor to play in the order they choose. A step type the app declares is named by the id it was declared with, or after its class when it is a step class, so `Steps::Notify` is `notify`. The End step is offered whether it is listed or not, and the Start step is never offered. Ask the developer which step types each host should offer.
60
+ - `offers` — the step types this host's admins can add from the canvas palette, as a list of step type names. With none set, every registered step type is offered. The engine's own names are `question`, `checklist`, `multiple_select`, `fill_in_the_blank`, `file_upload`, `condition`, `switch`, `compare`, `compare_two_values`, `all_or_any`, `count`, `wait`, `flow_step`, `for_each` and `play_flows`, `file_upload` being the File upload step, which needs the file store from step 10, `compare_two_values` being the Compare two values step, which compares the outputs of two earlier steps, `all_or_any` being the All of or Any of step, which decides whether all or any of several such comparisons hold, `count` being the Count step, which counts how often an earlier step was answered, `wait` being the Wait step, which holds a run until a time counted from an earlier answer or until a date, `flow_step` being the Flow step, which names one of the same host's flows, `for_each` being the For each step, which runs the steps its box holds once per item of a list and needs a flow that saves its runs, and `play_flows` being the Play flows step, which lists several of the same host's flows for a visitor to play in the order they choose. A step type the app declares is named by the id it was declared with, or after its class when it is a step class, so `Steps::Notify` is `notify`. The End step is offered whether it is listed or not, and the Start step is never offered. Ask the developer which step types each host should offer.
61
61
  - `always_show_insert_buttons` — set to `true` to show each connection's button for inserting a step on this host's canvas without the pointer over the connection. A connection's label then sits to the right of the line so the button does not cover it. With none set, the button shows only with the pointer over the connection.
62
62
  - `hide_step_type_codes` — set to `true` to hide step type code names on this host's canvas. The add menu then lists each step type by its name alone, and a step's box shows its step type's name in place of the code name. With none set, the add menu and step boxes show the code names. The settings panel shows a step's id and code name either way.
63
63
  - `canvas_direction` — set to `:left_to_right` to lay this host's canvas out left to right. Steps then sit as compact cards in columns by stage, each step to the right of every step that leads into it apart from connections that loop back. Every connection is drawn as a line, labelled with the answer it is taken on, and branches fan out into separate rows and join again at the step they share. A connection that loops back runs above the steps and one that skips columns runs below them, so no line crosses a card. A group step's box holds its steps as their own small graph, entered from the step before it and left for the step after it. A flow wider than the canvas scrolls sideways inside it. An admin inserts or removes a connection with the buttons on its line, and drops a step or a step type onto the line to put it there. On a screen narrower than 640 pixels, each step sits on a row of its own with its connections listed under it. With none set, or with `:top_to_bottom`, the canvas is laid out top to bottom as before.
@@ -164,7 +164,7 @@ A Rails engine for flows an admin draws on a canvas and a visitor runs one step
164
164
  - A step type of the app's own that starts another flow is treated as a Flow step by every check above: the warnings about a missing flow, a version that cannot run and a flow that leads back, the refusal to remove a flow a waiting run depends on, and the job on `default`. It runs the flow and version its own settings name, or the ones its declaration works out from its other settings, and needs no setting in the initializer beyond being listed in a host's `offers` when that host sets `offers`.
165
165
  - A step type of the app's own whose outputs an admin names on each step offers those names wherever an earlier step's outputs are offered: on a Condition step, on each side of a Compare two values step, and in each comparison of an All of or Any of step. It needs no setting in the initializer beyond being listed in a host's `offers` when that host sets `offers`.
166
166
  - A step type of the app's own that chooses the flow to start from the run it is in is the exception. It never shows the warnings about a missing flow, a version that cannot run or a flow that leads back, so a flow holding it can be published with no flow in its settings. When a run reaches it and it chooses no flow, the engine raises `EasyFlow::InnerFlowError` naming the step and starts no run, and the visitor gets the app's error page. When the run it started ends at an End step with no output, the engine records `ended` as the step's answer and the job on `default` moves the run on from it, rather than failing.
167
- - A step type of the app's own that holds other steps, such as a "for each" step, is drawn on the canvas as a dashed box with a header line at the top and the steps it holds inside. It needs no migration and no setting in the initializer beyond being listed in a host's `offers` when that host sets `offers`. Flows with no such step load and run as before.
167
+ - The built-in For each step and a step type of the app's own that holds other steps are drawn on the canvas as a dashed box with a header line at the top and the steps it holds inside. Neither needs a migration or a setting in the initializer beyond being listed in a host's `offers` when that host sets `offers`. Flows with no such step load and run as before.
168
168
  - After adding a step type that holds other steps to a flow, check that its header line shows the step type's code followed by each filled-in setting, or the type's name and the settings' labels on a host with `hide_step_type_codes` set, and that clicking the header opens the step's settings.
169
169
  - After adding a step type that holds other steps to a flow, check that dragging another step onto its box puts the step inside the box, that dragging it onto the canvas outside the box takes it out, and that both leave the step's connections as they were.
170
170
  - After setting `canvas_direction` to `:left_to_right` on a host, check that its canvas shows the steps in columns with every connection drawn as a labelled line, that a flow wider than the canvas scrolls sideways inside it, and that a phone-width screen shows one step per row with its connections listed under it.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_flow
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.53.0
4
+ version: 0.55.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tylercschneider
@@ -157,6 +157,7 @@ files:
157
157
  - app/models/easy_flow/edge.rb
158
158
  - app/models/easy_flow/edit_history.rb
159
159
  - app/models/easy_flow/flow_step.rb
160
+ - app/models/easy_flow/for_each.rb
160
161
  - app/models/easy_flow/inputs.rb
161
162
  - app/models/easy_flow/layout.rb
162
163
  - app/models/easy_flow/name.rb
@@ -191,6 +192,7 @@ files:
191
192
  - app/models/easy_flow/version.rb
192
193
  - app/models/easy_flow/violation.rb
193
194
  - app/models/easy_flow/violation_sentence.rb
195
+ - app/models/easy_flow/visit_key.rb
194
196
  - app/models/easy_flow/wait.rb
195
197
  - app/models/easy_flow/write_data.rb
196
198
  - app/views/easy_flow/flows/complete.html.erb