Permalink
Browse files

Merge branch '0.5-beta'

Conflicts:
	src/compojure/route.clj
	test/compojure/test/route.clj
  • Loading branch information...
2 parents bcafbed + 584f985 commit 2be99257f546273a84dcaa6603966edc07fe6976 @weavejester committed Sep 21, 2010
Showing with 65 additions and 62 deletions.
  1. +1 −1 .gitignore
  2. +7 −7 project.clj
  3. +8 −15 src/compojure/core.clj
  4. +41 −31 src/compojure/response.clj
  5. +2 −2 src/compojure/route.clj
  6. +4 −4 test/compojure/test/response.clj
  7. +2 −2 test/compojure/test/route.clj
View
2 .gitignore
@@ -1,5 +1,5 @@
lib
classes
-compojure.jar
+*.jar
pom.xml
_site
View
14 project.clj
@@ -1,10 +1,10 @@
-(defproject compojure "0.4.1"
+(defproject compojure "0.5.0-beta3"
:description "A concise web framework for Clojure"
:url "http://github/weavejester/compojure"
- :dependencies [[org.clojure/clojure "1.1.0"]
- [org.clojure/clojure-contrib "1.1.0"]
- [clout "0.2.0"]
- [ring/ring-core "0.2.5"]]
- :dev-dependencies [[lein-clojars "0.5.0"]
+ :dependencies [[org.clojure/clojure "1.2.0"]
+ [org.clojure/clojure-contrib "1.2.0"]
+ [clout "0.3.0"]
+ [ring/ring-core "0.3.0"]]
+ :dev-dependencies [[lein-clojars "0.6.0"]
[swank-clojure "1.2.1"]
- [ring/ring-jetty-adapter "0.2.5"]])
+ [ring/ring-jetty-adapter "0.3.0"]])
View
23 src/compojure/core.clj
@@ -1,6 +1,7 @@
(ns compojure.core
"A concise syntax for generating Ring handlers."
- (:use [ring.middleware params cookies]
+ (:use clojure.contrib.def
+ [ring.middleware params cookies]
clout.core
compojure.response))
@@ -60,7 +61,7 @@
(if-let [route-params# (route-matches route# request#)]
(let [request# (#'assoc-route-params request# route-params#)]
(bind-request request# ~bindings
- (render request# (do ~@body)))))))))
+ (render (do ~@body) request#))))))))
(defn routes
"Create a Ring handler by combining several handlers into one."
@@ -70,20 +71,12 @@
(fn [request]
(some #(% request) handlers)))))
-(defn- apply-doc
- "Return a symbol and body with an optional docstring applied."
- [name doc? body]
- (if (string? doc?)
- (list* (vary-meta name assoc :doc doc?) body)
- (list* name doc? body)))
-
(defmacro defroutes
- "Define a Ring handler function from a sequence of routes. Takes an optional
- doc-string."
- [name doc? & routes]
- (let [[name & routes] (apply-doc name doc? routes)]
- `(def ~name
- (routes ~@routes))))
+ "Define a Ring handler function from a sequence of routes. The name may be
+ optionally be followed by a doc-string and metadata map."
+ [name & routes]
+ (let [[name routes] (name-with-attributes name routes)]
+ `(def ~name (routes ~@routes))))
(defmacro GET "Generate a GET route."
[path args & body]
View
72 src/compojure/response.clj
@@ -1,37 +1,47 @@
(ns compojure.response
"Methods for generating Ring response maps"
+ (:use [ring.util.response :only (response header)])
(:import java.util.Map
[java.io File InputStream]
[clojure.lang IDeref IFn ISeq]))
-(defmulti render
- "Given the request map and an arbitrary value x, turn x into a valid HTTP
- response map. Dispatches on the type of x."
- (fn [_ x] (type x)))
-
-(defmethod render nil [_ _] nil)
-
-(defmethod render String [_ html]
- {:status 200
- :headers {"Content-Type" "text/html"}
- :body html})
-
-(defmethod render Map [_ m]
- (merge {:status 200, :headers {}, :body ""} m))
-
-(defmethod render IFn [request handler]
- (render request (handler request)))
-
-(defmethod render IDeref [request ref-like]
- (render request (deref ref-like)))
-
-(defmethod render File [_ file]
- {:status 200, :headers {}, :body file})
-
-(defmethod render ISeq [_ coll]
- {:status 200, :headers {}, :body coll})
-
-(defmethod render InputStream [_ stream]
- {:status 200, :headers {}, :body stream})
-
-(prefer-method render Map IFn)
+(defprotocol Renderable
+ (render [this request]
+ "Render the object into a form suitable for the given request map."))
+
+(extend-type nil
+ Renderable
+ (render [_ _] nil))
+
+(extend-type String
+ Renderable
+ (render [this _]
+ (-> (response this)
+ (header "Content-Type" "text/html"))))
+
+(extend-type Map
+ Renderable
+ (render [this _]
+ (merge (response "") this)))
+
+(extend-type IFn
+ Renderable
+ (render [this request]
+ (render (this request) request)))
+
+(extend-type IDeref
+ Renderable
+ (render [this request]
+ (render (deref this) request)))
+
+(extend-type File
+ Renderable
+ (render [this _] (response this)))
+
+(extend-type ISeq
+ Renderable
+ (render [this _] (response this)))
+
+(extend-type InputStream
+ Renderable
+ (render [this _] (response this)))
View
4 src/compojure/route.clj
@@ -20,10 +20,10 @@
(defn resources
"A route for serving resources on the classpath. Accepts the following
keys:
- :root - the root prefix to get the resources from. Defaults to 'public'."
+ :root - the root prefix to get the resources from. Defaults to '/public'."
[path & [options]]
(GET (add-wildcard path) {{resource-path "*"} :params}
- (let [options (merge {:root "public"} options)]
+ (let [options (merge {:root "/public"} options)]
(resource-response resource-path options))))
(defn not-found
View
8 test/compojure/test/response.clj
@@ -3,21 +3,21 @@
(:require [compojure.response :as response]))
(deftest response-with-nil
- (is (nil? (response/render {} nil))))
+ (is (nil? (response/render nil {}))))
(def test-response
{:status 200
:headers {"Content-Type" "text/html"}
:body "<h1>Foo</h1>"})
(deftest response-with-string
- (is (= (response/render {} "<h1>Foo</h1>")
+ (is (= (response/render "<h1>Foo</h1>" {})
test-response)))
(deftest response-with-fn
- (is (= (response/render {} (constantly test-response))
+ (is (= (response/render (constantly test-response) {})
test-response)))
(deftest response-with-deref
- (is (= (response/render {} (future test-response))
+ (is (= (response/render (future test-response) {})
test-response)))
View
4 test/compojure/test/route.clj
@@ -1,6 +1,6 @@
(ns compojure.test.route
(:use clojure.test
- [clojure.contrib.duck-streams :only (slurp*)])
+ [clojure.contrib.io :only (slurp*)])
(:require [compojure.route :as route]))
(deftest not-found-route
@@ -9,7 +9,7 @@
(is (= (:body response) "foo"))))
(deftest resources-route
- (let [route (route/resources "/foo" {:root "resources"})
+ (let [route (route/resources "/foo" {:root "/resources"})
response (route {:request-method :get, :uri "/foo/test.txt"})]
(is (= (:status response) 200))
(is (= (slurp* (:body response)) "foobar\n"))))

0 comments on commit 2be9925

Please sign in to comment.