Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler Hurst committed Jan 21, 2020
2 parents a259061 + 2b31a45 commit fe87e43
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changelog

## v0.2.0
* Renamed `:error` to `:errors` on Command struct
* Enhanced documentation to show `&run/1` shortcut

## v0.1.0
* Initial release
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Add commandex as a `mix.exs` dependency:
```elixir
def deps do
[
{:commandex, "~> 0.1.0"}
{:commandex, "~> 0.2.0"}
]
end
```
Expand Down Expand Up @@ -74,7 +74,7 @@ The `command/1` macro will define a struct that looks like:
%RegisterUser{
success: false,
halted: false,
error: %{},
errors: %{},
params: %{email: nil, password: nil},
data: %{password_hash: nil, user: nil},
pipelines: [:hash_password, :create_user, :send_welcome_email]
Expand Down Expand Up @@ -107,10 +107,25 @@ Running a command is easy:
%{success: true, data: %{user: user}} ->
# Success! We've got a user now

%{success: false, error: %{password: :not_given}} ->
%{success: false, errors: %{password: :not_given}} ->
# Respond with a 400 or something

%{success: false, error: _error} ->
%{success: false, errors: _errors} ->
# I'm a lazy programmer that writes catch-all error handling
end
```

For even leaner implementations, you can run a command by passing
the params directly into `&run/1` without using `&new/1`:

```elixir
%{email: "[email protected]", password: "asdf1234"}
|> RegisterUser.run()
|> case do
%{success: true, data: %{user: user}} ->
# Success! We've got a user now

%{success: false, errors: _errors} ->
# I'm a lazy programmer that writes catch-all error handling
end
```
16 changes: 8 additions & 8 deletions lib/commandex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ defmodule Commandex do
%RegisterUser{
success: false,
halted: false,
error: %{},
errors: %{},
params: %{email: nil, password: nil},
data: %{password_hash: nil, user: nil},
pipelines: [:hash_password, :create_user, :send_welcome_email]
Expand Down Expand Up @@ -84,18 +84,18 @@ defmodule Commandex do
%{success: true, data: %{user: user}} ->
# Success! We've got a user now
%{success: false, error: %{password: :not_given}} ->
%{success: false, errors: %{password: :not_given}} ->
# Respond with a 400 or something
%{success: false, error: _error} ->
%{success: false, errors: _error} ->
# I'm a lazy programmer that writes catch-all error handling
end
"""

@type command :: %{
__struct__: atom,
data: map,
error: map,
errors: map,
halted: boolean,
params: map,
pipelines: [atom | {module, atom} | function],
Expand Down Expand Up @@ -257,9 +257,9 @@ defmodule Commandex do
end

@doc """
Sets an error for given key and value.
Sets error for given key and value.
`:error` is a map. Putting an error on the same key will overwrite the previous value.
`:errors` is a map. Putting an error on the same key will overwrite the previous value.
def hash_password(command, %{password: nil} = _params, _data) do
command
Expand All @@ -268,8 +268,8 @@ defmodule Commandex do
end
"""
@spec put_error(command, any, any) :: command
def put_error(%{error: error} = command, key, val) do
%{command | error: Map.put(error, key, val)}
def put_error(%{errors: error} = command, key, val) do
%{command | errors: Map.put(error, key, val)}
end

@doc """
Expand Down
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Commandex.MixProject do
use Mix.Project

@version "0.1.0"
@version "0.2.0"

def project do
[
Expand Down Expand Up @@ -37,7 +37,7 @@ defmodule Commandex.MixProject do
defp package do
[
files: ~w(lib mix.exs .formatter.exs README* LICENSE*),
maintainers: ["Henry Popp"],
maintainers: ["Henry Popp", "Tyler Hurst"],
licenses: ["MIT"],
links: %{"GitHub" => "https://github.com/codedge-llc/commandex"}
]
Expand Down

0 comments on commit fe87e43

Please sign in to comment.