Want more features on Pastebin? Sign Up, it's FREE!
Guest

macr eval problem

By: a guest on Oct 2nd, 2012  |  syntax: Lisp  |  size: 1.19 KB  |  views: 37  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. (defclass db-object () ())
  2.  
  3. (defun slot-list-from-query (query)
  4.   (format t "slot-list-from-query: ~s~&" query)
  5.   (mapcar
  6.    #'(lambda (pair)
  7.        (format t "pair: ~s~&" pair)
  8.        (let ((name (string-upcase (symbol-name (car pair)))))
  9.          (list (intern name)
  10.                :accessor
  11.                (intern (concatenate 'string name "-OF"))
  12.                :initarg (intern name "KEYWORD")))) query))
  13.  
  14. (defun make-class-with-slots (name slots)
  15.   (format t "defining class: ~s, slots: ~s~&" name slots)
  16.   `(defclass ,name (db-object) ,slots))
  17.  
  18. (defmacro table-to-class (name describe-table-query)
  19.   `(let ((db-class (make-class-with-slots
  20.                     ',name
  21.                     (slot-list-from-query ,describe-table-query))))
  22.      (eval db-class)
  23.      (defmethod initialize-instance :after ((object ,name) &key raw-data)
  24.                 (mapcar
  25.                  #'(lambda (pair)
  26.                      (setf (slot-value object
  27.                                        (find-symbol
  28.                                         (string-upcase
  29.                                          (symbol-name (car pair)))))
  30.                            (cdr pair))) raw-data))
  31.      db-class))
clone this paste RAW Paste Data