Skip to content

Instantly share code, notes, and snippets.

@domgetter
Created January 13, 2016 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save domgetter/ce5a2d59b4e1817cd782 to your computer and use it in GitHub Desktop.
Save domgetter/ce5a2d59b4e1817cd782 to your computer and use it in GitHub Desktop.
;; 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