Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Replaced render multimethod with Renderable protocol

  • Loading branch information...
commit 0411fa7ec76dfc5ccdfe2d5f19422539448ee6ec 1 parent bceba31
@weavejester authored
Showing with 43 additions and 31 deletions.
  1. +43 −31 src/compojure/response.clj
View
74 src/compojure/response.clj
@@ -4,34 +4,46 @@
[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 _]
+ {:status 200
+ :headers {"Content-Type" "text/html"}
+ :body this}))
+
+(extend-type Map
+ Renderable
+ (render [this _]
+ (merge {:status 200, :headers {}, :body ""} m)))
+
+(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 _]
+ {:status 200, :headers {}, :body file}))
+
+(extend-type ISeq
+ Renderable
+ (render [this _]
+ {:status 200, :headers {}, :body coll}))
+
+(extend-type InputStream
+ (render [this _]
+ {:status 200, :headers {}, :body stream}))
Please sign in to comment.
Something went wrong with that request. Please try again.