elixirecto

elixir - how to add conditional pipe into pipeline?


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"?


Solution

  • 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.