Created
December 21, 2011 03:35
-
-
Save budu/1504442 to your computer and use it in GitHub Desktop.
A factory defining macro.
This file contains 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
(defmacro deffactory | |
"Defines a factory function that returns an instance of the specified | |
record initialized with the default values provided overridden by the | |
values it is given." | |
[factory-name record-name defaults] | |
`(defn ~factory-name | |
~(str "A factory function returning a new instance of " record-name | |
" initialized with the defaults specified at the time of" | |
" definition overridden by the given values. The values can" | |
" be specified as a map or field-value pairs.") | |
([] (~factory-name {})) | |
([~'field-value-map] | |
(merge (new ~record-name ~@defaults) | |
~'field-value-map)) | |
([~'field ~'value & ~'field-value-pairs] | |
(~factory-name (apply hash-map ~'field ~'value | |
~'field-value-pairs))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment