View Source README
Mecto
"Mail merging" with Ecto structs.
A parser to interpolate MediaWiki-like [[foo.bar]]
markup using data from Ecto schemas.
Installation
If available in Hex, the package can be installed
by adding mecto
to your list of dependencies in mix.exs
:
def deps do
[
{:mecto, "~> 0.7.0"}
]
end
Usage
Mecto was originally built to provide dynamic "mail-merging" (think HEEx, but not compiled) from Elixir structs - specifically ones that use Ecto.Schema
.
Take this struct:
defmodule MyApp.BlogPost do
use Ecto.Schema
schema "blog_posts" do
field(:title, :string)
field(:content, :string)
end
end
You could then have some text like:
text = "The latest blog post is [[blog_post.title]]"
If you wanted to validate the text has correct markup, you could call:
Mecto.validate(text, MyApp.BlogPost)
> %{blog_post: %{title: :string}}
And Mecto would ensure that the fields used in the text actually exist on MyApp.BlogPost
. You can also take it a step further,
calling Mecto.interpolate
to then use the values in a specific struct:
Mecto.interpolate(text, %MyApp.BlogPost{title: "some title"})
> {:ok, "The latest blog post is some title"}
Mecto also handles relationships and custom Ecto types (i.e. defined with Ecto.Type
).
By default there is an implementation for Ecto.Enum
,
but you can use protocol_ex for your own types - this is required for them as
Elixir doesn't allow protocol dispatch on atoms (module names), only data...which Mecto doesn't have when parsing markup.
See the tests for more examples.
Documentation can be found at https://hexdocs.pm/mecto.
Support my work
If you want to support my work, you can donate on Liberapay: