Multi-method like dispatch

9 views
Skip to first unread message

Kasim Tuman

unread,
Feb 14, 2011, 8:34:34 PM2/14/11
to moustac...@googlegroups.com
Hi Guys,

I am currently using Moustache for routing and middleware. I would like to write following:
(app [""] (index-fn)
       ["getuser"] (getuser-fn))

As:

(defmulti my-handlers dispatch-fn)

(defmethod my-handlers [""]
  (index-fn)

(defmethod my-handlers ["getuser"]
  (getuser-fn)

What is the best approach? Did anyone try to do something similar? Any ideas or thoughts are welcome. I'd appreciate if Chris could respond.


Christophe Grand

unread,
Feb 15, 2011, 3:27:57 AM2/15/11
to moustac...@googlegroups.com
Hi Kasim,

With mustach you can get a seq of the path segments using the & notation:

(app [& path] ...)

which then you can pass to your mmethod

(app [& path] (my-handlers path))

However if your dispatch-fn ends up being identity, you should skip the mmethod and go straight to a good old map mappiing from paths to handlers.

hth,

Christophe
--
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.cgrand.net/ (en)

Kasim Tuman

unread,
Feb 15, 2011, 5:02:05 PM2/15/11
to moustac...@googlegroups.com
Thanks, Chris. I also think good old map is the best choice.
Reply all
Reply to author
Forward
0 new messages