Skip to content

Commit

Permalink
Merge pull request #81 from zonotope/v2.0.0
Browse files Browse the repository at this point in the history
V2.0.0
  • Loading branch information
borkdude authored Nov 15, 2018
2 parents c5d7742 + 39090fd commit 16fdf4a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 53 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ A client-side router for ClojureScript.
Add secretary to your `project.clj` `:dependencies` vector:

```clojure
[secretary "1.2.1"]
[secretary "1.2.3"]
```

For the current `SNAPSHOT` version use:
Expand All @@ -40,9 +40,9 @@ To get started `:require` secretary somewhere in your project.

```clojure
(ns app.routes
(:require [secretary.core :as secretary :refer-macros [defroute]])
(:require [secretary.core :as secretary :refer-macros [defroute]]))
```

**Note**: starting ClojureScript v0.0-2371, `:refer` cannot be used to import macros into your project anymore. The proper way to do it is by using `:refer-macros` as above. When using ClojureScript v0.0-2755 or above, if `(:require [secretary.core :as secretary])` is used, macros will be automatically aliased to `secretary`, e.g. `secretary/defroute`.

### Basic routing and dispatch

Expand Down Expand Up @@ -179,7 +179,7 @@ If a URI contains a query string it will automatically be extracted to
```


#### Route rendering
#### Route rendering

While route matching and dispatch is by itself useful, it is often
necessary to have functions which take a map of parameters and return
Expand Down Expand Up @@ -266,7 +266,7 @@ vector.

```clojure
(ns example
(:require [secretary.core :as secretary :include-macros true :refer [defroute]]
(:require [secretary.core :as secretary :refer-macros [defroute]]
[goog.events :as events]
[goog.history.EventType :as EventType])
(:import goog.History))
Expand Down
26 changes: 14 additions & 12 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,27 @@
:comments "same as Clojure"}

:dependencies
[[org.clojure/clojure "1.6.0"]]
[[org.clojure/clojure "1.7.0"]]

:profiles
{:dev {:source-paths ["dev/" "src/"]
:dependencies
[[org.clojure/clojurescript "0.0-3211"]
[com.cemerick/piggieback "0.1.6-SNAPSHOT"]
[weasel "0.6.0"]
[spellhouse/clairvoyant "0.0-33-g771b57f"]]
[[org.clojure/clojurescript "1.7.228"]
[com.cemerick/piggieback "0.2.1"]
[weasel "0.7.0"]
[spellhouse/clairvoyant "0.0-72-g15e1e44"]]
:plugins
[[lein-cljsbuild "1.0.5"]
[com.cemerick/clojurescript.test "0.2.3-SNAPSHOT"]]
[[lein-cljsbuild "1.1.2"]
[lein-doo "0.1.6"]]
:repl-options
{:nrepl-middleware [cemerick.piggieback/wrap-cljs-repl]}}}

:doo {:build "test"}

:aliases
{"run-tests" ["do" "clean," "cljsbuild" "once" "test"]
"test-once" ["do" "clean," "cljsbuild" "once" "test"]
"auto-test" ["do" "clean," "cljsbuild" "auto" "test"]}
{"run-tests" ["do" "clean," "doo" "phantom" "test" "once"]
"test-once" ["do" "clean," "doo" "phantom" "test" "once"]
"auto-test" ["do" "clean," "doo" "phantom" "test" "auto"]}

:cljsbuild
{:builds [{:id "dev"
Expand All @@ -55,8 +57,8 @@

{:id "test"
:source-paths ["src/" "test/"]
:notify-command ["phantomjs" :cljs.test/runner "target/js/test.js"]
:compiler {:output-to "target/js/test.js"
:compiler {:main secretary.test.runner
:output-to "target/js/test.js"
:optimizations :whitespace
:pretty-print true}}

Expand Down
52 changes: 25 additions & 27 deletions src/secretary/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,33 @@
(defn ^:private route-action-form [destruct body]
(let [params (gensym)]
`(fn [~params]
(cond
(map? ~params)
(if (:ring-route? (meta ~params))
~(cond
(map? destruct)
`(let [~destruct ~params]
~@body)
(let [~@(cond
(or (map? destruct)
(vector? params))
[destruct params]

(vector? destruct)
`(if (map? (:params ~params))
(let [{:keys ~destruct} (:params ~params)]
~@body)
(let [~destruct (:params ~params)]
~@body)))
(let [~(if (vector? destruct)
{:keys destruct}
destruct) ~params]
~@body))
(:ring-route? (meta params))
[{:keys destruct} params]

(vector? ~params)
(let [~destruct ~params]
~@body)))))
(map? (:params params))
[{:keys destruct} (:params params)]

(defmacro ^{:arglists '([name route destruct & body])}
defroute
"Define an instance of secretary.core/Route."
[name pattern destruct & body]
(vector? (:params params))
[destruct (:params params)])]
~@body))))

(defmacro ^{:arglists '([pattern destruct & body])}
route
"Define an anonymous instance of secretary.core/Route."
[pattern destruct & body]
(when-not (or (map? destruct) (vector? destruct))
(throw (IllegalArgumentException.
(str "defroute bindings must be a map or vector, given " (pr-str destruct)))))
`(def ~name
(secretary.core/make-route ~pattern ~(route-action-form destruct body))))
(str "route bindings must be a map or vector, given "
(pr-str destruct)))))
`(secretary.core/make-route ~pattern ~(route-action-form destruct body)))

(defmacro ^{:arglists '([name pattern destruct & body])}
defroute
"Define a named instance of secretary.core/Route."
[name pattern destruct & body]
`(def ~name (route ~pattern ~destruct ~body)))
6 changes: 2 additions & 4 deletions test/secretary/test/codec.cljs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
(ns secretary.test.codec
(:require
[cemerick.cljs.test :as t]
[secretary.codec :as codec])
(:require-macros
[cemerick.cljs.test :refer [deftest testing is are]]))
[cljs.test :as t :refer-macros [deftest testing is are]]
[secretary.codec :as codec]))

(deftest query-params-test
(testing "encodes query params"
Expand Down
8 changes: 3 additions & 5 deletions test/secretary/test/core.cljs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
(ns secretary.test.core
(:require
[cemerick.cljs.test :as t]
[secretary.core :as s])
(:require-macros
[cemerick.cljs.test :refer [deftest testing is are]]))
[cljs.test :as t :refer-macros [deftest testing is are]]
[secretary.core :as s]))

;; ---------------------------------------------------------------------
;; Route matching/rendering testing
Expand Down Expand Up @@ -114,7 +112,7 @@
(when-let [ms (s/route-matches r uri)]
[r ms]))
rs)]
(when r (.action r ms))))
(when r (.action r ms))))

(deftest defroute-test
(testing "dispatch! with basic routes"
Expand Down
7 changes: 7 additions & 0 deletions test/secretary/test/runner.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(ns secretary.test.runner
(:require [doo.runner :refer-macros [doo-tests]]
[secretary.test.core]
[secretary.test.codec]))

(doo-tests 'secretary.test.core
'secretary.test.codec)

0 comments on commit 16fdf4a

Please sign in to comment.