Macaw.State.Source (macaw v0.6.0)

Copy Markdown View Source

A state source a surface declares (spec/03 §1.1): %Macaw.State.Source{type, values, initial, scope, persist, snapshot}.

  • type, a data-model type, as a parameter's: :time, :number, :category, :text, :color or :point; required;
  • values, the closed list of legal values, each of the type, or nil for any value of the type;
  • values_from, legal values from data (spec/03 §1.7, spec/07 §2.2): "source.field", the current values of a field of one of the host's sources, checked at each change, or nil; never beside values;
  • multi, whether it holds several values, a list of distinct values of the type, false by default, and max, the most it holds, a positive integer, or nil for no bound;
  • initial, the value before any event: a literal of the type and among values, nil for none, {:host, key}, a value the host gives under key, a name, or {:param, name}, the value of the page param name (spec/06 §5);
  • scope, where it lives (spec/03 §2, §2.1): :surface, one surface instance's, the default; :page, the page's; :region, a persistent region's content's; :session, the visit's; :viewer, the viewer's, across visits; :shared, one value for every viewer of the surface, held by its owner; or :local, the browser's only, declared but never held on the server;
  • persist, whether a host's persistence keeps it (spec/03 §3): true by default, false to keep it out (a typed search, anything personal);
  • snapshot, whether a state snapshot holds it (spec/03 §4): true by default, false to keep it out of snapshots even when it persists;
  • from, nil, or {:export, surface, name} for a mirror of another surface's export (spec/03 §5.2): its value the export's, put by the page, never set by an action, never persisted or snapshotted, at :surface scope and with no initial.

A literal given as an atom, for a :category or :text source, is read as its spelling, and no atom is made.

Summary

Functions

A value judged as this source's (spec/03 §1.5): {:ok, value}, nil for no value, a :category or :text atom read as its spelling; {:error, {:type, type}} for one not of the type, {:error, {:not_allowed, value}} for one outside values.

A state source from a map or keyword list with atom keys: {:ok, source} or every fault at once, by path from the entry.

The entry as a document writes it (spec/03 §1.1): its map, the defaults left out.

The scopes a state source may live in (spec/03 §2.1), the seven: :local, :surface, :page, :region, :session, :viewer and :shared.

The types a state source may have.

Types

t()

@type t() :: %Macaw.State.Source{
  from: {:export, String.t(), String.t()} | nil,
  initial: term() | {:host, String.t()} | {:param, String.t()} | nil,
  max: pos_integer() | nil,
  multi: boolean(),
  persist: boolean(),
  scope: :local | :surface | :page | :region | :session | :viewer | :shared,
  snapshot: boolean(),
  type: :time | :number | :category | :text | :color | :point,
  values: [term(), ...] | nil,
  values_from: String.t() | nil
}

Functions

judge(source, given)

@spec judge(t(), term()) ::
  {:ok, term()} | {:error, {:type, atom()} | {:not_allowed, term()}}

A value judged as this source's (spec/03 §1.5): {:ok, value}, nil for no value, a :category or :text atom read as its spelling; {:error, {:type, type}} for one not of the type, {:error, {:not_allowed, value}} for one outside values.

A multi source's value is a list of distinct values of the type, each judged as a single one is, [] read as nil: a term that isn't a list is {:error, {:type, {:list, type}}}, as is a list holding a value not of the type; a value given twice {:error, {:duplicate, value}}; more than max values {:error, {:limit, :max, max, count}}. Whether a value is among a values_from source's current values is the request-time check's (spec/07 §2.2), never this one's.

iex> source = %Macaw.State.Source{type: :category, values: ["asc", "desc"]}
iex> Macaw.State.Source.judge(source, :desc)
{:ok, "desc"}
iex> Macaw.State.Source.judge(source, "up")
{:error, {:not_allowed, "up"}}
iex> Macaw.State.Source.judge(%Macaw.State.Source{type: :number}, "3")
{:error, {:type, :number}}

new(attrs)

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

A state source from a map or keyword list with atom keys: {:ok, source} or every fault at once, by path from the entry.

iex> Macaw.State.Source.new(%{type: :category, values: ["asc", "desc"], initial: :asc})
{:ok, %Macaw.State.Source{type: :category, values: ["asc", "desc"], values_from: nil, multi: false, max: nil, initial: "asc", scope: :surface, persist: true, snapshot: true, from: nil}}
iex> Macaw.State.Source.new(%{type: :number, initial: "high", scope: :global})
{:error, [{[:initial], {:type, :number}}, {[:scope], {:enum, [:local, :surface, :page, :region, :session, :viewer, :shared]}}]}

node(source)

@spec node(t()) :: map()

The entry as a document writes it (spec/03 §1.1): its map, the defaults left out.

scopes()

@spec scopes() :: [atom(), ...]

The scopes a state source may live in (spec/03 §2.1), the seven: :local, :surface, :page, :region, :session, :viewer and :shared.

types()

@spec types() :: [atom(), ...]

The types a state source may have.