Skip to content

Instantly share code, notes, and snippets.

@slyphon
Forked from michalmarczyk/update-bean.clj
Created April 23, 2010 03:00
Show Gist options
  • Save slyphon/376119 to your computer and use it in GitHub Desktop.
Save slyphon/376119 to your computer and use it in GitHub Desktop.
(use '[clojure.contrib [str-utils :only (re-gsub)]])
(use '[clojure.contrib [java-utils :only (as-str)]])
(defn #^String upcase
"upcases the initial character in s"
[s]
(let [string (as-str s)]
(apply str (Character/toUpperCase (first string)) (rest string))))
(defn #^String camelize [s]
(re-gsub #"(?:^|_|-)(.)" (fn [[_ ch & rest]] (.toUpperCase ch)) (as-str s)))
(defmacro update-bean
([obj props-map]
(let [props (cond (map? props-map) (seq props-map)
(get &env props-map) (seq (&env props-map))
:else (seq (eval props-map)))
ks (map (comp symbol #(str "set" (-> (as-str %) camelize upcase)) first) props)
vs (map second props)
dots (map (fn [k v] `(. ~k ~v)) ks vs)]
`(doto ~obj ~@dots))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment