Skip to content
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

Open
wants to merge 2 commits into
base: 1.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/noir/cookies.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice change.


(defn signed-name [k]
"Construct the name of the signing cookie using a simple suffix."
Expand Down
43 changes: 21 additions & 22 deletions src/noir/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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!"]
[#"/" "--"]
[#":" ">"]
[#"\*" "<"]]))))
Copy link
Contributor

Choose a reason for hiding this comment

The 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))))
Expand All @@ -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"
Expand Down Expand Up @@ -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)))
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Down
24 changes: 12 additions & 12 deletions src/noir/exception.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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) " :: ")]])))
Copy link
Contributor

Choose a reason for hiding this comment

The 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)))
Expand Down
9 changes: 4 additions & 5 deletions src/noir/statuses.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
5 changes: 2 additions & 3 deletions src/noir/validation.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down