Skip to content

Commit

Permalink
round two.
Browse files Browse the repository at this point in the history
strip out hooks and filters in favor of plugs. plug stack in
controllers allows for before/after functionality, but router
stack doesn't. not sure that it needs it.
add functionality/tests for matched controller/action in
conn.private.
improved test coverage.
  • Loading branch information
slogsdon committed Dec 24, 2014
1 parent 7bcbed3 commit 29d76da
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 392 deletions.
4 changes: 3 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use Mix.Config

config :sugar, views_dir: "test/fixtures/templates"
config :sugar,
config_test: [true],
views_dir: "test/fixtures/templates"
133 changes: 0 additions & 133 deletions lib/sugar/controller/hooks.ex

This file was deleted.

49 changes: 26 additions & 23 deletions lib/sugar/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ defmodule Sugar.Router do
Routes are defined with the form:
method route [guard], handler, action
method route [guard], controller, action
`method` is `get`, `post`, `put`, `patch`, or `delete`, each
responsible for a single HTTP method. `method` can also be `any`, which will
match on all HTTP methods. `options` is yet another option for `method`, but
when using `options`, only a route path and the methods that route path
supports are needed. `handler` is any valid Elixir module name, and
`action` is any valid public function defined in the `handler` module.
supports are needed. `controller` is any valid Elixir module name, and
`action` is any valid public function defined in the `controller` module.
`get/3`, `post/3`, `put/3`, `patch/3`, `delete/3`, `options/2`, and `any/3`
are already built-in as described. `resource/2` exists but will need
Expand Down Expand Up @@ -156,12 +156,12 @@ defmodule Sugar.Router do
## Arguments
* `route` - `String|List`
* `handler` - `Atom`
* `controller` - `Atom`
* `action` - `Atom`
"""
@spec unquote(verb)(binary | list, atom, atom) :: ast
defmacro unquote(verb)(route, handler, action) do
build_match unquote(verb), route, handler, action, __CALLER__
defmacro unquote(verb)(route, controller, action) do
build_match unquote(verb), route, controller, action, __CALLER__
end
end

Expand All @@ -185,16 +185,16 @@ defmodule Sugar.Router do
* `method` - `Atom`
* `route` - `String|List`
* `handler` - `Atom`
* `controller` - `Atom`
* `action` - `Atom`
"""
@spec raw(atom, binary | list, atom, atom) :: ast
defmacro raw(method, route, handler, action) do
build_match method, route, handler, action, __CALLER__
defmacro raw(method, route, controller, action) do
build_match method, route, controller, action, __CALLER__
end

@doc """
Creates RESTful resource endpoints for a route/handler
Creates RESTful resource endpoints for a route/controller
combination.
## Example
Expand All @@ -214,7 +214,7 @@ defmodule Sugar.Router do
options, "/users/:_id", "HEAD,GET,PUT,PATCH,DELETE"
"""
@spec resource(atom, atom, Keyword.t) :: [ast]
defmacro resource(resource, handler, opts \\ []) do
defmacro resource(resource, controller, opts \\ []) do
arg = Keyword.get opts, :arg, :id
allowed = Keyword.get opts, :only, [ :index, :create, :show,
:update, :patch, :delete ]
Expand All @@ -231,12 +231,12 @@ defmodule Sugar.Router do
{ :delete, "#{prepend_path}#{resource}/:#{arg}", :delete } ]

options_routes =
[ { "/#{ignore_args prepend_path}#{resource}", [ index: :get, create: :post ] },
{ "/#{ignore_args prepend_path}#{resource}/:_#{arg}", [ show: :get, update: :put,
patch: :patch, delete: :delete ] } ]
[ { "/" <> ignore_args(prepend_path) <> "#{resource}", [ index: :get, create: :post ] },
{ "/" <> ignore_args(prepend_path) <> "#{resource}/:_#{arg}", [ show: :get, update: :put,
patch: :patch, delete: :delete ] } ]

for { method, path, action } <- routes |> filter(allowed) do
build_match method, path, handler, action, __CALLER__
build_match method, path, controller, action, __CALLER__
end ++ for { path, methods } <- options_routes do
allows = methods
|> filter(allowed)
Expand Down Expand Up @@ -271,10 +271,10 @@ defmodule Sugar.Router do

do_build_match :options, route, body, caller
end
defp build_match(method, route, handler, action, caller) do
body = build_body handler, action
# body_json = build_body handler, action, :json
# body_xml = build_body handler, action, :xml
defp build_match(method, route, controller, action, caller) do
body = build_body controller, action
# body_json = build_body controller, action, :json
# body_xml = build_body controller, action, :xml

[ #do_build_match(method, route <> ".json", body_json, caller),
#do_build_match(method, route <> ".xml", body_xml, caller),
Expand All @@ -294,8 +294,8 @@ defmodule Sugar.Router do
end
end

defp build_body(handler, action), do: build_body(handler, action, :skip)
defp build_body(handler, action, add_header) do
defp build_body(controller, action), do: build_body(controller, action, :skip)
defp build_body(controller, action, add_header) do
header = case add_header do
:json -> [{"accept", "application/json"}]
:xml -> [{"accept", "application/xml"}]
Expand All @@ -304,8 +304,11 @@ defmodule Sugar.Router do

quote do
opts = [ action: unquote(action), args: binding() ]
unquote(handler).call %{ conn | req_headers: unquote(header) ++
conn.req_headers }, unquote(handler).init(opts)
%{ conn | req_headers: unquote(header) ++ conn.req_headers,
private: conn.private
|> Map.put(:controller, unquote(controller))
|> Map.put(:action, unquote(action)) }
|> unquote(controller).call(unquote(controller).init(opts))
end
end

Expand Down
90 changes: 0 additions & 90 deletions lib/sugar/router/filters.ex

This file was deleted.

3 changes: 2 additions & 1 deletion lib/sugar/views/finder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ defmodule Sugar.Views.Finder do
List of `Sugar.Templates.Template`
"""
def all(root) do
Path.wildcard("#{root}/**/*.*")
root <> "/**/*.*"
|> Path.wildcard
|> Enum.map(fn (path) -> build(path) end)
end

Expand Down
5 changes: 5 additions & 0 deletions test/sugar/config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ defmodule Sugar.ConfigTest do
import Sugar.Config

test "get/1" do
expected_list = [true, {:http, [port: 4000]},
{:https, [certfile: "",
keyfile: "",
port: 4443]}]
assert get(:router) === nil
assert get(:config_test) === expected_list
end

test "get/2" do
Expand Down
Loading

0 comments on commit 29d76da

Please sign in to comment.