(do-with-temp* model explicit-attributes f)
Implementation of with-temp
. You can implement this if you need to do some sort of special behavior for a
particular model. But normally you would just implement with-temp-defaults
. If you need to do special setup when
using with-temp
, you can implement a :before
method:
(m/defmethod do-with-temp* :before :default
[model attributes f]
(set-up-db!)
f)
explicit-attributes
are the attributes specified in the with-temp
form itself, any may be nil
. The default
implementation merges the attributes from with-temp-defaults
like
(merge {} (with-temp-defaults model) explict-attributes)
Implementation of [[with-temp]]. You can implement this if you need to do some sort of special behavior for a particular model. But normally you would just implement [[with-temp-defaults]]. If you need to do special setup when using [[with-temp]], you can implement a `:before` method: ```clj (m/defmethod do-with-temp* :before :default [model attributes f] (set-up-db!) f) ``` `explicit-attributes` are the attributes specified in the `with-temp` form itself, any may be `nil`. The default implementation merges the attributes from [[with-temp-defaults]] like ```clj (merge {} (with-temp-defaults model) explict-attributes) ```
(with-temp [modelable temp-object-binding attributes & more] & body)
Define a temporary instance of a model and bind it to temp-object-binding
. The object is inserted
using [[insert/insert-returning-instances!]] using the values from with-temp-defaults
merged with a map of
attributes
. At the conclusion of body
, the object is deleted. This is primarily intended for usage in tests, so
this adds a [[clojure.test/testing]] context around body
as well.
with-temp
can create multiple objects in one form if you pass additional bindings.
temp-object-binding
and attributes
are optional, and default to _
and nil
, respectively. If you're creating
multiple objects at once these must be explicitly specified.
Examples:
;;; use the with-temp-defaults for :models/bird
(with-temp [:models/bird bird]
(do-something bird))
;;; use the with-temp-defaults for :models/bird merged with {:name "Lucky Pigeon"}
(with-temp [:models/bird bird {:name "Lucky Pigeon"}]
(do-something bird))
;;; define multiple instances at the same time
(with-temp [:models/bird bird-1 {:name "Parroty"}
:models/bird bird-2 {:name "Green Friend", :best-friend-id (:id bird-1)}]
(do-something bird))
If you want to implement custom behavior for a model other than default values, you can implement do-with-temp*
.
Define a temporary instance of a model and bind it to `temp-object-binding`. The object is inserted using [[insert/insert-returning-instances!]] using the values from [[with-temp-defaults]] merged with a map of `attributes`. At the conclusion of `body`, the object is deleted. This is primarily intended for usage in tests, so this adds a [[clojure.test/testing]] context around `body` as well. [[with-temp]] can create multiple objects in one form if you pass additional bindings. `temp-object-binding` and `attributes` are optional, and default to `_` and `nil`, respectively. If you're creating multiple objects at once these must be explicitly specified. Examples: ```clj ;;; use the with-temp-defaults for :models/bird (with-temp [:models/bird bird] (do-something bird)) ;;; use the with-temp-defaults for :models/bird merged with {:name "Lucky Pigeon"} (with-temp [:models/bird bird {:name "Lucky Pigeon"}] (do-something bird)) ;;; define multiple instances at the same time (with-temp [:models/bird bird-1 {:name "Parroty"} :models/bird bird-2 {:name "Green Friend", :best-friend-id (:id bird-1)}] (do-something bird)) ``` If you want to implement custom behavior for a model other than default values, you can implement [[do-with-temp*]].
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close