Piper
Plug-like data processing library.
Installation
If available in Hex, the package can be installed as:
Add
piper
to your list of dependencies inmix.exs
:def deps do [{:piper, "~> 0.1.0"}] end
Ensure
piper
is started before your application:def application do [applications: [:piper]] end
Usage
Piper pipeline can be built using either functions or modules. This way you can provide deencapsulation and processing data in pipeline-like way. Example:
defmodule FetchFromRepoPipe do
alias MyApp.Repo
def init(opts), do: opts
def call(%Piper.Data{data: %{"id" => id}}, _opts) do
data
|> assign(:user, Repo.get(User, id))
end
end
defmodule UpdatePassword do
use Piper.Builder
alias MyApp.Repo
pipe FetchFromRepoPipe
pipe :encrypt, module: Comeoin.Bcrypt
pipe :set
pipe :save
def encrypt(%{data: %{"password" => password}} = data, [module: module]) do
data
|> assign(:password, module.hashpw(password))
end
def set(%{assigns: %{user: user, password: password}} = data, _opts) do
data
|> assign(:user, %{user | password: password})
end
def save(%{assigns: %{user: user}} = data, _opts) do
data
|> assign(:user, Repo.update(user))
end
end
Special thanks
- For initial idea Rafał Radziszewski (@rafal-radziszewski)
- Jose Valim (@josevalim)
- Andrea Leopardi (@whatyouhide)
- Sonny Scroggin (@scrogson)
- Gabriel Jaldon (@gjaldon)
- and other contributors to elixir-lang/plug for your work on Plug which allowed us to create this library.