Created
June 15, 2012 02:05
-
-
Save flyingmachine/2934281 to your computer and use it in GitHub Desktop.
routing for noir
This file contains hidden or 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
(ns routey.views.arenas | |
(:require [routey.views.common :as common] | |
[noir.content.pages :as pages]) | |
(:use noir.core | |
hiccup.core | |
hiccup.page-helpers | |
routey.views.routes)) | |
(defpage-r shiny {} | |
(common/layout | |
[:h2 "Create an Arena"])) | |
(defpage-r edit [id]) |
This file contains hidden or 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
(ns routey.views.routes | |
(require [clojure.string :as string])) | |
(defn- throwf [msg & args] | |
(throw (Exception. (apply format msg args)))) | |
(def routes '{:arenas/shiny [shiny "/arenas/new"] | |
:arenas/edit [[:get "/arenas/edit/:id"]]}) | |
(defn url-forr | |
([route-name] (url-forr route-name {})) | |
([route-name route-args] | |
(let [route (last (flatten (route-name routes))) | |
route-arg-names (noir.core/route-arguments route)] | |
(when (nil? route) | |
(throwf "missing route for %s" route-name)) | |
(when (not (every? #(contains? route-args %) route-arg-names)) | |
(throwf "missing route-arg for %s" [route-args route-arg-names])) | |
(reduce (fn [path [k v]] | |
(assert (keyword? k)) | |
(string/replace path (str k) (str v))) route route-args)))) | |
(defmacro defpage-r [route & body] | |
`(noir.core/defpage ~@((keyword (str (re-find #"[^.]*$" (str (ns-name *ns*))) "/" route)) routes) ~@body)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment