Hello π Welcome to my corner of the internet. I write here about the different challenges I encounter, and the projects I work on. Find out more about me.
The init and call construction of plugs give you the ability to provide default arguments. Read more about it here. This is a short explenation of how you can work with that.
defmodule Project.CakeDescriber
def init(opts) do
default_opts = %{color: βredβ}
Map.merge(default_opts, opts)
end
def call(conn, opts \\ %{}) do
color = Map.fetch!(opts, :color)
taste = Map.fetch!(opts, :taste)
IO.puts(β#{color} and #{taste} cakes are the best!β)
end
end
Now if you call:
Project.CakeDescriber, %{taste: βfruityβ}
It will return: βred and fruity cakes are the best!β.
Then, if you call:
Project.CakeDescriber, %{taste: βcarrot-yβ, color: βpurpleβ}
It will return: βpurple and carrot-y cakes are the best!β.