Created
January 13, 2016 21:21
-
-
Save domgetter/ce5a2d59b4e1817cd782 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Use defroutes to create a defresource macro that generates restful routes like | |
;; Rails: http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions | |
(def restful-routes | |
[['GET "" "/index" ] | |
['GET "/new" "/new" ] | |
['POST "" "/create" ] | |
['GET "/:id" "/show" 'id] | |
['GET "/:id/edit" "/edit" 'id] | |
['PATCH "/:id" "/update" 'id] | |
['PUT "/:id" "/update" 'id] | |
['GET "/:id" "/destroy" 'id]]) | |
(defmacro defresource [resource] | |
(concat (list 'defroutes (read-string (name resource))) | |
(for [[method source action & [param & params]] restful-routes] | |
(list method (str "/" (name resource) source) [] | |
(if param | |
(list (read-string (str (name resource) action)) param) | |
(list (read-string (str (name resource) action)))))))) | |
(macroexpand '(defresource :photos)) | |
;; (defroutes photos | |
;; (GET "/photos" [] (photos/index)) | |
;; (GET "/photos/new" [] (photos/new)) | |
;; (POST "/photos" [] (photos/create)) | |
;; (GET "/photos/:id" [] (photos/show id)) | |
;; (GET "/photos/:id/edit" [] (photos/edit id)) | |
;; (PATCH "/photos/:id" [] (photos/update id)) | |
;; (PUT "/photos/:id" [] (photos/update id)) | |
;; (GET "/photos/:id" [] (photos/destroy id))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment