Skip to content

Commit

Permalink
Merge pull request #189 from andrewmcveigh/multi-method-decisions
Browse files Browse the repository at this point in the history
Allow decisions to be multi-methods
  • Loading branch information
ordnungswidrig committed Oct 1, 2015
2 parents dc5fb8b + eb3e7e4 commit 198f18f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/liberator/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 0 additions & 4 deletions src/liberator/representation.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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}]
Expand Down
6 changes: 5 additions & 1 deletion src/liberator/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 18 additions & 1 deletion test/test_defresource.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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")))

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 198f18f

Please sign in to comment.