diff --git a/src/liberator/core.clj b/src/liberator/core.clj index 3cd5631..8a847cb 100644 --- a/src/liberator/core.clj +++ b/src/liberator/core.clj @@ -71,7 +71,9 @@ (declare handle-exception) (defn decide [name test then else {:keys [resource request] :as context}] - (if (or (fn? test) (contains? resource name)) + (if (or (fn? test) + (instance? clojure.lang.MultiFn test) + (contains? resource name)) (try (let [ftest (or (resource name) test) ftest (make-function ftest) diff --git a/src/liberator/representation.clj b/src/liberator/representation.clj index 825096b..3a82a6f 100644 --- a/src/liberator/representation.clj +++ b/src/liberator/representation.clj @@ -201,10 +201,6 @@ (as-response [this context] (as-response (render-map-generic this context) context)) - clojure.lang.MultiFn - (as-response [multi-fn context] - (as-response (multi-fn context) context)) - ;; If a string is returned, we should carry out the conversion of both the charset and the encoding. String (as-response [this {representation :representation}] diff --git a/src/liberator/util.clj b/src/liberator/util.clj index 60b6f9d..625a3dc 100644 --- a/src/liberator/util.clj +++ b/src/liberator/util.clj @@ -5,7 +5,11 @@ java.util.Date)) (defn make-function [x] - (if (or (fn? x) (keyword? x)) x (constantly x))) + (if (or (fn? x) + (instance? clojure.lang.MultiFn x) + (keyword? x)) + x + (constantly x))) (defn apply-if-function [function-or-value request] (if (fn? function-or-value) diff --git a/test/test_defresource.clj b/test/test_defresource.clj index fcdec87..605c432 100644 --- a/test/test_defresource.clj +++ b/test/test_defresource.clj @@ -11,6 +11,17 @@ (defresource with-multimethod :handle-ok with-multimethod*) +(defmulti with-service-available?-multimethod* + (comp :service-available? :request)) + +(defmethod with-service-available?-multimethod* :available [_] true) + +(defmethod with-service-available?-multimethod* :not-available [_] false) + +(defresource with-decisions-multimethod + :service-available? with-service-available?-multimethod* + :handle-ok (fn [_] "with-service-available?-multimethod")) + (defresource without-param :handle-ok (fn [_] (format "The text is %s" "test"))) @@ -60,7 +71,13 @@ => {:headers {"Vary" "Accept", "Content-Type" "application/json;charset=UTF-8"}, :body "OK", :status 200}) (fact "should allow multi methods as handlers" (with-multimethod {:request-method :get}) - => {:headers {"Content-Type" "text/plain;charset=UTF-8"}, :body "with-multimethod", :status 200})) + => {:headers {"Content-Type" "text/plain;charset=UTF-8"}, :body "with-multimethod", :status 200}) + (fact "should allow multi methods as decisions" + (with-decisions-multimethod {:request-method :get :service-available? :available}) + => {:headers {"Content-Type" "text/plain;charset=UTF-8"}, :body "with-service-available?-multimethod", :status 200}) + (fact "should allow multi methods as decisions alternate path" + (with-decisions-multimethod {:request-method :get :service-available? :not-available}) + => {:headers {"Content-Type" "text/plain;charset=UTF-8"}, :body "Service not available.", :status 503})) (def fn-with-options