Vela v0.3.2 Vela View Source

Vela is a tiny library providing easy management of validated cached state with some history.

Including use Vela in your module would turn the module into struct, setting field accordingly to the specification, passed as a parameter.

Vela allows the following configurable parameters per field:

  • limit — length of the series to keep (default 5)
  • compare_by — comparator extraction function to extract the value, to be used for comparison, from the underlying terms (by default it returns the whole value)
  • validator — the function to be used to invalidate the accumulated values
  • errors — number of errors to keep (default: 5)

Also, Vela accepts :mη keyword parameter for the cases when the consumer needs the very custom meta to be passed to the struct.

Vela implements Access behaviour.

Usage

defmodule Vela.Test do
  use Vela,
    series1: [limit: 3, errors: 1], # no validation
    series2: [limit: 2, validator: Vela.Test]
    series3: [
          compare_by: &Vela.Test.comparator/1,
          validator: &Vela.Test.validator/2
    ]

  @behaviour Vela.Validator

  @impl Vela.Validator
  def valid?(_serie, value) do
    value > 0
  end

  @spec comparator(%{created_at :: DateTime.t()}) :: DateTime.t()
  def comparator(%{created_at: created_at}),
    do: created_at

  @spec validator(serie :: atom(), value :: t()) :: boolean()
  def validator(_, value),
    do: is_integer(value) and value > 300
end

In the example above, before any structure update attempt (via Access,) this valid?/3 function would be called.

If it returns true, the value gets inserted / updated, and the series behind is truncated if needed. It it returns false, the state is not updated, and the value is put into the map under __errors__ key of the struct. The length of errors is also configurable via errors: keyword parameter.

Link to this section Summary

Types

Represents a key in the Vela structure

t()

Represents the struct created by this behaviour module

Represents a value in the Vela structure

Link to this section Types

Represents a key in the Vela structure

Link to this type

t()

View Source
t() :: %atom(){
  :__errors__ => keyword(),
  :__meta__ => keyword(),
  optional(serie()) => [value()]
}

Represents the struct created by this behaviour module

Represents a value in the Vela structure

Link to this section Functions

Link to this function

flat_map(vela, fun \\ &(&1))

View Source
flat_map(vela :: t(), ({serie(), value()} -> {serie(), value()})) :: list()
Link to this function

map(vela, fun)

View Source
map(vela :: t(), ({serie(), value()} -> {serie(), value()})) :: t()