From e7b39431620eb73c7ff583caece36b2885a73596 Mon Sep 17 00:00:00 2001 From: Andrew Mcveigh Date: Tue, 16 Dec 2014 14:52:20 +0100 Subject: [PATCH 1/3] Allow decisions to be multi-methods --- src/liberator/core.clj | 4 +++- src/liberator/util.clj | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/liberator/core.clj b/src/liberator/core.clj index 8e90f5b..abc32e0 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/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) From 38ddb7de0cadfa1090211cd0a5541583efc7a5a5 Mon Sep 17 00:00:00 2001 From: Andrew Mcveigh Date: Sat, 20 Dec 2014 10:38:11 +0100 Subject: [PATCH 2/3] Add tests for multi-method decisions --- test/test_defresource.clj | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) 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 From eb3e7e41591d798313e17bfda81eeace4c6ba06d Mon Sep 17 00:00:00 2001 From: Andrew Mcveigh Date: Sat, 20 Dec 2014 10:40:42 +0100 Subject: [PATCH 3/3] Remove now redundant MultiFn as-response --- src/liberator/representation.clj | 4 ---- 1 file changed, 4 deletions(-) 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}]