View Source ExAequo.KeywordParams (ExAequo v0.6.8)

Tools to facilitate dispatching on keyword parameters, used in contexts like the following

  @defaults [a: 1, b: false] # Keyword or Map
  def some_fun(..., options \ []) # options again can be a Keyword or Map
    {a, b} = tuple_from_params(@defaults, options, [:a, :b])

Merging defaults and actual parameters

Its most useful feature is that you will get a map whatever the mixtures of maps and keywords the input was

    iex(0)> merge_params([])
    %{}

    iex(1)> merge_params([a: 1], %{b: 2})
    %{a: 1, b: 2}

    iex(2)> merge_params(%{a: 1}, [a: 2, b: 2])
    %{a: 2, b: 2}

Strict merging

Not implemented yet

Extracting params from the merged defaults and actuals

iex(3)> defaults = [foo: false, depth: 3]
...(3)> tuple_from_params(defaults, %{foo: true}, [:foo, :depth])
{true, 3}

As defaults are required a missing parameter will raise an Error

iex(4)> try do
...(4)>   tuple_from_params([], [foo: 1], [:bar])
...(4)> rescue
...(4)>   KeyError -> :caught
...(4)> end
:caught

Alternatively on can extract a map

iex(5)> map_from_params([], [hello: "world"], [:hello])
%{hello: "world"}

Summary

Functions

This is the 2 param form which is identical to an empty default map

This is the 2 param form which is identical to an empty default map

Types

@type date_tuple() ::
  {non_neg_integer(), 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12, 1..255}
@type lstat_result() :: {:ok, File.Stat.t()} | {:error, File.posix()}
@type param_type() :: Keyword.t() | map()
@type params_t() :: map() | Keyword.t()

Functions

Link to this function

map_from_params(actual, keys)

View Source
@spec map_from_params(params_t(), list()) :: map()

This is the 2 param form which is identical to an empty default map

iex(6)> map_from_params(%{a: 1, b: 2}, [:a])
%{a: 1}
Link to this function

map_from_params(default, actual, keys)

View Source
@spec map_from_params(params_t(), params_t(), list()) :: map()
@spec merge_params(params_t()) :: map()
Link to this function

merge_params(default, actual)

View Source
@spec merge_params(params_t(), params_t()) :: map()
Link to this function

tuple_from_params(actual, keys)

View Source
@spec tuple_from_params(params_t(), list()) :: tuple()

This is the 2 param form which is identical to an empty default map

iex(7)> tuple_from_params(%{a: 1, b: 2}, [:b, :a])
{2, 1}
Link to this function

tuple_from_params(default, actual, keys)

View Source
@spec tuple_from_params(params_t(), params_t(), list()) :: tuple()