Skip to content

Commit

Permalink
we can render generated/state_table.rb.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Mar 13, 2024
1 parent c1ef889 commit 06b31ae
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 2 deletions.
38 changes: 38 additions & 0 deletions lib/trailblazer/workflow/generate/state_guards.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module Trailblazer
module Workflow
module Generate
# State guards are code blocks defined by the user to allow/deny executing
# a catch event.
module StateGuards
module_function

def call(iteration_set, namespace:, **options)
states = StateTable.aggregate_by_state(iteration_set)
cli_rows = StateTable.render_data(states, **options)

available_states = cli_rows.collect do |row|
{
suggested_state_name: row["state name"],
key: row[:catch_events].collect { |position| Present.id_for_position(position) }.uniq.sort
}
end

# formatting, find longest state name.
max_length = available_states.collect { |row| row[:suggested_state_name].inspect.length }.max

state_guard_rows = available_states.collect do |row|
id_snippet = %(, id: #{row[:key].inspect}) # TODO: move me to serializer code.

%( #{row[:suggested_state_name].inspect.ljust(max_length)} => {guard: ->(ctx, process_model:, **) { raise "implement me!" }#{id_snippet}},)
end.join("\n")

snippet = %(#{namespace}::StateGuards = Trailblazer::Workflow::Collaboration::StateGuards.from_user_hash({
#{state_guard_rows}
})
)
end
end
end
end # Generate
end
end
43 changes: 43 additions & 0 deletions lib/trailblazer/workflow/generate/state_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module Trailblazer
module Workflow
class Generate
# state_table is a datastructure connecting the event label with the IDs
# of the possible catch events, so we can retrieve the start positions.
#
# This generates app/concepts/posting/collaboration/generated/state_table.rb
class StateTable < Trailblazer::Activity::Railway
step Subprocess(Introspect::StateTable)
step :render
# Generate code stubs for a state table.

def render(ctx, rows:, namespace:, **)
available_states = rows.collect do |row|
{
suggested_state_name: row["state name"],
key: row[:catch_events].collect { |position| Introspect::Present.id_for_position(position) }.uniq.sort
}
end

# formatting, find longest state name.
max_length = available_states.collect { |row| row[:suggested_state_name].inspect.length }.max

state_guard_rows = available_states.collect do |row|
id_snippet = %(id: #{row[:key].inspect}) # TODO: move me to serializer code.

%( #{row[:suggested_state_name].inspect.ljust(max_length)} => {#{id_snippet},)
end.join("\n")

snippet = %(# This file is generated by trailblazer-workflow.
module #{namespace}::Generated
StateTable = {
#{state_guard_rows}
}
end
)

ctx[:snippet] = snippet
end
end
end # Generate
end
end
26 changes: 26 additions & 0 deletions test/generate_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
require "test_helper"
require "json"

class GenerateStateTableTest < Minitest::Spec
it "what" do
iteration_set, lanes_cfg = fixtures()

_, (ctx, _) = Trailblazer::Workflow::Generate::StateTable.invoke([{iteration_set: iteration_set, lanes_cfg: lanes_cfg, namespace: "App::Posting"}, {}])

# puts ctx[:snippet]
assert_equal ctx[:snippet],
%(# This file is generated by trailblazer-workflow.
module App::Posting::Generated
StateTable = {
"⏸︎ Create form" => {id: ["catch-before-Activity_0wc2mcq"],
"⏸︎ Create" => {id: ["catch-before-Activity_1psp91r"],
"⏸︎ Update form♦Notify approver" => {id: ["catch-before-Activity_1165bw9", "catch-before-Activity_1dt5di5"],
"⏸︎ Update" => {id: ["catch-before-Activity_0j78uzd"],
"⏸︎ Delete? form♦Publish" => {id: ["catch-before-Activity_0bsjggk", "catch-before-Activity_0ha7224"],
"⏸︎ Revise form" => {id: ["catch-before-Activity_0zsock2"],
"⏸︎ Delete♦Cancel" => {id: ["catch-before-Activity_15nnysv", "catch-before-Activity_1uhozy1"],
"⏸︎ Archive" => {id: ["catch-before-Activity_0fy41qq"],
"⏸︎ Revise" => {id: ["catch-before-Activity_1wiumzv"],
}
end
)
end
end

class GenerateTest < Minitest::Spec
# UNIT TEST {Generate::Representer}
it "works with PRO's JSON format" do
Expand Down
6 changes: 4 additions & 2 deletions test/introspect_test.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require "test_helper"

class IntrospectStateTableTest < Minitest::Spec
it "StateTable.call" do
it "StateTable.call produces {:rows}" do
iteration_set, lanes_cfg = fixtures()

cli_state_table = Trailblazer::Workflow::Introspect::StateTable.invoke([{iteration_set: iteration_set, lanes_cfg: lanes_cfg}, {}])
signal, (ctx, _) = Trailblazer::Workflow::Introspect::StateTable.invoke([{iteration_set: iteration_set, lanes_cfg: lanes_cfg}, {}])

assert_equal ctx[:rows].collect { |row| row["state name"] }.inspect,
%(["⏸︎ Create form", "⏸︎ Create", "⏸︎ Update form♦Notify approver", "⏸︎ Update", "⏸︎ Delete? form♦Publish", "⏸︎ Revise form", "⏸︎ Delete♦Cancel", "⏸︎ Archive", "⏸︎ Revise"])
end

it "StateTable.render" do
Expand Down

0 comments on commit 06b31ae

Please sign in to comment.