# `AshBackendDrivenOptimisticUi.Rules`
[🔗](https://github.com/Undeadlol1/ash_backend_driven_optimistic_ui/blob/v0.1.0/lib/ash_backend_driven_optimistic_ui/rules.ex#L1)

A resource's rules as JSON-ready data for the client.

Rules are keyed `"<type>.<action>"`, where `<type>` is the resource's
JSON:API type or, without one, its module's last segment underscored. Each
rule is `%{"effects" => [effect]}`, effects in order:

  * `%{"set" => %{"attr" => json_logic}, "source" => "derived" | "declared"}`
  * `%{"push" => %{"event" => "xp_awarded", "payload" => %{"key" => json_logic}}}`; the
    payload also gets `"id"`, `{"var": "ctx.push_ids.<event>.<label>"}` (see
    `AshBackendDrivenOptimisticUi.PushId`), except in the rule of a create action
  * `%{"where" => json_logic, "effects" => [effect]}`
  * `%{"insert" => %{"at" => "start" | "end" | %{"sorted_by" => "field"}}}`: put the record
    the sets build into the list, at an end or before the first row whose `field` sorts after its own
  * `%{"remove" => %{}}`: take the record out of the list
  * `%{"clear" => %{"from" => path}}`: empty the nested lists at `path`
  * `%{"edit" => %{"from" => path}}`: apply the sets to the record's own row in the nested lists at `path`

An insert with `"into"`, or a remove with `"from"`, works on a list nested
inside the one record the client holds. The path is a list of steps,
`%{"key" => "exercises"}` or `%{"key" => "exercises", "match" => %{"field" => json_logic}}`,
the latter keeping only the rows whose `field` equals the value.

An expression `AshBackendDrivenOptimisticUi.Compiler` cannot compile raises, so a rule
set is never half-built.

# `for_resource`

```elixir
@spec for_resource(Ash.Resource.t()) :: %{required(String.t()) =&gt; map()}
```

Every `optimistic` of `resource`, keyed `"<type>.<action>"`.

# `reactions_for_resource`

```elixir
@spec reactions_for_resource(Ash.Resource.t()) :: %{required(String.t()) =&gt; [map()]}
```

Every `on` of `resource`, keyed by event name.

# `type`

```elixir
@spec type(Ash.Resource.t()) :: String.t()
```

The JSON:API type of `resource`, or its module's last segment underscored.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
