Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

Commit

Permalink
generate actions with to options, allow nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
princemaple committed Feb 17, 2016
1 parent acfd210 commit 1671832
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 23 deletions.
25 changes: 23 additions & 2 deletions lib/extripe/actions/create.ex
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
1 change: 1 addition & 0 deletions lib/extripe/actions/crud.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule Extripe.Actions.CRUD do
opts = Keyword.drop(opts, [:only, :except])

quote do
require Extripe.Utils.Endpoint
Module.put_attribute __MODULE__, :crud_actions, unquote(actions)
Module.put_attribute __MODULE__, :endpoint_opts, unquote(opts)
@before_compile unquote(__MODULE__)
Expand Down
33 changes: 25 additions & 8 deletions lib/extripe/actions/delete.ex
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
28 changes: 25 additions & 3 deletions lib/extripe/actions/index.ex
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
27 changes: 25 additions & 2 deletions lib/extripe/actions/show.ex
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
33 changes: 25 additions & 8 deletions lib/extripe/actions/update.ex
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

0 comments on commit 1671832

Please sign in to comment.