GenDurable.State (gen_durable v0.1.2)

Copy Markdown View Source

Typed FSM state: an Ecto embedded schema per FSM, encoded to/from jsonb. Typically defined as a nested State module inside the FSM, where it is adopted by convention (see GenDurable.FSM):

defmodule Checkout do
  use GenDurable.FSM, version: 1

  defmodule State do
    use GenDurable.State

    embedded_schema do
      field :order, :integer
      field :n, :integer, default: 0
    end
  end
end

The engine loads the jsonb column into the struct before step/2 (from_db/2) and dumps the returned struct back to jsonb on the outcome (to_db/2). FSMs may also run with no state module, in which case state is a plain string-keyed map (allowed, but unsupported — you are on your own).

Summary

Functions

Normalize user-supplied state (for insert) into a JSON string. Accepts the state struct, or a map (atom or string keys) which is cast through the schema.

Load a DB jsonb value (raw string or already-decoded map) into the FSM state.

Dump an FSM state value returned by a step into a JSON string for a jsonb param.

Functions

cast(module, value)

Normalize user-supplied state (for insert) into a JSON string. Accepts the state struct, or a map (atom or string keys) which is cast through the schema.

from_db(module, data)

Load a DB jsonb value (raw string or already-decoded map) into the FSM state.

to_db(module, struct)

Dump an FSM state value returned by a step into a JSON string for a jsonb param.