Macaw.Preset (macaw v0.6.0)

Copy Markdown View Source

A preset: a fragment of one component's settings, kept in the library and referenced by name (spec/07 §1). %Macaw.Preset{name, component, settings, doc}.

  • name, its name: lower-case words joined by _, optionally qualified by its package's prefix with . ("ops.dense_table"); required;
  • component, the name of the component whose settings it holds; required;
  • settings, its settings by parameter name, each the term it is, a binding to state {:state, name} among them; %{} by default;
  • doc, a line for a person, or nil.

A configured component is one fragment stack, macaw_layers' cascade (Macaw.Layers.Fragment.stack/2): the component's defaults, then the preset, then the cell's own settings, the higher layer's value replacing the lower's whole, setting by setting. A cell that carries its settings inline is the same stack without the middle layer, so both validate the same way and explain/1 answers which layer set each value.

A package's presets are its members answering __preset__/0, a keyword list or a map of a preset's keys, which the registry builds at boot (Macaw.Registry.preset/2):

defmodule MyApp.DenseTable do
  def __preset__,
    do: [name: "dense_table", component: "macaw.table", settings: %{"order" => "desc"}]
end

Names are strings, read once here; an atom is read as its spelling, and no atom is made from one.

iex> {:ok, preset} = Macaw.Preset.new(name: :dense_table, component: "macaw.table", settings: %{order: "desc"})
iex> {preset.name, preset.settings}
{"dense_table", %{"order" => "desc"}}
iex> Macaw.Preset.stack(preset, %{"sort" => "v"})
%{"order" => "desc", "sort" => "v"}

Summary

Types

A layer of a cell's settings stack: :defaults, {:preset, name} or :cell.

t()

Functions

Which layer set each setting of a stack (spec/07 §1): one entry per setting, in name order, %{path: [param], value, layer, overrode}, layer the layer that set it and overrode the layers it set it over, lowest first. It is macaw_layers' explain (Macaw.Layers.Fragment.explain/2) over the stack's own cascade, so it cannot drift from it.

The layers of a configured component's settings, lowest first (spec/07 §1): its defaults, the preset's settings when it has one, and the cell's own, each {layer, settings}. defaults is the component's parameters' defaults by name, as Macaw.Component.parameters/1 gives them.

A preset from a keyword list or a map with atom keys: {:ok, preset} or every fault at once, by path from the preset. A missing name or component is :required there, and one that isn't a name {:type, :name}; settings that aren't a map {[:settings], {:type, :map}}, a setting's name that isn't one {:type, :name} at [:settings, name]; a doc that isn't a string {:type, :string}; another key :unknown_key. Whether the settings are the component's to take is judged where the preset is stacked into a cell, with the cell's own settings over it.

A cell's own settings stacked over the preset (spec/07 §1): one cascade, the cell's value replacing the preset's whole, setting by setting. nil for no preset is the cell's settings alone, as inline settings are.

Types

layer()

@type layer() :: :defaults | {:preset, String.t()} | :cell

A layer of a cell's settings stack: :defaults, {:preset, name} or :cell.

t()

@type t() :: %Macaw.Preset{
  component: String.t(),
  doc: String.t() | nil,
  name: String.t(),
  settings: %{required(String.t()) => term()}
}

Functions

explain(layers)

@spec explain([{layer(), map()}]) :: [map()]

Which layer set each setting of a stack (spec/07 §1): one entry per setting, in name order, %{path: [param], value, layer, overrode}, layer the layer that set it and overrode the layers it set it over, lowest first. It is macaw_layers' explain (Macaw.Layers.Fragment.explain/2) over the stack's own cascade, so it cannot drift from it.

iex> {:ok, preset} = Macaw.Preset.new(name: "wide", component: "macaw.table", settings: %{"order" => "desc"})
iex> Macaw.Preset.explain(Macaw.Preset.layers(%{"order" => "asc"}, preset, %{}))
[%{path: ["order"], value: "desc", layer: {:preset, "wide"}, overrode: [:defaults]}]

layers(defaults, preset, settings)

@spec layers(map(), t() | nil, map()) :: [{layer(), map()}, ...]

The layers of a configured component's settings, lowest first (spec/07 §1): its defaults, the preset's settings when it has one, and the cell's own, each {layer, settings}. defaults is the component's parameters' defaults by name, as Macaw.Component.parameters/1 gives them.

new(attrs)

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

A preset from a keyword list or a map with atom keys: {:ok, preset} or every fault at once, by path from the preset. A missing name or component is :required there, and one that isn't a name {:type, :name}; settings that aren't a map {[:settings], {:type, :map}}, a setting's name that isn't one {:type, :name} at [:settings, name]; a doc that isn't a string {:type, :string}; another key :unknown_key. Whether the settings are the component's to take is judged where the preset is stacked into a cell, with the cell's own settings over it.

stack(arg1, settings)

@spec stack(t() | nil, map()) :: map()

A cell's own settings stacked over the preset (spec/07 §1): one cascade, the cell's value replacing the preset's whole, setting by setting. nil for no preset is the cell's settings alone, as inline settings are.

iex> {:ok, preset} = Macaw.Preset.new(name: "wide", component: "macaw.table", settings: %{"order" => "desc", "sort" => "t"})
iex> Macaw.Preset.stack(preset, %{"sort" => "v"})
%{"order" => "desc", "sort" => "v"}
iex> Macaw.Preset.stack(nil, %{"sort" => "v"})
%{"sort" => "v"}