From 228a2eff39b4f7c257de77b3387bd59190a7c684 Mon Sep 17 00:00:00 2001 From: gered Date: Sat, 14 Feb 2015 15:42:35 -0500 Subject: [PATCH] split out session helpers into separate namespace mainly done for code readability. nicer to alias the namespace separately and be able to write code like (session/assoc :foo "bar") instead of (response/session-assoc :foo "bar") but clearly a minor cosmetic change regardless --- src/clj_webtoolbox/response.clj | 44 --------------------------- src/clj_webtoolbox/session.clj | 53 +++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 44 deletions(-) create mode 100644 src/clj_webtoolbox/session.clj diff --git a/src/clj_webtoolbox/response.clj b/src/clj_webtoolbox/response.clj index 549120d..883e403 100644 --- a/src/clj_webtoolbox/response.clj +++ b/src/clj_webtoolbox/response.clj @@ -80,47 +80,3 @@ cookies set in the existing response." [resp name value & [opts]] (assoc-in resp [:cookies name] (merge (:value value opts)))) - -(defn session - "Returns an updated Ring response with session information added. This will - overwrite existing session data." - [resp session] - (assoc resp :session session)) - -(defn using-session - "Returns an updated Ring response with session information added from the - Ring request. You can use this to set the initial session to be modified - by subsequent functions to modify the response's session map (so as to - modify the existing session rather then overwrite it completely)." - [resp request] - (assoc resp :session (:session request))) - -(defn session-assoc - "Returns an updated Ring response with the new key/value set in the - response's session map." - [resp k v] - (assoc-in resp [:session k] v)) - -(defn session-dissoc - "Returns an updated Ring response with the key removed from the response's - session map." - [resp k] - (update-in resp [:session] dissoc k)) - -(defn session-assoc-in - "Returns an updated Ring response with the new key/value set in the - response's session map. ks should be a vector of keywords referring - to a nested value to set in the session. Any levels that do not exist - will be created." - [resp ks v] - (assoc-in resp (concat [:session] ks) v)) - -(defn session-update-in - "Returns an updated Ring response where a specific value within the - existing response's session map is 'updated' using function f which - takes the existing value along with any supplied args and should - return the new value. ks should be a vector of keywords referring - to a nested value to update. Any levels that do not exist will be - created." - [resp ks f & args] - (apply update-in resp (concat [:session] ks) f args)) \ No newline at end of file diff --git a/src/clj_webtoolbox/session.clj b/src/clj_webtoolbox/session.clj new file mode 100644 index 0000000..0fb7196 --- /dev/null +++ b/src/clj_webtoolbox/session.clj @@ -0,0 +1,53 @@ +(ns clj-webtoolbox.session + "Convenience helper functions for applying session data to Ring response maps." + (:refer-clojure :exclude [set dissoc assoc assoc-in update-in])) + +(defn set + "Returns an updated Ring response with session information added. This will + overwrite existing session data." + [resp session] + (clojure.core/assoc resp :session session)) + +(defn set-from-request + "Returns an updated Ring response with session information added from the + Ring request. You can use this to set the initial session to be modified + by subsequent functions to modify the response's session map (so as to + modify the existing session rather then overwrite it completely)." + [resp request] + (clojure.core/assoc resp :session (:session request))) + +(defn assoc + "Returns an updated Ring response with the new key/value set in the + response's session map." + [resp k v] + (clojure.core/assoc-in resp [:session k] v)) + +(defn dissoc + "Returns an updated Ring response with the key removed from the response's + session map." + [resp k] + (clojure.core/update-in resp [:session] clojure.core/dissoc k)) + +(defn assoc-in + "Returns an updated Ring response with the new key/value set in the + response's session map. ks should be a vector of keywords referring + to a nested value to set in the session. Any levels that do not exist + will be created." + [resp ks v] + (clojure.core/assoc-in resp (concat [:session] ks) v)) + +(defn update-in + "Returns an updated Ring response where a specific value within the + existing response's session map is 'updated' using function f which + takes the existing value along with any supplied args and should + return the new value. ks should be a vector of keywords referring + to a nested value to update. Any levels that do not exist will be + created." + [resp ks f & args] + (apply clojure.core/update-in resp (concat [:session] ks) f args)) + +(defn clear + [resp] + "Returns an updated Ring response which will cause the session to be + completely cleared." + (clojure.core/assoc resp :session nil))