View Source ExAequo.KeywordParams (ExAequo v0.5.2)
tools-to-facilitate-dispatching-on-keyword-parameters-used-in-contexts-like-the-following
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
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
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"}
Link to this section 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
Link to this section 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()
Link to this section Functions
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}
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}