Created
May 4, 2010 19:19
-
-
Save arohner/389842 to your computer and use it in GitHub Desktop.
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
;; 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