Skip to content

Instantly share code, notes, and snippets.

@arohner
Created May 4, 2010 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arohner/389842 to your computer and use it in GitHub Desktop.
Save arohner/389842 to your computer and use it in GitHub Desktop.
;; routes.clj
(ns winston.ui.routes
(:use [clojure.contrib.except :only (throwf)])
(:require [clojure.contrib.string :as str])))
(defn path-for* [routefn route-args]
{:pre [routefn]}
(let [path (-> routefn meta ::http-path)]
(when (not path)
(throwf "no route metadata on %s" routefn))
(reduce (fn [path [key val]]
(assert (keyword? key))
(str/replace-str (str key) (str val) path)) path route-args)))
(defmacro path-for
"Returns the URL path for a var defined using defroutefn. If the route contains any variables, i.e. /foo/:id, route-args is a map of keywords to values to replace.
path-for resolves vars at runtime (not compile time), which means it can be used to refer to vars that aren't required in the current namespace, as long as some other code loaded the var being pointed at."
[routefn & [route-args]]
`(path-for* (resolve (quote ~routefn)) ~route-args))
---------------
;; bar.clj
(ns winston.bar)
(defn b []
(println "b!"))
----
;; foo.clj
(ns winston.foo
(:use [winston.ui.routes :only (path-for)]))
(defn a []
(println "a!")
(println (path-for bar/b)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment