module RatatuiRuby::Tea::Router

Declarative DSL for Fractal Architecture.

Large applications decompose into bags. Each bag has its own Model, UPDATE, and VIEW. Parent bags route messages to child bags and compose views. Writing this routing logic by hand is tedious and error-prone.

Include this module to declare routes and keymaps. Call from_router to generate an UPDATE lambda that handles routing automatically.

A bag is a module containing Model, INITIAL, UPDATE, and VIEW constants. Bags compose: parent bags delegate to child bags.

Example

class Dashboard
  include Tea::Router

  route :stats, to: StatsPanel
  route :network, to: NetworkPanel

  keymap do
    key "s", -> { SystemInfo.fetch_command }, route: :stats
    key "q", -> { Command.exit }
  end

  Model = Data.define(:stats, :network)
  INITIAL = Model.new(stats: StatsPanel::INITIAL, network: NetworkPanel::INITIAL)
  VIEW = ->(model, tui) { ... }
  UPDATE = from_router
end