elixir - how to add conditional pipe into pipeline?

asiniy picture asiniy · May 7, 2016 · Viewed 7.9k times · Source

I have a small pipeline in elixir, it's about changing ecto model state:

model
|> cast(params, ~w(something), ~w())
|> conditional
|> Repo.update

The problem is that I have conditional pipe which can be nil sometimes, so in the case it's nil it should do nothing and can works (I presume it will be fn(x) -> x end)

So, my question is: "how can I do that"?

Answer

NoDisplayName picture NoDisplayName · May 7, 2016
model
|> cast(params, ~w(something), ~w())
|> maybe_do_something(conditional)
|> Repo.update

defp maybe_do_something(changeset, nil), do: changeset

defp maybe_do_something(changeset, func) do
  # Do something with changeset
end

Not sure if I got your question right, but maybe thats what you are looking for.