Skip to content

Commit

Permalink
Merge pull request #3 from akoutmos/remove-anonymous-fns
Browse files Browse the repository at this point in the history
Remove anonymous fns
  • Loading branch information
akoutmos authored May 12, 2020
2 parents 488b065 + 44ff3bc commit ac527a3
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 36 deletions.
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,31 +68,25 @@ The `:if` entry can be:
the predicate (a predicate is a module that implements the `Unplug.Predicate` behavior).
- A module that implements the `Unplug.Predicate` behavior. Without any predicate options being specified, an
empty list is sent as the second argument to the predicate's `call/2` function.
- An anonymous function of arity one. That one argument being the `conn` for the current request.

The `:do` entry can be:

- A tuple where the first element is the plug module that you want to execute upon a truthy value from the
predicate, and the second element being options that you want to send to that plug.
- A plug module that you want to execute upon a truthy value from the predicate. Without any options specified,
Unplug will assume that an empty list should be sent to the plug's `init/1` function.
- An anonymous function that takes one argument, with that argument being the current request's `conn`. This
function must also return a `Plug.Conn` struct.

There is also an `:else` entry that can be provided to `Unplug`. The `:else` entry is identical to the `:do` entry
in terms of what it accepts as valid input. The `:else` entry is what is execute if the `:if` entry yields a falsey
value for the current request.

Another example to show off the anonymous function and `:else` functionality could be something like this:
An example to show off the `:else` functionality could be something like this:

```elixir
plug Unplug,
if: fn conn -> conn.remote_ip == {10, 0, 0, 1} end,
if: MyCoolApp.WhiteListedIPAddress,
do: MyCoolApp.MetricsExporter,
else: fn conn ->
Logger.warn("Someone is trying to steal my metrics!")
conn
end,
else: MyCoolApp.LogWarning
```

If the above example, we only want to expose our Prometheus metrics if the request is coming from a known safe source
Expand Down
3 changes: 0 additions & 3 deletions lib/unplug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,14 @@ defmodule Unplug do

defp eval_plug_init(:compile, :skip), do: :skip
defp eval_plug_init(:compile, {plug, opts}), do: plug.init(opts)
defp eval_plug_init(:compile, plug) when is_function(plug, 1), do: nil
defp eval_plug_init(:compile, plug), do: plug.init([])
defp eval_plug_init(:runtime, :skip), do: :skip
defp eval_plug_init(:runtime, _plug), do: nil
defp eval_plug_init(bad_arg, _plug), do: raise("Invalid value #{inspect(bad_arg)} for Unplug config :init_mode")

defp exec_if_condition_call(conn, predicate) when is_function(predicate, 1), do: predicate.(conn)
defp exec_if_condition_call(conn, {predicate_module, predicate_opts}), do: predicate_module.call(conn, predicate_opts)
defp exec_if_condition_call(conn, predicate_module), do: predicate_module.call(conn, [])

defp exec_plug_call(conn, function, _plug_opts) when is_function(function, 1), do: function.(conn)
defp exec_plug_call(conn, {plug_module, _init_opts}, plug_opts), do: plug_module.call(conn, plug_opts)
defp exec_plug_call(conn, plug_module, plug_opts), do: plug_module.call(conn, plug_opts)
end
24 changes: 0 additions & 24 deletions test/unplug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -179,29 +179,5 @@ defmodule UnplugTest do

assert conn.assigns == %{other: "setting"}
end

test "should accept anonymous functions for if/do/else" do
opts =
Unplug.init(
if: fn c -> c.assigns[:thing] == "nice" end,
do: fn c -> assign(c, :cool, "it's good") end,
else: fn c -> assign(c, :no, "it's bad") end
)

conn =
:get
|> conn("/")
|> Unplug.call(opts)

assert conn.assigns == %{no: "it's bad"}

conn =
:get
|> conn("/")
|> assign(:thing, "nice")
|> Unplug.call(opts)

assert conn.assigns == %{cool: "it's good", thing: "nice"}
end
end
end

0 comments on commit ac527a3

Please sign in to comment.