Macaw.Overlay.Policy (macaw v0.6.0)

Copy Markdown View Source

The host's say over one source's overlay (spec/13 §3.1, #453): who may change its rows, how a row is judged, where the changes are held and how many. The host knows the viewer and the source, so the policy is the host's, given to Macaw.State.new/2 as overlays: by source name, as it binds the source itself; a document never says it.

  • rules: (required), the host's row rules: a function of one row, a map of each field to its value, answering :ok or {:error, faults}, each {[field | path], reason}; a reason that is text is shown as it is, at its field. It is called only with a row of the schema's types, after macaw's own checks pass.
  • write: (required), whether this viewer may change the rows, a boolean: the host judges its viewer once, as it builds the visit's state. false offers no form and no remove control, and a change sent by hand is :forbidden.
  • held:, where the changes live: :visit (the default), in the visit's state alone, gone on reload, carried between requests with no socket by the page's own forms; or :session, also kept by the host's session persistence (Macaw.State.Persistence.Session), surviving reloads within the session. A URL never carries an overlay.
  • filters:, how the source's request params select rows, so the rows the visitor added answer a request as the source's do: each param the source declares, mapped to the field it selects by equality, or to nil for a param that selects no rows; %{} by default.
  • max:, the most changes the overlay holds, added and removed rows together, a positive integer, 100 by default.

Set wrong, it fails loudly: new/1 answers every fault by key, and Macaw.State.new/2 refuses a policy missing for an overlaid source, one given for a source the surface doesn't overlay, and filters: that leave a declared param out or name a field the schema lacks.

iex> {:ok, policy} = Macaw.Overlay.Policy.new(rules: fn _row -> :ok end, write: true)
iex> {policy.held, policy.max}
{:visit, 100}
iex> Macaw.Overlay.Policy.new(write: "yes", held: :disk)
{:error, [{[:held], {:enum, [:visit, :session]}}, {[:rules], :required}, {[:write], {:type, :boolean}}]}

Summary

Functions

A policy of the keys given: {:ok, policy}, or every fault at once, by key: a key none of the five, :unknown_key; rules missing, :required, or not a function of one argument, :not_a_function; write missing, :required, or not a boolean, {:type, :boolean}; held neither :visit nor :session, {:enum, [:visit, :session]}; filters not a map of names to names or nil, {:type, :map} or {:type, :name} at its key; max not a positive integer, {:type, :pos_integer}. Opts that aren't a keyword list are {[], {:type, :keyword}}.

Types

t()

@type t() :: %Macaw.Overlay.Policy{
  filters: %{required(String.t()) => String.t() | nil},
  held: :visit | :session,
  max: pos_integer(),
  rules: (map() -> :ok | {:error, [Macaw.Base.Fault.t()]}),
  write: boolean()
}

Functions

new(policy)

@spec new(term()) :: {:ok, t()} | {:error, [Macaw.Base.Fault.t(), ...]}

A policy of the keys given: {:ok, policy}, or every fault at once, by key: a key none of the five, :unknown_key; rules missing, :required, or not a function of one argument, :not_a_function; write missing, :required, or not a boolean, {:type, :boolean}; held neither :visit nor :session, {:enum, [:visit, :session]}; filters not a map of names to names or nil, {:type, :map} or {:type, :name} at its key; max not a positive integer, {:type, :pos_integer}. Opts that aren't a keyword list are {[], {:type, :keyword}}.