From 2889fd14617ce6f4f74fd6770a0de8e158dc64ef Mon Sep 17 00:00:00 2001 From: Bronsa Date: Thu, 12 Jan 2012 18:57:50 +0100 Subject: [PATCH] Some minor code tweaking Some minor code tweaking --- src/noir/cookies.clj | 7 +++---- src/noir/core.clj | 43 ++++++++++++++++++++--------------------- src/noir/exception.clj | 24 +++++++++++------------ src/noir/statuses.clj | 9 ++++----- src/noir/validation.clj | 5 ++--- 5 files changed, 42 insertions(+), 46 deletions(-) diff --git a/src/noir/cookies.clj b/src/noir/cookies.clj index da46a97..7b2a200 100644 --- a/src/noir/cookies.clj +++ b/src/noir/cookies.clj @@ -24,10 +24,9 @@ ([k] (get k nil)) ([k default] (let [str-k (name k)] - (if-let [v (or (get-in @*new-cookies* [str-k :value]) - (get-in *cur-cookies* [str-k :value]))] - v - default)))) + (or (get-in @*new-cookies* [str-k :value]) + (get-in *cur-cookies* [str-k :value]) + default)))) (defn signed-name [k] "Construct the name of the signing cookie using a simple suffix." diff --git a/src/noir/core.clj b/src/noir/core.clj index 04e2886..1c96335 100644 --- a/src/noir/core.clj +++ b/src/noir/core.clj @@ -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!"] + [#"/" "--"] + [#":" ">"] + [#"\*" "<"]])))) (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))) (defmacro url-for "given a named route, i.e. (defpage foo \"/foo/:id\"), returns the url for the diff --git a/src/noir/exception.clj b/src/noir/exception.clj index 9a310e4..39d8474 100644 --- a/src/noir/exception.clj +++ b/src/noir/exception.clj @@ -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) " :: ")]]))) (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))) diff --git a/src/noir/statuses.clj b/src/noir/statuses.clj index c17d7e3..bdc1a6e 100644 --- a/src/noir/statuses.clj +++ b/src/noir/statuses.clj @@ -23,11 +23,10 @@ (let [{:keys [status headers]} orig content (or (get-page status) (get-page 404)) headers (merge {"Content-Type" "text/html; charset=utf-8"} - headers) - final (-> orig - (assoc :headers headers) - (assoc :body content))] - final)) + headers)] + (assoc orig + :headers headers + :body content))) (defn wrap-status-pages [handler] (fn [request] diff --git a/src/noir/validation.clj b/src/noir/validation.clj index d36b96b..c71d68f 100644 --- a/src/noir/validation.clj +++ b/src/noir/validation.clj @@ -53,9 +53,8 @@ "Explicitly set an error for the given field. This can be used to create complex error cases, such as in a multi-step login process." [field error] - (let [merge-map (if (get-errors field) - {field error} - {field [error]})] + (let [merge-map {field (if (get-errors field) + error [error])}] (swap! *errors* #(merge-with conj % merge-map)) nil))