Skip to content

Instantly share code, notes, and snippets.

@j0ni
Created January 2, 2012 22:41
Show Gist options
  • Save j0ni/1552453 to your computer and use it in GitHub Desktop.
Save j0ni/1552453 to your computer and use it in GitHub Desktop.
rest resource macro
(ns j0ni.whatever
(:use compojure.core))
(defmacro explode-resource [rsrc controller-fn attrs]
`(context ~(str "/" rsrc) []
(GET "/"
[]
(~(symbol (controller-fn "index"))))
(GET "/new"
[]
(~(symbol (controller-fn "new"))))
(POST "/"
[~@(map #(symbol (name %)) attrs)]
(~(symbol (controller-fn "create")) ~@(map #(symbol (name %)) attrs)))
(GET "/:id"
[~(symbol "id")]
(~(symbol (controller-fn "show")) ~(symbol "id")))
(DELETE "/:id"
[~(symbol "id")]
(~(symbol (controller-fn "destroy")) ~(symbol "id")))
(GET "/:id/edit"
[~(symbol "id")]
(~(symbol (controller-fn "edit")) ~(symbol "id")))
(PUT "/:id"
[~(symbol "id") ~@(map #(symbol (name %)) attrs)]
(~(symbol (controller-fn "update")) ~(symbol "id") ~@(map #(symbol (name %)) attrs)))))
(defmacro resource
([rsrc attrs]
`(explode-resource ~rsrc ~#(str %) ~attrs))
([rsrc ns attrs]
`(explode-resource ~rsrc ~#(str ns "/" %) ~attrs)))
@j0ni
Copy link
Author

j0ni commented Jan 5, 2012

one day this fucker will work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment