Skip to content

Instantly share code, notes, and snippets.

@budu
Created December 21, 2011 03:35
Show Gist options
  • Save budu/1504442 to your computer and use it in GitHub Desktop.
Save budu/1504442 to your computer and use it in GitHub Desktop.
A factory defining macro.
(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