domkm / silk

Routing for Clojure & ClojureScript
222 stars 13 forks source link

Custom Patterns #8

Closed leblowl closed 9 years ago

leblowl commented 9 years ago

Hey stoked on the tool, thank you! I thought I'd share a little keyword pattern that I cooked up:

(defrecord IDKey [k]
  silk/Pattern
  (-match [_ s]
    (hash-map k (keyword s)))
  (-unmatch [_ params]
    (name (get params k)))
  (-match-validator [_]
    string?)
  (-unmatch-validators [_]
    {k keyword?}))

It simply coerces strings to keywords. It helps me in clojurescript land when moving data between clj and js. Cheers :)

leblowl commented 9 years ago

I guess I didn't fully understand patterns and how they fit into matching/unmatching...when I unmatch to find the route and then match with that route, the argument stays the same. However what this will do is check to see you if the arg you pass is a keyword!