Sablon.XML.Store (sablon v0.4.4)

Copy Markdown View Source

The mutable cell behind a document tree.

Rendering a template is a long sequence of in-place edits - nodes are removed, duplicated and re-parented while operations are evaluated - and references to a node have to stay valid across all of them. Threading a persistent tree through every one of those edits would obscure the algorithm it implements, so the tree lives in a mutable store instead and nodes are {store, id} pairs pointing into it.

The store is backed by the process dictionary: a render happens in a single process, so this needs neither an extra process nor an ETS table, and the data disappears with the process even if drop/1 is never called. Because of that a document cannot be shared across processes - build it, edit it and serialise it in the same one.

Summary

Functions

Removes a key.

Discards everything the store holds.

Reads a key as an {:ok, value} / :error pair.

Reads a key, raising when it is not set.

Reads a key, returning default when it is not set.

Increments an integer counter and returns its new value.

Creates an empty store.

Writes a key.

Updates a key, starting from default when it is not set.

Types

t()

@opaque t()

Functions

delete(store, key)

@spec delete(t(), term()) :: :ok

Removes a key.

drop(store)

@spec drop(t()) :: :ok

Discards everything the store holds.

Optional - the data is process local and dies with the process - but worth calling in a long lived process that renders many documents.

fetch(store, key)

@spec fetch(t(), term()) :: {:ok, term()} | :error

Reads a key as an {:ok, value} / :error pair.

fetch!(store, key)

@spec fetch!(t(), term()) :: term()

Reads a key, raising when it is not set.

get(store, key, default \\ nil)

@spec get(t(), term(), term()) :: term()

Reads a key, returning default when it is not set.

increment(store, key)

@spec increment(t(), term()) :: integer()

Increments an integer counter and returns its new value.

Used to hand out node ids, which must stay unique for the life of a tree.

new()

@spec new() :: t()

Creates an empty store.

put(store, key, value)

@spec put(t(), term(), term()) :: :ok

Writes a key.

update(store, key, default, fun)

@spec update(t(), term(), term(), (term() -> term())) :: term()

Updates a key, starting from default when it is not set.