plymio_codi v0.3.1 Plymio.Codi.Pattern.Other View Source
This module collects the other, simple patterns.
See Plymio.Codi for an overview and documentation terms.
Pattern: form
The form pattern is a convenience to embed arbitrary code.
Valid keys in the cpo are:
| Key | Aliases |
|---|---|
:form | :forms, :ast, :asts |
Examples
A simple example with four functions:
iex> {:ok, {forms, _}} = [
...> form: quote(do: def(add_1(x), do: x + 1)),
...> ast: quote(do: def(sqr_x(x), do: x * x)),
...> forms: [
...> quote(do: def(sub_1(x), do: x - 1)),
...> quote(do: def(sub_2(x), do: x - 2)),
...> ]
...> ] |> produce_codi
...> forms |> harnais_helper_show_forms!
["def(add_1(x)) do\n x + 1\n end",
"def(sqr_x(x)) do\n x * x\n end",
"def(sub_1(x)) do\n x - 1\n end",
"def(sub_2(x)) do\n x - 2\n end"]
Here the subtraction functions are renamed:
iex> {:ok, {forms, _}} = [
...> form: quote(do: def(add_1(x), do: x + 1)),
...> ast: quote(do: def(sqr_x(x), do: x * x)),
...> forms: [
...> forms: [quote(do: def(sub_1(x), do: x - 1)),
...> quote(do: def(sub_2(x), do: x - 2))],
...> forms_edit: [rename_funs: [sub_1: :decr_1, sub_2: :take_away_2]]]
...> ] |> produce_codi
...> forms |> harnais_helper_show_forms!
["def(add_1(x)) do\n x + 1\n end",
"def(sqr_x(x)) do\n x * x\n end",
"def(decr_1(x)) do\n x - 1\n end",
"def(take_away_2(x)) do\n x - 2\n end"]
In this example the edits are “global” and applied to all produced forms:
iex> forms_edit = [rename_funs: [
...> sub_1: :decr_1,
...> sub_2: :take_away_2,
...> add_1: :incr_1,
...> sqr_x: :power_2]
...> ]
...> {:ok, {forms, _}} = [
...> form: quote(do: def(add_1(x), do: x + 1)),
...> ast: quote(do: def(sqr_x(x), do: x * x)),
...> forms: [quote(do: def(sub_1(x), do: x - 1)),
...> quote(do: def(sub_2(x), do: x - 2))],
...> ] |> produce_codi(forms_edit: forms_edit)
...> forms |> harnais_helper_show_forms!
["def(incr_1(x)) do\n x + 1\n end",
"def(power_2(x)) do\n x * x\n end",
"def(decr_1(x)) do\n x - 1\n end",
"def(take_away_2(x)) do\n x - 2\n end"]
Pattern: since
The since pattern builds a @since module attribute form.
Valid keys in the cpo are:
| Key | Aliases |
|---|---|
:since |
Examples
The value must be a string and is validated by Version.parse/1:
iex> {:ok, {forms, _}} = [
...> since: "1.7.9"
...> ] |> produce_codi
...> forms |> harnais_helper_show_forms!
["@since(\"1.7.9\")"]
iex> {:error, error} = [
...> since: "1.2.3.4.5"
...> ] |> produce_codi
...> error |> Exception.message
"since invalid, got: 1.2.3.4.5"
Pattern: deprecated
The deprecated pattern builds a @deprecated module attribute form.
Valid keys in the cpo are:
| Key | Aliases |
|---|---|
:deprecated |
Examples
The value must be a string.
iex> {:ok, {forms, _}} = [
...> deprecated: "This function has been deprecated since 1.7.9"
...> ] |> produce_codi
...> forms |> harnais_helper_show_forms!
["@deprecated(\"This function has been deprecated since 1.7.9\")"]