Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/statifier_examples/charts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ defmodule StatifierExamples.Charts do
[
CardAuth.block_types(),
Signup.block_types(),
Library.block_types(),
Messaging.block_types(),
%{TypedSendStep.type_name() => TypedSendStep}
]
Expand Down
14 changes: 14 additions & 0 deletions lib/statifier_examples/library.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ defmodule StatifierExamples.Library do
them to the fixtures every page offers, for the reason the card and
signup domains each keep their own list: which documents a domain ships
is a fact about the domain.

The one block type the library world registers, `myapp.setup`
(`block_types/0`), is used by neither document: it is the root of a
branch library setup that can end more than one way
(`StatifierExamples.Library.Setups`).
"""

alias StatifierExamples.Charts.Fixture
alias StatifierExamples.Library.Setup

# `{key, file}`. Listed rather than globbed, as `StatifierExamples.CardAuth`
# lists its own: a stray file in `priv/` should not silently become an
Expand All @@ -33,4 +39,12 @@ defmodule StatifierExamples.Library do
"""
@spec fixtures() :: [Fixture.t()]
def fixtures, do: Enum.map(@documents, fn {key, file} -> Fixture.load!(key, file) end)

@doc """
The library world's own block type, as a `type_name => module` map: the
root a branch library setup that can end more than one way is built on
(`StatifierExamples.Library.Setup`).
"""
@spec block_types() :: %{optional(String.t()) => module()}
def block_types, do: %{"myapp.setup" => Setup}
end
11 changes: 7 additions & 4 deletions lib/statifier_examples/library/onboarding.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ defmodule StatifierExamples.Library.Onboarding do
options in), and the Path goes on to its closing screen once the last
one has finished.

The subchart declares the one outcome `done`, which is the one root
outcome each setup declares (`StatifierExamples.Library.Setups`' "How a
setup finishes"). statifier_blocks refuses, at child creation, a child
whose root declares an outcome the block did not.
The subchart declares `done` and `site_visit_needed`, every root outcome
a setup declares (`StatifierExamples.Library.Setups`' "How a setup
finishes"): statifier_blocks refuses, at child creation, a child whose
root declares an outcome the block did not. It writes what each setup
finished with to `last_setup`, the setup's done data, whose `outcome` is
the setup's root outcome, so the onboarding can read how the setup of
the latest pass ended.

## The allow-list: `resolve_chart/2`

Expand Down
167 changes: 167 additions & 0 deletions lib/statifier_examples/library/setup.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
defmodule StatifierExamples.Library.Setup do
@moduledoc """
`myapp.setup`: the root of a branch library setup that can end more than
one way.

A setup's steps are `myapp.step` blocks (`StatifierExamples.Signup.ReturnableStep`),
and a step finishes as `done` whichever button was pressed, so a setup
whose root is a `core.sequence` declares the one root outcome `done` and
how it went lives only in its `responses`. A parent that runs the setup
as a child (`StatifierExamples.Library.Onboarding`) sees nothing of a
child but the root outcome it finished with, so a setup that should be
able to tell its parent "this needs a site visit" says it through this
root instead.

## What an author configures

The steps go in the `body` slot, in order, exactly as they went in a
sequence's `body`. Beside them, three params name the one other way the
setup can end:

* `ending` - the outcome it ends with, such as `site_visit_needed`.
Blank, the setup declares `done` alone and runs as a sequence would.
* `ending_on` - the outcome of the button whose press can end it.
* `ending_when` - a condition on that press, read from what the press
carried (`_event.data.<question key>`), so the press ends the setup
only when the answer calls for it. Blank, every press of the button
ends it.

The press is recorded first, as every press is: the step it belongs to
writes its responses, and then the whole setup ends with `ending`,
whatever steps are left. A setup whose steps all finish without that
press ends with `done`.

## What it is made of

A **composite**: a `core.group` whose `body` this type exposes as its own
pass-through slot (`statifier_blocks`' ADR-0002 pass-through amendment),
so the author's steps keep their own ids, and whose `interrupts` hold one
`core.on_event` when an ending is named. The handler listens for the
button's press, `StatifierExamples.Signup.Screen.outcome_event/1`, under
the configured condition, abandons the group and names the outcome it
finishes with under `finish_as` (the ADR's `C8`). `declared_outcomes/1`
answers `done` and then the ending, from the block's own config (`C9`),
which is what a compile for use as a child reads as the document's root
outcomes, one top-level `<final>` each (ADR-0004's Amendment of
2026-08-29, `C1`).

The handler runs in a region of its own beside the body, so the step's
own transition on the same press is taken too: the step records the
press and the group then leaves with the ending.
"""

use StatifierBlocks.Composite,
name: "myapp.setup",
params: [
%{
key: "ending",
type: :string,
label: "It can also end with",
required?: false,
default: ""
},
%{
key: "ending_on",
type: :string,
label: "When this button is pressed",
required?: false,
default: ""
},
%{
key: "ending_when",
type: :expression,
label: "And this holds of the press",
required?: false,
default: ""
}
],
slots: [%{name: "body", to: {"setup", "body"}, label: "Steps"}],
sentence: "Run the setup's steps",
palette_entry: %{
label: "Setup",
group: "Library",
description:
"Runs a setup's steps; a button's press may end it with an outcome of its own.",
icon: "wrench-screwdriver",
keywords: ["setup", "steps", "outcome", "ending"],
order: 0
},
version: 1

alias StatifierBlocks.Block
alias StatifierExamples.Signup.Screen

@doc """
The group the author's steps land in, with the ending's handler on its
rail when the config names an ending.

Pure, and the ids are local. The group's `body` is written empty, which
is what makes it a legal pass-through target.
"""
@impl StatifierBlocks.Composite
def subtree(params) do
handlers =
case ending(params) do
nil -> []
ending -> [handler(ending, params)]
end

[Block.new("core.group", id: "setup", slots: %{"body" => [], "interrupts" => handlers})]
end

@doc """
The outcomes this block declares: `done`, then the ending when the config
names one.

iex> StatifierExamples.Library.Setup.declared_outcomes(%{
...> "ending" => "site_visit_needed",
...> "ending_on" => "test_reported"
...> })
["done", "site_visit_needed"]

iex> StatifierExamples.Library.Setup.declared_outcomes(%{"ending" => ""})
["done"]
"""
@impl StatifierBlocks.Composite
def declared_outcomes(config) do
case ending(config) do
nil -> ["done"]
ending -> ["done", ending]
end
end

@spec handler(String.t(), map()) :: Block.t()
defp handler(ending, params) do
Block.new("core.on_event",
id: "ending",
config: %{
"event" => Screen.outcome_event(text(params, "ending_on")),
"payload" => "",
"cond" => text(params, "ending_when"),
"outcome" => "abandon",
"finish_as" => ending,
"capture" => %{}
}
)
end

# The ending a config names, or `nil` for a blank one or one with no
# button to end on, so a config still being typed into answers no handler
# rather than one listening for nothing.
@spec ending(map()) :: String.t() | nil
defp ending(config) do
case {text(config, "ending"), text(config, "ending_on")} do
{"", _button} -> nil
{_ending, ""} -> nil
{ending, _button} -> ending
end
end

@spec text(map(), String.t()) :: String.t()
defp text(config, key) do
case Map.get(config, key) do
value when is_binary(value) -> value
_absent -> ""
end
end
end
64 changes: 49 additions & 15 deletions lib/statifier_examples/library/setups.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,31 @@ defmodule StatifierExamples.Library.Setups do

## How a setup finishes

Each document's root is a `core.sequence`, so it declares one root
outcome, `done`, and a document compiled for use as a child finishes
with it (`outcomes/1`). What happened inside a setup - which catalogue
source, whether the reader read a test card - is in its `responses`,
where each step's press recorded it, and not in the outcome: a
`myapp.step` finishes as `done` whichever button was pressed.
A setup whose root is a `core.sequence` declares one root outcome,
`done`, and a document compiled for use as a child finishes with it
(`outcomes/1`). The catalogue sync and the reading-list import are such
setups. What happened inside one - which catalogue source, which term -
is in its `responses`, where each step's press recorded it, and not in
the outcome: a `myapp.step` finishes as `done` whichever button was
pressed.

A setup that should be able to tell its parent how it went has a
`myapp.setup` root instead (`StatifierExamples.Library.Setup`), which
declares `done` and the one ending its config names. The card-reader
setup is one: a Finish on its test-card screen saying the reader read
nothing ends it with `site_visit_needed`, which `outcomes/1` answers
beside `done` and the onboarding reads
(`StatifierExamples.Library.Onboarding`).

## What a librarian reads

Each document's `metadata` description is written for whoever edits the
document: it names block types and fixture files. The settings page
draws `description/1` instead, one sentence per setup for the librarian
choosing which to run. It is kept here, beside the one list, rather than
in the documents, because which setups a page offers and how it words
them is this list's, and the document's own description already has a
reader: the person editing it.

None of the three is among the fixtures `StatifierExamples.Charts.fixtures/0`
lists. Like the patron registration steps, each is run as a supplied
Expand All @@ -38,13 +57,16 @@ defmodule StatifierExamples.Library.Setups do
alias StatifierBlocks.{Decode, Document}
alias StatifierExamples.Charts.Subchart

# `{key, path file, screens file}`, in the order a page offers them.
# Listed rather than globbed, as `StatifierExamples.Library` lists its
# documents: a stray file in `priv/` should not become a setup.
# `{key, path file, screens file, description}`, in the order a page
# offers them. Listed rather than globbed, as `StatifierExamples.Library`
# lists its documents: a stray file in `priv/` should not become a setup.
@setups [
{"catalogue_sync", "catalogue_sync.json", "catalogue_sync_screens.json"},
{"reading_list_import", "reading_list_import.json", "reading_list_import_screens.json"},
{"card_reader_setup", "card_reader_setup.json", "card_reader_setup_screens.json"}
{"catalogue_sync", "catalogue_sync.json", "catalogue_sync_screens.json",
"Keep this branch's catalogue in step: choose where its records come from, how often they sync and who hears when a sync fails."},
{"reading_list_import", "reading_list_import.json", "reading_list_import_screens.json",
"Bring in the reading lists local schools send for a term, and choose whether their titles are held at the branch."},
{"card_reader_setup", "card_reader_setup.json", "card_reader_setup_screens.json",
"Set up the branch's library-card reader and check it reads a card; a reader that reads nothing is sent for a site visit."}
]

@doc "The setups' keys, in the order a page offers them."
Expand All @@ -57,21 +79,32 @@ defmodule StatifierExamples.Library.Setups do
"""
@spec document(String.t()) :: {:ok, Document.t()} | :error
def document(key) do
with {:ok, {_key, path_file, _screens_file}} <- setup(key) do
with {:ok, {_key, path_file, _screens_file, _description}} <- setup(key) do
case Decode.decode(File.read!(fixture(path_file))) do
{:ok, document} -> {:ok, document}
{:error, error} -> raise "#{path_file} is not a block document: #{inspect(error)}"
end
end
end

@doc """
What the setup does, in one sentence for a librarian (the moduledoc's
"What a librarian reads"), or `:error` for a key this module does not
list.
"""
@spec description(String.t()) :: {:ok, String.t()} | :error
def description(key) do
with {:ok, {_key, _path_file, _screens_file, description}} <- setup(key),
do: {:ok, description}
end

@doc """
The screens document the setup's steps name, decoded: `{:ok, screens}`,
or `:error` for a key this module does not list.
"""
@spec screens(String.t()) :: {:ok, map()} | :error
def screens(key) do
with {:ok, {_key, _path_file, screens_file}} <- setup(key),
with {:ok, {_key, _path_file, screens_file, _description}} <- setup(key),
do: {:ok, screens_file |> fixture() |> File.read!() |> Jason.decode!()}
end

Expand Down Expand Up @@ -116,7 +149,8 @@ defmodule StatifierExamples.Library.Setups do
end
end

@spec setup(String.t()) :: {:ok, {String.t(), String.t(), String.t()}} | :error
@spec setup(String.t()) ::
{:ok, {String.t(), String.t(), String.t(), String.t()}} | :error
defp setup(key) do
case List.keyfind(@setups, key, 0) do
nil -> :error
Expand Down
7 changes: 5 additions & 2 deletions lib/statifier_examples_web/live/settings_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ defmodule StatifierExamplesWeb.SettingsLive do
The page lists what `StatifierExamples.Library.Setups` lists - a
catalogue sync, a reading-list import and a card-reader setup - with
each one's name, what it does and the root outcomes its document
declares. Pressing a setup's Start starts it alone as a durable Journey
declares. What it does is the sentence `StatifierExamples.Library.Setups`
keeps for a librarian (its "What a librarian reads"), not the document's
own description, which is written for whoever edits the document. Pressing a setup's Start starts it alone as a durable Journey
under the librarian's branch scope, through
`StatifierExamplesWeb.JourneyHost.start/2`, and sends the reader to the
Journey page
Expand Down Expand Up @@ -116,11 +118,12 @@ defmodule StatifierExamplesWeb.SettingsLive do
for key <- Setups.keys() do
{:ok, %Document{metadata: metadata}} = Setups.document(key)
{:ok, outcomes} = Setups.outcomes(key)
{:ok, description} = Setups.description(key)

%{
key: key,
name: Map.get(metadata, "name", key),
description: Map.get(metadata, "description", ""),
description: description,
outcomes: outcomes
}
end
Expand Down
Loading
Loading