Skip to content

Instantly share code, notes, and snippets.

@flyingmachine
Created June 15, 2012 02:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flyingmachine/2934281 to your computer and use it in GitHub Desktop.
Save flyingmachine/2934281 to your computer and use it in GitHub Desktop.
routing for noir
(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])
(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