Browse files

Removed access to *ns* from helper.

  • Loading branch information...
1 parent ea71505 commit f7ddfb38cc58451b729a0a5c11cf90c4e421e3ce @r0man committed Aug 11, 2012
Showing with 32 additions and 52 deletions.
  1. +7 −3 src/clj/routes/core.clj
  2. +19 −34 src/clj/routes/helper.clj
  3. +6 −15 test/clj/routes/test/helper.clj
View
10 src/clj/routes/core.clj
@@ -1,16 +1,20 @@
(ns routes.core
- (:require [routes.helper :refer [route make-params parse-pattern make-route route-args register]]
+ (:require [routes.helper :refer [route make-params parse-pattern make-route]]
+ [routes.helper :refer [route-args register qualified?]]
[routes.server :refer [*server*]]))
+(defn- qualify [s]
+ (symbol (str (if-not (qualified? s) (str *ns* "/")) s)))
+
(defmacro defroute
"Define a route."
[name args pattern & {:as options}]
(let [name# name
args# args
pattern# pattern
- options# options
+ options# (assoc options :root (route (qualify (:root options))))
symbol# (symbol (str name# "-route"))
- route# (register (make-route symbol# args pattern options))]
+ route# (register (make-route *ns* symbol# args pattern options#))]
`(do
(def ^:export ~symbol#
(routes.helper/register
View
53 src/clj/routes/helper.clj
@@ -21,24 +21,18 @@
content (if-not (empty? content) content)]
[:a {:href href} content]))
-(defn qualified?
- "Returns true if the `s` is namespace qualified, otherwise false."
- [s] (if s (re-matches #".+/.+" (str s))))
-
-(defn qualify [sym]
- (when sym
- (if (qualified? sym)
- (symbol sym)
- (symbol (str *ns* "/" sym)))))
-
(defn route
"Lookup a route by `symbol`."
- [sym] (get @*routes* (qualify sym)))
+ [sym] (get @*routes* sym))
+
+(defn route-symbol
+ "Returns the namespace qualified name of `route` as a symbol."
+ [route] (symbol (str (:ns route) "/" (:name route))))
(defn register
"Register `route` by it's name."
[route]
- (swap! *routes* assoc (:qualified route) route)
+ (swap! *routes* assoc (route-symbol route) route)
route)
(defn parse-keys [pattern]
@@ -119,18 +113,16 @@
"Returns the server of the route."
[route] (if route (or (:server route) (route-server (:root route)))))
-(defn make-route [name args [pattern & params] & [options]]
- (let [ns (symbol (str (or (:ns options) *ns*)))
- root (route (:root options))]
- (map->Route
- {:ns ns
- :name (symbol name)
- :qualified (symbol (str ns "/" name))
- :root root
- :args args
- :pattern (parse-pattern pattern)
- :params (apply make-params pattern params)
- :server (or (:server options) (route-server (:root options)))})))
+(defn make-route [ns name args [pattern & params] & [options]]
+ (map->Route
+ {:ns ns
+ :name (symbol name)
+ :qualified (symbol (str ns "/" name))
+ :root (:root options)
+ :args args
+ :pattern (parse-pattern pattern)
+ :params (apply make-params pattern params)
+ :server (route-server options)}))
(defn format-path [route & args]
(->> (map (fn [arg params]
@@ -150,17 +142,10 @@
(flatten)
(apply format (route-pattern route))))
-;; (defn format-path [route & args]
-;; (interleave (route-params route) args))
-
(defn format-url [route & args]
(str (server-url (or *server* (:server route)))
(apply format-path route args)))
-;; (require '[routes.params :as params])
-
-;; (make-route 'root '[] ["/"] :server {:server-name "example.com"})
-
-;; (make-route 'country '[]
-;; ["/:iso-3166-1-alpha-2-:name" params/iso-3166-1-alpha-2 params/string]
-;; :root 'countries-route)
+(defn qualified?
+ "Returns true if the `s` is namespace qualified, otherwise false."
+ [s] (if s (re-matches #".+/.+" (str s))))
View
21 test/clj/routes/test/helper.clj
@@ -29,19 +29,6 @@
country-of-continent-2-route [europe spain]
"https://example.com/continents/1-europe/countries/es-spain"))
-(deftest test-qualified?
- (is (not (qualified? nil)))
- (is (not (qualified? "")))
- (is (not (qualified? 'root-route)))
- (is (qualified? 'routes.test.fixtures/root-route)))
-
-(deftest test-qualify
- (is (nil? (qualify nil)))
- (is (= (symbol (str *ns* "/a"))
- (qualify 'a)))
- (is (= 'routes.test.helper/a
- (qualify 'routes.test.helper/a))))
-
(deftest link-to-test
(is (= (link-to "http://example.com/")
[:a {:href "http://example.com/"} nil]))
@@ -55,10 +42,14 @@
(is (nil? (route "")))
(is (nil? (route 'unknown-route))))
+(deftest test-route-symbol
+ (is (= (symbol (str *ns* "/example-route"))
+ (route-symbol {:ns *ns* :name 'example-route}))))
+
(deftest test-register
- (let [example-route {:qualified 'routes.test.helper/example-route}]
+ (let [example-route {:ns *ns* :name 'example-route}]
(register example-route)
- (is (= example-route (route 'routes.test.helper/example-route)))))
+ (is (= example-route (route (route-symbol example-route))))))
(deftest test-split-by
(are [coll counts expected]

0 comments on commit f7ddfb3

Please sign in to comment.