Skip to content

Instantly share code, notes, and snippets.

Created January 7, 2013 10:36
Show Gist options
  • Save anonymous/4474008 to your computer and use it in GitHub Desktop.
Save anonymous/4474008 to your computer and use it in GitHub Desktop.
(defmacro export
"Marks the associated var for export."
[sym]
`(let [v# (var ~sym)]
(alter-meta! v# assoc :export true)))
(defn ns-exports
"Returns a list of vars referred by a namespace that are flagged for export."
[ns-sym]
(let [ns (find-ns ns-sym)
referred-syms (map second (ns-refers ns))
exports (filter #((meta %) :export) referred-syms)]
exports))
(defn- parse-var
"Given a var, return a vector of symbols representing the vars namespace and local name."
[^clojure.lang.Var v]
(let [ns (.ns v)
ns-sym (.name ns)
sym (.sym v)]
[ns-sym sym]))
(defn refer-exports
"Refer all exported symbols refered by the namespace."
[ns-sym]
(let [exports (ns-exports ns-sym)
exports (map parse-var exports)]
(doseq [x exports]
(refer (first x) :only [(second x)]))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment