Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor Cleanup/Improvements #5

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 18 additions & 7 deletions lib/rop.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,30 @@ defmodule Rop do
Raise the arguments else

For example:
iex> ok({:ok, 1})
iex> do_try({:ok, 1})
1

iex> ok({:error, "some"})
iex> do_try({:error, "some"})
** (RuntimeError) some

iex> ok({:anything, "some"})
iex> do_try({:anything, "some"})
** (ArgumentError) raise/1 and reraise/2 expect a module name, string or exception as the first argument, got: {:anything, \"some\"}
"""
def ok({:ok, x}), do: x
def ok({:error, x}), do: raise x
def ok(x), do: raise x
def do_try({:ok, x}), do: x
def do_try({:error, x}), do: raise x
def do_try(x), do: raise x

@doc ~s"""
Wraps the value in an ok tagged tuple like {:ok, value}
"""
def ok({:ok, x}), do: {:ok, x}
def ok({:error, x}), do: {:error, x}
def ok(x), do: {:ok, x}

@doc ~s"""
Wraps the value in an error tagged tuple like {:error, value}
"""
def err(x), do: {:error, x}

@doc ~s"""
No need to stop pipelining in case of an error somewhere in the middle
Expand Down Expand Up @@ -86,7 +97,7 @@ defmodule Rop do
quote do
(fn ->
try do
unquote(args) |> unquote(func)
{:ok, unquote(args) |> unquote(func)}
rescue
e -> {:error, e}
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ defmodule Rop.Mixfile do
# Type "mix help deps" for more examples and options
defp deps do
[
{:ex_spec, "~> 1.0", only: :test},
{:ex_spec, "~> 2.0", only: :test},
{:earmark, "~> 0.2", only: :dev},
{:ex_doc, "~> 0.11", only: :dev}
]
Expand Down
8 changes: 5 additions & 3 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
%{"earmark": {:hex, :earmark, "0.2.0"},
"ex_doc": {:hex, :ex_doc, "0.11.3"},
"ex_spec": {:hex, :ex_spec, "1.0.0"}}
%{
"earmark": {:hex, :earmark, "0.2.0", "bc1636bc2efa0c1c172a5bcbf7c8eb73632d8c4512a6c2dac02d2ae454750af6", [:mix], [], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.11.3", "bb16cb3f4135d880ce25279dc19a9d70802bc4f4942f0c3de9e4862517ae3ace", [:mix], [{:earmark, "~> 0.1.17 or ~> 0.2", [hex: :earmark, repo: "hexpm", optional: true]}], "hexpm"},
"ex_spec": {:hex, :ex_spec, "2.0.1", "8bdbd6fa85995fbf836ed799571d44be6f9ebbcace075209fd0ad06372c111cf", [:mix], [], "hexpm"},
}
14 changes: 7 additions & 7 deletions test/rop_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule RopTest do
doctest Rop


ExSpec.describe ">>> macro" do
describe ">>> macro" do
test "returns the piped value in case of happy path" do
assert (0 |> inc >>> inc >>> inc) == {:ok, 3}
end
Expand All @@ -23,17 +23,17 @@ defmodule RopTest do
end
end

ExSpec.describe "try_catch macro" do
describe "try_catch macro" do
test "catches and returns raised errors in a tagged tupple { :error, %SomeError{} } if something breaks" do
assert (:raise |> try_catch(arithmetic_error)) == {:error, %ArithmeticError{}}
end

test "returns the value otherwise" do
assert (:pass |> try_catch(arithmetic_error)) == 1
test "returns the piped value otherwise" do
assert (:pass |> try_catch(arithmetic_error)) == {:ok, 1}
end
end

ExSpec.describe "tee" do
describe "tee" do
test "passes the arguments through after executing them in the function" do
a = (fn ->
0 |> simple_inc |> simple_inc |> tee(simple_sideeffect)
Expand All @@ -50,7 +50,7 @@ defmodule RopTest do
end
end

ExSpec.describe "bind" do
describe "bind" do
test "wraps a function to return a tagged tuple `{:ok, result}` from the returned value" do
a = 0 |> simple_inc |> bind(simple_inc)
assert a == {:ok, 2}
Expand Down Expand Up @@ -80,6 +80,6 @@ defmodule RopTest do
end

defp arithmetic_error(:raise) do
1 / 0
raise ArithmeticError
end
end