Skip to content

Instantly share code, notes, and snippets.

@mhaemmerle
Created September 13, 2012 07:12
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 mhaemmerle/3712523 to your computer and use it in GitHub Desktop.
Save mhaemmerle/3712523 to your computer and use it in GitHub Desktop.
update macro
(defmacro defupdate
[multi-fn dispatch-value & body-fn]
`(. ~(with-meta multi-fn {:tag 'clojure.lang.MultiFn})
addMethod
~dispatch-value
(fn [{:keys [action# verb# body#]}]
(fn [channel# state#]
(let [result# (apply ~@body-fn (state# body#))]
(enqueue-and-close channel# (:response result#))
(log/info "update" result#)
result#)))))
(defmulti update :verb)
(defupdate update :add (fn [_ _] {:result nil :response nil}))
(. (with-meta update {:tag 'clojure.lang.MultiFn})
addMethod
:add
(fn [{:keys [action verb body]}]
(fn [channel state]
(let [result (apply (fn [_ _] {:result nil :response nil}) (state body))]
(enqueue-and-close channel (:response result))
(log/info "update" result)
result)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment