soma.cell

Document Lifecycle

Documents move through draft -> review -> published states.

Example 064 of the basic Soma programs: a small, commented program that teaches one thing. Every output below was printed by soma 3.2.4 when this page was built.

How it is built

The cell DocumentLifecycle keeps 2 memory slots: docs (Map<String, String>, persistent and consistent) and status (Map<String, String>, persistent and consistent). Its state machine doc_status starts in draft and allows 5 transitions: draft -> review, review -> published, review -> draft, published -> archived and * -> cancelled. archived and cancelled are terminal. Handlers: create (line 30) and demo (line 37).

Generated from soma describe on this file.

The source

064_document_lifecycle.cell

// ============================================================================
// 064 — Document Lifecycle
// ============================================================================
// Documents move through draft -> review -> published states.
//
// Run:   soma run 064_document_lifecycle.cell demo
// Expect:
//   Created: Design Spec v1
//   Draft -> review
//   Review -> published
//   Valid transitions from published: ["archived", "cancelled"]

cell DocumentLifecycle {

    memory {
        docs: Map<String, String> [persistent, consistent]
        status: Map<String, String> [persistent, consistent]
    }

    state doc_status {
        initial: draft

        draft -> review
        review -> published
        review -> draft
        published -> archived
        * -> cancelled
    }

    on create(title: String) {
        let id = to_string(next_id())
        docs.set(id, title)
        status.set(id, "draft")
        return map("id", id, "title", title, "status", "draft")
    }

    on demo() {
        let doc = create("Design Spec v1")
        print("Created: {doc.title}")

        transition("1", "review")
        print("Draft -> review")

        transition("1", "published")
        print("Review -> published")

        let next = valid_transitions("1")
        print("Valid transitions from published: {next}")

        return "done"
    }
}

What the toolchain says

$ soma check 064_document_lifecycle.cell
✓ All checks passed.
$ soma run 064_document_lifecycle.cell demo
Created: Design Spec v1
Draft -> review
Review -> published
Valid transitions from published: ["archived", "cancelled"]
done