Skip to content

Instantly share code, notes, and snippets.

@edtsech
Last active December 10, 2015 01:34
Show Gist options
  • Save edtsech/4359055 to your computer and use it in GitHub Desktop.
Save edtsech/4359055 to your computer and use it in GitHub Desktop.
Get Rails like controller syntax from Compojure DSL in 5 minutes with few very simple macros.
(defroutes posts-routes
(context "/posts" []
(GET "/" request ...)
(GET "/new" request ...)
(POST "/" request ...)))
(defmacro defcontroller
([nm & exprs]
`(defroutes ~(symbol (str (name nm) "-routes"))
(context ~(str "/" (name nm)) []
~@exprs))))
(defmacro index [bindings & exprs]
`(GET "/" ~bindings ~@exprs))
(defmacro new [bindings & exprs]
`(GET "/new" ~bindings ~@exprs))
(defmacro create [bindings & exprs]
`(POST "/" ~bindings ~@exprs))
(defcontroller posts
(index request ...)
(new request ...)
(create request ...))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment