Skip to content

Commit

Permalink
Added make-route function for low-level route creation
Browse files Browse the repository at this point in the history
  • Loading branch information
weavejester committed May 9, 2012
1 parent 99544ec commit ec4e6ad
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/compojure/core.clj
Expand Up @@ -83,14 +83,22 @@
`(let [~@(vector-bindings bindings request)] ~@body)
`(let [~bindings ~request] ~@body)))

(defn make-route
"Returns a function that will only call the handler if the method and Clout
route match the request."
[method route handler]
(if-method method
(if-route route
(fn [request]
(render (handler request) request)))))

(defn- compile-route
"Compile a route in the form (method path & body) into a function."
[method route bindings body]
`(#'if-method ~method
(#'if-route ~(prepare-route route)
(fn [request#]
(let-request [~bindings request#]
(render (do ~@body) request#))))))
`(make-route
~method ~(prepare-route route)
(fn [request#]
(let-request [~bindings request#] ~@body))))

(defn routing
"Apply a list of routes to a Ring request map."
Expand Down
9 changes: 9 additions & 0 deletions test/compojure/test/core.clj
Expand Up @@ -133,3 +133,12 @@
body)
"/foo" "foo"
"/bar" "bar")))

(deftest make-route-test
(let [handler (make-route :get (route-compile "/foo/:id") #(-> % :params :id))]
(are [method url body] (= (:body (handler (request method url)))
body)
:get "/foo/10" "10"
:post "/foo/10" nil
:get "/bar/10" nil
:get "/foo" nil)))

0 comments on commit ec4e6ad

Please sign in to comment.