Created
September 13, 2012 07:12
-
-
Save mhaemmerle/3712523 to your computer and use it in GitHub Desktop.
update macro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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