Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions docs/then.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ You can also call functions from other modules:
defmodule Calculator do
use Then

@then {Logger, :info}
@then {IO, :puts}
def multiply(a, b) do
a * b
end
Expand All @@ -62,6 +62,20 @@ defmodule MyAudit do
end
```

**Aliased modules work too:**

```elixir
defmodule Calculator do
alias IO, as: MyIO
use Then

@then {MyIO, :puts}
def calculate(x) do
x * 2
end
end
```

### Real-world Example

```elixir
Expand All @@ -77,8 +91,8 @@ defmodule UserService do
end

# side effects separately
def audit_creation({:ok, user}), do: Logger.info("User #{user.email} created")
def audit_creation({:error, reason}), do: Logger.warn("User wasn't created. #{reason}")
def audit_creation({:ok, user}), do: IO.puts("✅ User #{user.email} created")
def audit_creation({:error, reason}), do: IO.puts("❌ User creation failed: #{reason}")
end
```

Expand Down
8 changes: 4 additions & 4 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ defmodule Then.MixProject do

defp docs do
[
main: "then",
main: "getting-started",
source_url: "https://github.com/bardoor/then",
extras: [
"docs/then.md",
"CHANGELOG.md",
"LICENSE"
{"docs/then.md", title: "Getting Started", filename: "getting-started"},
{"CHANGELOG.md", title: "Changelog"},
{"LICENSE", title: "License"}
],
groups_for_extras: [
"Documentation": ["docs/then.md"],
Expand Down
23 changes: 23 additions & 0 deletions test/then_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
defmodule ThenTest do
use ExUnit.Case
import ExUnit.CaptureIO

doctest Then

defmodule TestModule do
Expand Down Expand Up @@ -579,4 +581,25 @@ defmodule ThenTest do
assert_received {:arity_1, "one arg: z"}
end
end

describe "aliased external modules" do
test "works with aliased modules" do
defmodule AliasedTest do
alias IO, as: MyIO
use Then

@then {MyIO, :puts}
def test_aliased_call(value) do
value * 2
end
end

output = capture_io(fn ->
result = AliasedTest.test_aliased_call(5)
assert result == 10
end)

assert output == "10\n"
end
end
end