suitepad-gmbh / pipette

Flow-Based Programming framework for Elixir
MIT License
21 stars 3 forks source link

Feat/custom start stage #20

Closed spieker closed 6 years ago

spieker commented 6 years ago

The method to start a stage can be configured in the main application. We use this in our application, to read some stage configurations from the applications configuration.

config :pipette, start_stage: {FooModule, :start_stage, bar: "baz"}

defmodule FooModule do
  def start_stage(%{__struct__: module} = stage, args) when is_atom(module) do
    stage_conf(stage)
    |> Pipette.Controller.start_stage(args)
  end

  defp stage_conf({:cfg, key}), do: Application.get_env(:my_app, key)

  defp stage_conf(%{__struct__: module} = struct) when is_atom(module) do
    Map.from_struct(struct)
    |> stage_conf()
    |> (&struct(module, &1)).()
  end

  defp stage_conf(%{} = map) do
    Enum.map(map, fn {key, value} -> {key, stage_conf(value)} end)
    |> Map.new()
  end

  defp stage_conf(tuple) when is_tuple(tuple) do
    Tuple.to_list(tuple)
    |> stage_conf()
    |> List.to_tuple()
  end

  defp stage_conf(list) when is_list(list), do: Enum.map(list, &stage_conf(&1))
  defp stage_conf(value), do: value
end