-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Code tweaking #74
base: 1.3
Are you sure you want to change the base?
Code tweaking #74
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,11 +15,12 @@ | |
|
||
(defn- route->key [action rte] | ||
(let [action (string/replace (str action) #".*/" "")] | ||
(str action (-> rte | ||
(string/replace #"\." "!dot!") | ||
(string/replace #"/" "--") | ||
(string/replace #":" ">") | ||
(string/replace #"\*" "<"))))) | ||
(str action (reduce #(apply string/replace %1 %2) | ||
rte | ||
[[#"\." "!dot!"] | ||
[#"/" "--"] | ||
[#":" ">"] | ||
[#"\*" "<"]])))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find this one harder to read. I think I prefer the old version. |
||
|
||
(defn- throwf [msg & args] | ||
(throw (Exception. (apply format msg args)))) | ||
|
@@ -44,20 +45,19 @@ | |
(let [[action url] (if (vector? cur) | ||
[(keyword->symbol "compojure.core" (first cur)) (second cur)] | ||
[default-action cur]) | ||
final (-> result | ||
(assoc :fn-name (if fn-name | ||
fn-name | ||
(symbol (route->key action url)))) | ||
(assoc :url url) | ||
(assoc :action action))] | ||
final (assoc result | ||
:fn-name (or fn-name | ||
(symbol (route->key action url))) | ||
:url url | ||
:action action)] | ||
[final (rest all)]))) | ||
|
||
(defn- parse-destruct-body [[result [cur :as all]]] | ||
(when-not (some true? (map #(% cur) [vector? map? symbol?])) | ||
(when-not (some #(% cur) [vector? map? symbol?]) | ||
(throwf "Invalid destructuring param: %s" cur)) | ||
(-> result | ||
(assoc :destruct cur) | ||
(assoc :body (rest all)))) | ||
(assoc result | ||
:destruct cur | ||
:body (rest all))) | ||
|
||
(defn ^{:skip-wiki true} parse-args | ||
"parses the arguments to defpage. Returns a map containing the keys :name :action :url :destruct :body" | ||
|
@@ -120,17 +120,16 @@ | |
(when-not (every? (set (keys route-args)) route-arg-names) | ||
(throwf "Missing route-args %s" (vec (filter #(not (contains? route-args %)) route-arg-names)))) | ||
(reduce (fn [path [k v]] | ||
(if (= k :*) | ||
(string/replace path "*" (str v)) | ||
(string/replace path (str k) (str v)))) | ||
(string/replace path | ||
(if (= k :*) "*" (str k)) | ||
(str v))) | ||
url | ||
route-args))) | ||
|
||
(defn url-for-fn* [route-fn route-args] | ||
(let [url (-> route-fn meta ::url)] | ||
(when-not url | ||
(throwf "No url metadata on %s" route-fn)) | ||
(url-for* url route-args))) | ||
(if-let [url (-> route-fn meta ::url)] | ||
(url-for* url route-args) | ||
(throwf "No url metadata on %s" route-fn))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice clean up as well. |
||
|
||
(defmacro url-for | ||
"given a named route, i.e. (defpage foo \"/foo/:id\"), returns the url for the | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,24 +12,24 @@ | |
(re-seq #".*--" k))) | ||
|
||
(defn- key->route-fn [k] | ||
(if (route-fn? k) | ||
(let [with-slahes (-> k | ||
(string/replace #"!dot!" ".") | ||
(string/replace #"--" "/") | ||
(string/replace #">" ":") | ||
(string/replace #"<" "*")) | ||
separated (string/replace with-slahes #"(POST|GET|HEAD|ANY|PUT|DELETE)" #(str (first %1) " :: "))] | ||
separated) | ||
k)) | ||
(if-not (route-fn? k) | ||
k | ||
(reduce #(apply string/replace %1 %2) | ||
k | ||
[[#"!dot!" "."] | ||
[#"--" "/"] | ||
[#">" ":"] | ||
[#"<" "*"] | ||
[#"(POST|GET|HEAD|ANY|PUT|DELETE)" #(str (first %1) " :: ")]]))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the flow of this better... especially with using if-not vs if. |
||
|
||
(defn- ex-item [{anon :annon-fn func :fn nams :ns clj? :clojure f :file line :line :as ex}] | ||
(let [func-name (if (and anon func (re-seq #"eval" func)) | ||
"anon [fn]" | ||
(key->route-fn func)) | ||
ns-str (if clj? | ||
(if (route-fn? func) | ||
(str nams " :: " func-name) | ||
(str nams "/" func-name)) | ||
(str nams | ||
(if (route-fn? func) " :: " "/") | ||
func-name) | ||
(str (:method ex) "." (:class ex))) | ||
in-ns? (and nams (re-seq | ||
(re-pattern (str (options/get :ns))) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice change.