Skip to content

Commit

Permalink
bump version to 0.1.0-SNAPSHOT
Browse files Browse the repository at this point in the history
  • Loading branch information
Dom Kiva-Meyer committed Feb 27, 2015
2 parents 02dce05 + 5972ea6 commit 0413874
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
16 changes: 12 additions & 4 deletions spec/domkm/silk_spec.cljx
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@
(silk/routes [[:id2 [["foo" "bar" :baz]]]])
[:id3 [nil {"a" :b}]]
[:id4 [["search"] {"q" (silk/? :q {:q "default"})}]]
[:id5 [["users"] {"q" (silk/? :q)}]]]))
[:id5 [["users"] {"q" (silk/? :q)}]]
[:id6 [["test"] {"q" (silk/? (silk/int :q))}]]]))
(spec/with-all clean-params
#(dissoc % :domkm.silk/routes :domkm.silk/url :domkm.silk/pattern :domkm.silk/name))
(spec/it
Expand All @@ -303,7 +304,9 @@
(spec/it
"unmatches successfully"
(spec/should= (silk/map->URL {:query {"a" "c"}})
(silk/unmatch @routes {:domkm.silk/name :id3 :b "c"})))
(silk/unmatch @routes {:domkm.silk/name :id3 :b "c"}))
(spec/should= (silk/map->URL {:path ["test"] :query {"q" nil}})
(silk/unmatch @routes {:domkm.silk/name :id6})))
(spec/it
"unmatches unsuccessfully"
(spec/should-throw AssertionError (silk/unmatch @routes {})))
Expand All @@ -326,7 +329,10 @@
(silk/arrive @routes "/users")))
(spec/should= {:q "bar"}
(@clean-params
(silk/arrive @routes "/users?q=bar"))))
(silk/arrive @routes "/users?q=bar")))
(spec/should= {:q nil}
(@clean-params
(silk/arrive @routes "/test"))))
(spec/it
"departs"
(spec/should= "/foo/bar/bloop"
Expand All @@ -340,4 +346,6 @@
(spec/should= "/users"
(silk/depart @routes :id5))
(spec/should= "/users?q=bar"
(silk/depart @routes :id5 {:q "bar"})))))
(silk/depart @routes :id5 {:q "bar"}))
(spec/should= "/test"
(silk/depart @routes :id6)))))
34 changes: 25 additions & 9 deletions src/domkm/silk.cljx
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,22 @@
(-match [this that])
(-unmatch [this params])
(-match-validator [this])
(-unmatch-validators [this]))
(-unmatch-validators [this])
(-create-default [this v]))

(defn pattern? [x]
(satisfies? Pattern x))

(defn- optional-value-is-allowed [validator]
(fn [x]
(if (= x ::optional-key-has-no-value)
true
(validator x))))

(defn match-validator [ptrn]
{:pre [(pattern? ptrn)]
:post [(fn? %)]}
(-match-validator ptrn))
(optional-value-is-allowed (-match-validator ptrn)))

(defn unmatch-validators [ptrn]
{:pre [(pattern? ptrn)]
Expand Down Expand Up @@ -134,6 +141,10 @@
:post [(match-valid? ptrn %)]}
(-unmatch ptrn params))

(defn create-default
([ptrn] (create-default ptrn nil))
([ptrn v] (-create-default ptrn v)))


;;;; Native Patterns ;;;;

Expand All @@ -158,7 +169,9 @@
(-match-validator [_]
some?)
(-unmatch-validators [this]
{this some?}))
{this some?})
(-create-default [this v]
{this v}))

(defn ^:private match-coll [ks %1s %2s]
(loop [ks ks
Expand Down Expand Up @@ -229,13 +242,16 @@
deserialize
(hash-map param-key)))
(-unmatch [_ params]
(->> param-key
(get params)
serialize))
(let [v (get params param-key)]
(if (= v ::optional-key-has-no-value)
v
(serialize v))))
(-match-validator [_]
string?)
(-unmatch-validators [_]
{param-key validate}))
{param-key (optional-value-is-allowed validate)})
(-create-default [_ v]
(create-default param-key v)))

(defn regex
([k re]
Expand Down Expand Up @@ -316,7 +332,7 @@
Pattern
(-match [_ that]
(if (nil? that)
(or default-params {ptrn nil})
(or default-params (create-default ptrn))
(match ptrn that)))
(-unmatch [_ params]
(let [r (unmatch ptrn
Expand All @@ -325,7 +341,7 @@
dval
pval))
params
(or default-params {ptrn ::optional-key-has-no-value})))]
(or default-params (create-default ptrn ::optional-key-has-no-value))))]
(if-not (= ::optional-key-has-no-value r)
r)))
(-match-validator [_]
Expand Down

0 comments on commit 0413874

Please sign in to comment.