Created
July 28, 2011 03:07
-
-
Save jcromartie/1110894 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(defn timestamp | |
"Returns object timestamped with the current system time in milliseconds" | |
[obj millis] | |
(assoc obj :timestamp (System/currentTimeMillis))) | |
(defn new-model-object | |
"Returns new, uniquely identified, timestamped object with initial | |
key-val pairs" | |
[& key-vals] | |
(timestamp (merge (apply hash-map kvs) | |
{:uuid (uuid)}))) | |
(defn revisioned-update | |
"Returns object with old state appended to value at :revisions key, | |
and new, timestamped state, after applying (f obj & args) to the | |
current state." | |
[obj f & args] | |
(let [obj1 (update-in obj [:revisions] conj obj) | |
updated (apply f obj1 args)] | |
(timestamp updated))) | |
(defn history | |
"Returns all states of model object, oldest first" | |
[obj] | |
(sort-by :timestamp | |
(conj (:revisions obj) | |
(dissoc obj :revisions)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment