-
Notifications
You must be signed in to change notification settings - Fork 0
/
implicit.clj
69 lines (56 loc) · 1.59 KB
/
implicit.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
(ns stowaway.implicit
(:refer-clojure :exclude [update])
(:require [stowaway.core :as core]))
(def ^:dynamic *storage*)
(defn unbound?
[value]
(instance? clojure.lang.Var$Unbound value))
(defn bound-val
[value]
(when-not (unbound? value)
value))
(defmacro with-storage
"Binds a reified storage stratey to *storage*
(with-storage config
(update my-model))"
[config & body]
`(binding [*storage* (core/reify-storage
(or (bound-val *storage*)
~config))]
~@body))
(defmacro with-transacted-storage
"Binds a transaction strategy to *storage*
(with-transacted-storage config
(update my-first-model)
(update my-other-model))"
[config & body]
`(let [s# (core/reify-storage (or (bound-val *storage*)
~config))]
(core/with-transaction s# #(binding [*storage* %]
~@body))))
(defn- assert-storage-present []
(when (unbound? *storage*)
(throw (RuntimeException. "No storage strategy has been specified."))))
(defn create
[model]
(assert-storage-present)
(core/create *storage* model))
(defn select
[criteria options]
(assert-storage-present)
(core/select *storage* criteria options))
(defn update
([model]
(assert-storage-present)
(core/update *storage* model))
([attr criteria]
(assert-storage-present)
(core/update *storage* attr criteria)))
(defn delete
[model]
(assert-storage-present)
(core/delete *storage* model))
(defn delete-by
[model]
(assert-storage-present)
(core/delete-by *storage* model))