Skip to content

Commit

Permalink
Add protect/2 for behaviours and implementations in the same module
Browse files Browse the repository at this point in the history
  • Loading branch information
saneery committed Oct 3, 2020
1 parent 8c692de commit 41a248c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/hammox.ex
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,25 @@ defmodule Hammox do
add_2.(1.5, 2.5) # throws Hammox.TypeMatchError
```
If pass module and list instead of MFA and module works like `protect/3`
```elixir
# calling this
Hammox.protect(SomeModule, foo: 1)
# works same as this
Hammox.protect(SomeModule, SomeModule, foo: 1)
```
"""
@spec protect(mfa :: mfa(), behaviour_name :: module()) :: fun()
@spec protect(
mfa() | module(),
module() | [{atom(), arity() | [arity()]}]
) ::
fun() | map()
def protect(mfa, behaviour_name)

def protect(module_name, funs) when is_atom(module_name and is_list(funs)),
do: protect(module_name, module_name, funs)

def protect({module_name, function_name, arity}, behaviour_name)
when is_atom(module_name) and is_atom(function_name) and is_integer(arity) and
is_atom(behaviour_name) do
Expand Down
8 changes: 8 additions & 0 deletions test/hammox_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ defmodule HammoxTest do
)
end)
end

test "decorate multiple functions" do
assert %{foo_0: _, other_foo_1: _} =
Hammox.protect(Hammox.Test.SmallBehaviour,
foo: 0,
other_foo: 1
)
end
end

describe "protect/3" do
Expand Down

0 comments on commit 41a248c

Please sign in to comment.