Skip to content

Instantly share code, notes, and snippets.

@arohner
Created October 10, 2012 04:34
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 arohner/3863185 to your computer and use it in GitHub Desktop.
Save arohner/3863185 to your computer and use it in GitHub Desktop.
macro generating macro
(defmacro def-with-disable-macro
"Generates a pair of macros, with-enable-foo and with-disable-foo, which use with-redefs to replace foo with (constantly nil), or restore it."
[v short-name]
(let [disable-name (symbol (format "with-disable-%s" short-name))
enable-name (symbol (format "with-enable-%s" short-name))]
`(do
(let [v# #'~v
v-name# (quote ~v)
old-value# (deref #'~v)
ns# *ns*]
(defmacro ~disable-name [& body#]
`(with-redefs [~v-name# (constantly nil)]
(do
~@body#)))
(defmacro ~enable-name [& body#]
`(do
(with-redefs [~v-name# ~old-value#]
(do
~@body#))))))))
@arohner
Copy link
Author

arohner commented Oct 10, 2012

(defmacro def-with-disable-macro
"Generates a pair of macros, with-enable-foo and with-disable-foo, which use with-redefs to replace foo with (constantly nil), or restore it."
[v short-name](let [disable-name %28symbol %28format "with-disable-%s" short-name))
enable-name (symbol (format "with-enable-%s" short-name))]
(do (let [v# #'~v v-name# (quote ~v) old-value# (deref #'~v) ns# *ns*] (defmacro ~disable-name [& body#] (with-redefs [~v-name# (constantly nil)](do
~@Body#)))
(defmacro ~enable-name [& body#]
`(do
(with-redefs [~v-name# ~old-value#](do
~@Body#))))))))

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