Created
January 12, 2010 02:41
-
-
Save aboekhoff/274828 to your computer and use it in GitHub Desktop.
dynamic session middleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns #^{:doc "A handful of utilities for handling sessions"} | |
somnium.financier.app.control.session | |
(:refer-clojure :exclude (assoc! dissoc! get))) | |
;;;; declaring session var here for global-ish access to session | |
(def *session* {}) | |
(def *flash* {}) | |
(defn with-dynamic-session | |
[handler] | |
(fn [{:keys [session flash] :as request}] | |
(binding [*session* session | |
*flash* flash] | |
(let [response (handler request)] | |
(assoc response :session *session*))))) | |
(defn clear! | |
"resets the session" | |
[] | |
(set! *session* nil)) | |
(defn get | |
"get a key from the session" | |
([k] (*session* k)) | |
([k else] (or (*session* k) else))) | |
(defn assoc! | |
"associates a value in the session" | |
[k v] | |
(set! *session* (assoc *session* k v))) | |
(defn flash-assoc! | |
[k v] | |
(set! *flash* (assoc *flash* k v))) | |
(defn dissoc! | |
"dissociates a value from the session" | |
[k] | |
(set! *session* (dissoc *session* k))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment