Vela v0.7.1 Vela behaviour 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 (default: & &1)
  • comparator — the function that accepts a series name and two values and returns the greater one to be used in Vela.δ/1 (default: &</2)
  • threshold — if specified, the inserted value is checked to fit in δ ± threshold; whether it does not, it goes to errors (float() | nil, default: nil)
  • validator — the function to be used to invalidate the accumulated values (default: fn _ -> true end)
  • sorter — the function to be used to sort values within one serie, if none is given, it sorts in the natural order, FIFO, newest is the one to pop
  • 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

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

  @spec validator(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?/2 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-value pair in errors and unmatched

Represents a key in the Vela structure

t()

Represents the struct created by this behaviour module

Represents a value in the Vela structure

Functions

Returns {min, max} tuple for each serie, using the comparator given as a second parameter, or a default comparator for each serie.

Flat maps the series using fun and returns the keyword with duplicated keys and mapped values.

Maps the series using fun and returns the new Vela instance with series mapped

Callbacks

Returns {min, max} tuple for each serie, using the comparator given as a second parameter, or a default comparator for this serie.

Checks two velas given as an input for equality

Removes obsoleted elements from the series using the validator given as a second parameter, or a default validator for this serie.

Returns a keyword with series as keys and the hottest value as a value

Link to this section Types

Specs

kv() :: {serie(), value()}

Represents a key-value pair in errors and unmatched

Specs

serie() :: atom()

Represents a key in the Vela structure

Specs

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

Represents the struct created by this behaviour module

Specs

value() :: any()

Represents a value in the Vela structure

Link to this section Functions

Link to this function

δ(vela, comparator \\ nil)

View Source

Specs

δ(t(), nil | (serie(), value(), value() -> boolean())) :: [
  {atom(), {value(), value()}}
]

Returns {min, max} tuple for each serie, using the comparator given as a second parameter, or a default comparator for each serie.

Link to this function

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

View Source

Specs

flat_map(
  vela :: t(),
  ({serie(), value()} -> {serie(), value()})
  | (serie(), value() -> {serie(), value()})
) :: [{serie(), value()}]

Flat maps the series using fun and returns the keyword with duplicated keys and mapped values.

Example:

defmodule EO do
  use Vela,
    even: [limit: 2],
    odd: [limit: 2]

  def flat_map(%EO{} = v),
    do: Vela.flat_map(v, & {&1, &2+1})
end
EO.flat_map(struct(EO, [even: [2, 4], odd: [1, 3]]))

#⇒ [even: 3, even: 5, odd: 2, odd: 4]

Specs

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

Maps the series using fun and returns the new Vela instance with series mapped

Link to this section Callbacks

Specs

delta(t(), nil | (serie(), value(), value() -> boolean())) :: [
  {atom(), {value(), value()}}
]

Returns {min, max} tuple for each serie, using the comparator given as a second parameter, or a default comparator for this serie.

Specs

equal?(t(), t()) :: boolean()

Checks two velas given as an input for equality

Specs

purge(t(), nil | (serie(), value() -> boolean())) :: t()

Removes obsoleted elements from the series using the validator given as a second parameter, or a default validator for this serie.

Specs

slice(t()) :: [kv()]

Returns a keyword with series as keys and the hottest value as a value

Example:

defmodule AB do
  use Vela, a: [], b: []
end
AB.slice(struct(AB, [a: [1, 2], b: [3, 4]]))

#⇒ [a: 1, b: 3]