Skip to content

Instantly share code, notes, and snippets.

@cstorey
Created September 8, 2012 11:51
Show Gist options
  • Save cstorey/3674136 to your computer and use it in GitHub Desktop.
Save cstorey/3674136 to your computer and use it in GitHub Desktop.
Stubbing a protocol in Clojurescript.
(require '[cljs.analyzer :as a])
(defn methods-by-protocol [ns]
(->> (a/get-namespace ns)
:defs
(map val)
(filter :protocol)
(reduce
(fn [map d]
(update-in map [(:protocol d)] #(conj (or % #{}) d)))
{})))
(defn methods-in-protocol [qualified-symbol]
(-> qualified-symbol
namespace
symbol
methods-by-protocol
(get qualified-symbol)))
(defmacro moot [proto]
(let [signatures (-> (cljs.analyzer/resolve-var &env proto) :name methods-in-protocol)
methods (map (fn [sig]
`(~(-> sig :name name symbol)
~@(:method-params sig)
#_(impl-goes-here) nil))
signatures)]
`(reify ~proto ~@methods)))
#_(
(defprotocol ReviewDeck
(when-next-card-due [deck thunk]))
(assert (= (when-next-card-due (m/moot ReviewDeck)) nil))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment