Macaw.Registry (macaw v0.6.0)

Copy Markdown View Source

A host's registry of components and layouts (spec/12 §1.1): built from the packages the host enables and the layouts it adds, and nothing else.

registry = Macaw.Registry.new!([Macaw.Builtin])
{:ok, Macaw.Builtin.Table} = Macaw.Registry.component(registry, "macaw.table")

Closed by default: new([]) holds nothing, and a component whose package isn't listed is unknown though its module is loaded. Names are matched by spelling; no atom is made from a document's name. Two components of one name across packages fail at boot, naming both.

Layouts (Macaw.Layout) are closed the same way: a registry holds the built-in layouts (Macaw.Layout.builtins/0) and the ones layouts: lists, and nothing else.

Templates and layers (spec/06 §2.4) are its packages' members too: a member answering __template__/0 is a template and one answering __layer__/0 a layer, each built as the registry is, so a template that doesn't build fails the boot. So are presets (spec/07 §1): a member answering __preset__/0 is a preset, a fragment of one component's settings.

Themes (spec/12 §1.1, spec/11 §1) are closed the same way: a registry holds the built-in themes ("default", "dark", "color_safe" and "print") and the ones themes: lists, each a %Macaw.Layers.Theme{}, by name, and nothing else. A surface's theme preference renders in the theme of its name here.

Summary

Functions

A component by name, a string or an atom read as its spelling: {:ok, module} or :error.

Every component, {name, module}, in name order.

The registry a configuration names (spec/12 §1.1): packages:, Macaw.Builtin first and always, layouts: and themes:: {:ok, registry} or every fault, as new/2 gives them. It reads only the keyword list it is given. A host keeps its configuration where it keeps its own, and builds its registry from it once, at boot, with from_config!/1

from_config/1, or an ArgumentError with every fault: a host's boot.

Every hook of the registry's packages (Macaw.Component.Package.hooks/1).

A layer by name, a string or an atom read as its spelling: {:ok, layer} or :error.

Every layer, {name, layer}, in name order.

A layout by name, a string or an atom read as its spelling: {:ok, module} or :error.

Every layout, {name, module}, in name order.

The registry of the listed packages: {:ok, registry}, or every fault at once, a package's own at [package] and a name two packages share as {:duplicate, name, [{module, package}, {module, package}]}.

The registry of the listed packages, or an ArgumentError with every fault: a host's boot.

The packages, as given, each once.

A preset by name, a string or an atom read as its spelling: {:ok, preset} or :error (spec/07 §1). A package's presets are its members answering __preset__/0.

Every preset, {name, preset}, in name order.

A template by name, a string or an atom read as its spelling: {:ok, template} or :error.

Every template, {name, template}, in name order.

A theme by name, a string or an atom read as its spelling: {:ok, theme} or :error (spec/12 §1.1). A registry holds the built-in themes and the host's themes:.

Every theme, {name, theme}, in name order.

Types

t()

@type t() :: %Macaw.Registry{
  components: %{required(String.t()) => module()},
  layers: %{required(String.t()) => Macaw.Template.Layer.t()},
  layouts: %{required(String.t()) => module()},
  packages: [module()],
  presets: %{required(String.t()) => Macaw.Preset.t()},
  templates: %{required(String.t()) => Macaw.Template.t()},
  themes: %{required(String.t()) => Macaw.Layers.Theme.t()}
}

Functions

component(registry, name)

@spec component(t(), String.t() | atom()) :: {:ok, module()} | :error

A component by name, a string or an atom read as its spelling: {:ok, module} or :error.

components(registry)

@spec components(t()) :: [{String.t(), module()}]

Every component, {name, module}, in name order.

from_config(config)

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

The registry a configuration names (spec/12 §1.1): packages:, Macaw.Builtin first and always, layouts: and themes:: {:ok, registry} or every fault, as new/2 gives them. It reads only the keyword list it is given. A host keeps its configuration where it keeps its own, and builds its registry from it once, at boot, with from_config!/1:

registry = Macaw.Registry.from_config!(Application.fetch_env!(:my_app, :macaw))

from_config([]) is the built-in registry, what Macaw.Surface.show/2 validates against when it is given no registry:.

from_config!(config)

@spec from_config!(keyword()) :: t()

from_config/1, or an ArgumentError with every fault: a host's boot.

hooks(registry)

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

Every hook of the registry's packages (Macaw.Component.Package.hooks/1).

layer(registry, name)

@spec layer(t(), String.t() | atom()) :: {:ok, Macaw.Template.Layer.t()} | :error

A layer by name, a string or an atom read as its spelling: {:ok, layer} or :error.

layers(registry)

@spec layers(t()) :: [{String.t(), Macaw.Template.Layer.t()}]

Every layer, {name, layer}, in name order.

layout(registry, name)

@spec layout(t(), String.t() | atom()) :: {:ok, module()} | :error

A layout by name, a string or an atom read as its spelling: {:ok, module} or :error.

layouts(registry)

@spec layouts(t()) :: [{String.t(), module()}]

Every layout, {name, module}, in name order.

new(packages, opts \\ [])

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

The registry of the listed packages: {:ok, registry}, or every fault at once, a package's own at [package] and a name two packages share as {:duplicate, name, [{module, package}, {module, package}]}.

layouts: the layouts the host adds to the built-in ones, each a module implementing Macaw.Layout: one that doesn't is :not_a_layout at [:layouts, module], and a layout name two of them share, or one shares with a built-in, is {:duplicate_layout, name, [module, module]} at [:layouts].

A package's templates and layers are built from its members: one that doesn't build has its faults at [package, module | path]; a name two templates or layers share is {:duplicate_template, name, [{module, package}, …]}; a layer on a template no listed package provides is {:unknown_template, name} at [package, module, :on], and a template's print: naming no layer on it {:unknown_layer, name} at [package, module, :print]. A preset is built from its member as a template is; a name two presets share is {:duplicate_preset, name, [{module, package}, …]}, and a preset of a component no listed package provides {:unknown_component, name} at [package, module, :component].

themes: the themes the host adds to the built-in ones, each a %Macaw.Layers.Theme{} whose name is lower-case words, so its class and its stylesheet can't be broken out of: one that isn't a theme is {:type, :theme} at [:themes, i], one whose name isn't words {:type, :word} at [:themes, i, :name], and a name two of them share, or one shares with a built-in, {:duplicate_theme, name} at [:themes]. Themes that aren't a list are {[:themes], {:type, :list}}.

new!(packages, opts \\ [])

@spec new!([module()], keyword()) :: t()

The registry of the listed packages, or an ArgumentError with every fault: a host's boot.

packages(registry)

@spec packages(t()) :: [module()]

The packages, as given, each once.

preset(registry, name)

@spec preset(t(), String.t() | atom()) :: {:ok, Macaw.Preset.t()} | :error

A preset by name, a string or an atom read as its spelling: {:ok, preset} or :error (spec/07 §1). A package's presets are its members answering __preset__/0.

presets(registry)

@spec presets(t()) :: [{String.t(), Macaw.Preset.t()}]

Every preset, {name, preset}, in name order.

template(registry, name)

@spec template(t(), String.t() | atom()) :: {:ok, Macaw.Template.t()} | :error

A template by name, a string or an atom read as its spelling: {:ok, template} or :error.

templates(registry)

@spec templates(t()) :: [{String.t(), Macaw.Template.t()}]

Every template, {name, template}, in name order.

theme(registry, name)

@spec theme(t(), String.t() | atom()) :: {:ok, Macaw.Layers.Theme.t()} | :error

A theme by name, a string or an atom read as its spelling: {:ok, theme} or :error (spec/12 §1.1). A registry holds the built-in themes and the host's themes:.

iex> {:ok, theme} = Macaw.Registry.theme(Macaw.Registry.from_config!([]), :dark)
iex> theme.name
"dark"

themes(registry)

@spec themes(t()) :: [{String.t(), Macaw.Layers.Theme.t()}]

Every theme, {name, theme}, in name order.