Skip to content

Commit

Permalink
Add resource route :only and :except filters
Browse files Browse the repository at this point in the history
  • Loading branch information
swlkr committed Mar 19, 2019
1 parent 5df1865 commit d699993
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/coast/router.clj
Expand Up @@ -139,10 +139,10 @@
routes
[])
only? (and (not (empty? (filter #(= % :only) ks)))
(vector? (last ks))
(sequential? (last ks))
(not (empty? (last ks))))
except? (and (not (empty? (filter #(= % :except) ks)))
(vector? (last ks))
(sequential? (last ks))
(not (empty? (last ks))))
filter-resources (when (or only? except?) (last ks))
route-ks (if (or only? except?)
Expand Down Expand Up @@ -211,12 +211,26 @@
(filter some?)
(vec))))

(defn resource-fmt [coll]
(apply conj (vec (first coll))
(rest coll)))

(defn resource-args [val]
(if (or (= val '(:only))
(= val '(:except)))
(first val)
val))

(defn resource-route? [route]
(= :resource (first route)))

(defn expand-resource [route]
(if (resource-route? route)
(resource (second route))
(->> (partition-by #(contains? #{:only :except} %) route)
(map resource-args)
(resource-fmt)
(drop 1)
(apply resource))
[route]))

(defn wrap-routes
Expand Down

0 comments on commit d699993

Please sign in to comment.