Skip to content

Commit 0ec15e2

Browse files
committed
add module documentation and bump version for publishing
1 parent 9064d61 commit 0ec15e2

File tree

4 files changed

+69
-54
lines changed

4 files changed

+69
-54
lines changed

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/_build
22
/cover
33
/deps
4+
/docs
5+
/doc
46
erl_crash.dump
57
*.ez

Diff for: README.md

+2-38
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
**Effecient error handling in elixir pipelines. See [Handling Errors in Elixir](http://insights.workshop14.io/2015/10/18/handling-errors-in-elixir-no-one-say-monad.html) for a more detailed explination**
44

5+
[Documentation for OK is available on hexdoc](https://hexdocs.pm/ok)
6+
57
## Installation
68

79
[Available in Hex](https://hex.pm/docs/publish), the package can be installed as:
@@ -12,10 +14,6 @@
1214
[{:ok, "~> 0.0.1"}]
1315
end
1416

15-
2. Refetch the dependencies, execute:
16-
17-
$ mix deps.get
18-
1917
## Usage
2018

2119
The OK module works with the native error handling in Erlang/Elixir, that is a result tuple.
@@ -25,21 +23,6 @@ A result tuple is a two-tuple tagged either as a success(`:ok`) or a failure(`:e
2523
{:ok, value} | {:error, reason}
2624
```
2725

28-
### Bind
29-
30-
The primary functionality is the `OK.bind/2` function.
31-
This function takes a result tuple and a next function.
32-
If the result tuple is tagged as a success then it will be passed to the next function.
33-
If the tag is failure then the next function is skipped
34-
35-
```elixir
36-
OK.bind({:ok, 2}, fn (x) -> {:ok, 2 * x} end)
37-
# => {:ok, 4}
38-
39-
OK.bind({:error, :some_reason}, fn (x) -> {:ok, 2 * x} end)
40-
# => {:error, :some_reason}
41-
```
42-
4326
### '~>>' Macro
4427

4528
This macro allows pipelining results through a pipeline of anonymous functions.
@@ -62,22 +45,3 @@ def handle_user_data({:error, :key_not_found}), do: IO.puts("Could not find empl
6245
get_employee_data("my_company/employees.json")
6346
|> handle_user_data
6447
```
65-
66-
### Success
67-
68-
Wraps a value in a successful result tuple.
69-
70-
```elixir
71-
OK.success(:value)
72-
# => {:ok, :value}
73-
```
74-
75-
### Failure
76-
77-
Wraps a reason in a failure result tuple.
78-
79-
```elixir
80-
Ok.failure("reason")
81-
# => {:error, "reason"}
82-
```
83-

Diff for: lib/ok.ex

+58
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,67 @@
11
defmodule OK do
2+
@moduledoc """
3+
The `OK` module enables clean and expressive error handling in pipelines.
4+
5+
Many Elixir libraries follow the tagged tuple convention for functions that will not alway return a valid response.
6+
In case of a success the value is returned in an `:ok` tagged tuple.
7+
If the function fails then a reason is returned in an `:error` tagged tuple.
8+
9+
Calling code the matches on these two possible outcomes.
10+
11+
```elixir
12+
case my_func(args) do
13+
{:ok, value} ->
14+
do_more(value) # continue with subsequent processing
15+
{:error, reason} ->
16+
{:error, reason} # return early.
17+
end
18+
```
19+
20+
`OK` allows this code to be replaced by a result pipeline.
21+
22+
```elixir
23+
my_func(args)
24+
~>> &do_more/1
25+
```
26+
27+
*`OK` treates the combination of tagged tuples `{:ok, value} | {:error, reason}` as a result monad.
28+
The result monad is sometimes know as the try or either monad.*
29+
"""
30+
31+
@doc """
32+
Takes a result tuple and a next function.
33+
If the result tuple is tagged as a success then its value will be passed to the next function.
34+
If the tag is failure then the next function is skipped.
35+
36+
## Examples
37+
38+
iex> OK.bind({:ok, 2}, fn (x) -> {:ok, 2 * x} end)
39+
{:ok, 4}
40+
41+
iex> OK.bind({:error, :some_reason}, fn (x) -> {:ok, 2 * x} end)
42+
{:error, :some_reason}
43+
"""
244
def bind({:ok, value}, func) when is_function(func, 1), do: func.(value)
345
def bind(failure = {:error, _reason}, _func), do: failure
446

47+
@doc """
48+
Wraps a value as a successful result tuple.
49+
50+
## Examples
51+
52+
iex> OK.success(:value)
53+
{:ok, :value}
54+
"""
555
def success(value), do: {:ok, value}
656

57+
@doc """
58+
Creates a failed result tuple with the given reason.
59+
60+
## Examples
61+
62+
iex> OK.failure("reason")
63+
{:error, "reason"}
64+
"""
765
def failure(reason), do: {:error, reason}
866

967
defmacro lhs ~>> rhs do

Diff for: mix.exs

+7-16
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,24 @@ defmodule OK.Mixfile do
33

44
def project do
55
[app: :ok,
6-
version: "0.1.3",
6+
version: "0.1.4",
77
elixir: "~> 1.1",
88
build_embedded: Mix.env == :prod,
99
start_permanent: Mix.env == :prod,
1010
deps: deps,
11-
package: package,
12-
description: description]
11+
description: description,
12+
docs: [extras: ["README.md"], main: "readme"],
13+
package: package]
1314
end
1415

15-
# Configuration for the OTP application
16-
#
17-
# Type "mix help compile.app" for more information
1816
def application do
1917
[applications: [:logger]]
2018
end
2119

22-
# Dependencies can be Hex packages:
23-
#
24-
# {:mydep, "~> 0.3.0"}
25-
#
26-
# Or git/path repositories:
27-
#
28-
# {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"}
29-
#
30-
# Type "mix help deps" for more examples and options
3120
defp deps do
32-
[]
21+
[
22+
{:ex_doc, ">= 0.0.0", only: :dev}
23+
]
3324
end
3425

3526
defp description do

0 commit comments

Comments
 (0)