Skip to content

Commit

Permalink
Make compatible with new clutch put-document keyword semantics. Also …
Browse files Browse the repository at this point in the history
…work around clutch returning database status instead of nil if key is empty.
  • Loading branch information
kenrestivo committed May 24, 2012
1 parent d2e70cf commit 145e5b7
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/couch_session/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,31 @@
ring.middleware.session.store)
(:require [com.ashafa.clutch :as c]))

(defn clutch-hack
"Workaround for clutch, which returns a id-less document with database status
when you do (get-document nil)."
[key]
(if (empty? key) nil (c/get-document key)))

(deftype CouchStore [db]
SessionStore
(read-session [_ session-key]
(-> (c/with-db db (c/get-document session-key))
(-> (c/with-db db (clutch-hack session-key))
(decode)))

(write-session [_ session-key data]
(c/with-db db
(:_id (let [doc (c/get-document session-key)
(:_id (let [doc (clutch-hack session-key)
data (-> data
(encode)
(merge (select-keys doc [:_id :_rev])))]
(cond (not doc) (c/put-document data session-key)
(cond (not doc) (c/put-document data :id session-key)
(or (= data {}) (= data doc)) doc
:else (c/put-document data))))))

(delete-session [_ session-key]
(c/with-db db
(when-let [doc (c/get-document session-key)]
(when-let [doc (clutch-hack session-key)]
(c/delete-document doc)))
nil))

Expand Down

0 comments on commit 145e5b7

Please sign in to comment.