This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generate actions with to options, allow nesting
- Loading branch information
1 parent
acfd210
commit 1671832
Showing
6 changed files
with
124 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,30 @@ | ||
defmodule Extripe.Actions.Create do | ||
alias Extripe.API | ||
alias Extripe.Utils.Endpoint | ||
|
||
defmacro __using__(opts) do | ||
{scope, opts} = Keyword.pop(opts, :scope) | ||
{resource, opts} = Keyword.pop(opts, :resource) | ||
|
||
code = cond do | ||
is_tuple(scope) or is_nil(scope) -> | ||
quote do | ||
def create(params), do: API.post(create_url, params) | ||
defp create_url do | ||
Endpoint.build(unquote(scope), nil, unquote(resource)) | ||
end | ||
end | ||
is_binary(scope) -> | ||
quote do | ||
def create(scope_id, params), do: API.post(create_url(scope_id), params) | ||
defp create_url(scope_id) do | ||
Endpoint.build(unquote(scope), scope_id, unquote(resource)) | ||
end | ||
end | ||
end | ||
|
||
defmacro __using__(_) do | ||
quote do | ||
def create(params), do: API.post("/#{@resource}", params) | ||
unquote(code) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,32 @@ | ||
defmodule Extripe.Actions.Delete do | ||
alias Extripe.API | ||
alias Extripe.Utils.Endpoint | ||
|
||
defmacro __using__(_) do | ||
quote do | ||
def delete(id) when is_binary(id) do | ||
API.delete("/#{@resource}" <> "/#{id}") | ||
end | ||
defmacro __using__(opts) do | ||
{scope, opts} = Keyword.pop(opts, :scope) | ||
{resource, opts} = Keyword.pop(opts, :resource) | ||
|
||
code = cond do | ||
is_tuple(scope) or is_nil(scope) -> | ||
quote do | ||
def delete(id), do: API.delete(delete_url(id)) | ||
defdelegate destroy(id), to: __MODULE__, as: :delete | ||
defp delete_url(id) do | ||
Endpoint.build(unquote(scope), nil, unquote(resource), id) | ||
end | ||
end | ||
is_binary(scope) -> | ||
quote do | ||
def delete(scope_id, id), do: API.delete(delete_url(scope_id, id)) | ||
defdelegate destroy(scope_id, id), to: __MODULE__, as: :delete | ||
defp delete_url(scope_id, id) do | ||
Endpoint.build(unquote(scope), scope_id, unquote(resource), id) | ||
end | ||
end | ||
end | ||
|
||
def delete(params) when is_map(params) do | ||
API.delete("/#{@resource}" <> "/#{params.id}") | ||
end | ||
quote do | ||
unquote(code) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,32 @@ | ||
defmodule Extripe.Actions.Index do | ||
alias Extripe.API | ||
alias Extripe.Utils.Endpoint | ||
|
||
defmacro __using__(opts) do | ||
{scope, opts} = Keyword.pop(opts, :scope) | ||
{resource, opts} = Keyword.pop(opts, :resource) | ||
|
||
code = cond do | ||
is_tuple(scope) or is_nil(scope) -> | ||
quote do | ||
def index, do: API.get(index_url) | ||
defdelegate list, to: __MODULE__, as: :index | ||
defp index_url do | ||
Endpoint.build(unquote(scope), nil, unquote(resource)) | ||
end | ||
end | ||
is_binary(scope) -> | ||
quote do | ||
def index(scope_id), do: API.get(index_url(scope_id)) | ||
defdelegate list(scope_id), to: __MODULE__, as: :index | ||
defp index_url(scope_id) do | ||
Endpoint.build(unquote(scope), scope_id, unquote(resource)) | ||
end | ||
end | ||
end | ||
|
||
defmacro __using__(_) do | ||
quote do | ||
def index, do: API.get("/#{@resource}") | ||
def list, do: API.get("/#{@resource}") | ||
unquote(code) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,32 @@ | ||
defmodule Extripe.Actions.Show do | ||
alias Extripe.API | ||
alias Extripe.Utils.Endpoint | ||
|
||
defmacro __using__(opts) do | ||
{scope, opts} = Keyword.pop(opts, :scope) | ||
{resource, opts} = Keyword.pop(opts, :resource) | ||
|
||
code = cond do | ||
is_tuple(scope) or is_nil(scope) -> | ||
quote do | ||
def show(id), do: API.get(show_url(id)) | ||
defdelegate fetch(id), to: __MODULE__, as: :show | ||
defp show_url(id) do | ||
Endpoint.build(unquote(scope), nil, unquote(resource), id) | ||
end | ||
end | ||
is_binary(scope) -> | ||
quote do | ||
def show(scope_id, id), do: API.get(show_url(scope_id, id)) | ||
defdelegate fetch(scope_id, id), to: __MODULE__, as: :show | ||
defp show_url(scope_id, id) do | ||
Endpoint.build(unquote(scope), scope_id, unquote(resource), id) | ||
end | ||
end | ||
end | ||
|
||
defmacro __using__(_) do | ||
quote do | ||
def show(id), do: API.get("/#{@resource}" <> "/#{id}") | ||
unquote(code) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,32 @@ | ||
defmodule Extripe.Actions.Update do | ||
alias Extripe.API | ||
alias Extripe.Utils.Endpoint | ||
|
||
defmacro __using__(_) do | ||
quote do | ||
def update(params) do | ||
update(params.id, Map.delete(params)) | ||
end | ||
defmacro __using__(opts) do | ||
{scope, opts} = Keyword.pop(opts, :scope) | ||
{resource, opts} = Keyword.pop(opts, :resource) | ||
|
||
code = cond do | ||
is_tuple(scope) or is_nil(scope) -> | ||
quote do | ||
def update(params), do: update(params.id, Map.delete(params, :id)) | ||
def update(id, params), do: API.post(update_url(id), params) | ||
defp update_url(id) do | ||
Endpoint.build(unquote(scope), nil, unquote(resource), id) | ||
end | ||
end | ||
is_binary(scope) -> | ||
quote do | ||
def update(scope_id, params), do: update(scope_id, params.id, Map.delete(params, :id)) | ||
def update(scope_id, id, params), do: API.post(update_url(scope_id, id), params) | ||
defp update_url(scope_id, id) do | ||
Endpoint.build(unquote(scope), scope_id, unquote(resource), id) | ||
end | ||
end | ||
end | ||
|
||
def update(id, params) do | ||
API.post("/#{@resource}" <> "/#{id}", params) | ||
end | ||
quote do | ||
unquote(code) | ||
end | ||
end | ||
end |