From aa50c489c1086a0ff386246d6cd954efe807a66b Mon Sep 17 00:00:00 2001 From: Chris Granger Date: Sat, 23 Jul 2011 16:39:23 -0700 Subject: [PATCH] add in retrieval-lifed flashes Signed-off-by: Chris Granger --- src/noir/session.clj | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/noir/session.clj b/src/noir/session.clj index 8bdd5ec..1edaecf 100644 --- a/src/noir/session.clj +++ b/src/noir/session.clj @@ -9,12 +9,6 @@ (declare *noir-session*) (defonce mem (atom {})) -(defn noir-session [handler] - (fn [request] - (binding [*noir-session* (atom (:session request))] - (let [resp (handler request)] - (assoc resp :session @*noir-session*))))) - (defn put! "Associates the key with the given value in the session" [k v] @@ -36,6 +30,27 @@ [k] (swap! *noir-session* dissoc k)) +(defn flash-put! + "Store a value with a lifetime of one retrieval (on the first flash-get, + it is removed). This is often used for passing small messages to pages + after a redirect." + [v] + (put! :_flash v)) + +(defn flash-get + "Retrieve the flash stored value. This will remove the flash from the + session." + [] + (let [flash (get :_flash)] + (remove! :_flash) + flash)) + +(defn noir-session [handler] + (fn [request] + (binding [*noir-session* (atom (:session request))] + (when-let [resp (handler request)] + (assoc resp :session @*noir-session*))))) + (defn wrap-noir-session [handler] (-> handler (noir-session)