Skip to content

Instantly share code, notes, and snippets.

@jcromartie
Created July 28, 2011 03:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcromartie/1110894 to your computer and use it in GitHub Desktop.
Save jcromartie/1110894 to your computer and use it in GitHub Desktop.
(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