archimag / restas

Common Lisp web framework
http://restas.lisper.ru/
Other
257 stars 50 forks source link

A way to set the render method of a module #19

Closed lispegistus closed 11 years ago

lispegistus commented 11 years ago

I need to be able to set the render method of a module from within the module, doing it in define-module or mount-module would be inconvenient. After 10 minutes with the source, I came up with this hack:

(defun set-render-method (method &optional (package *package*))
  (let ((traits (gethash (find-package package) restas::*pkgmodules-traits*)))
    (setf (gethash :render-method traits)
      (alexandria:named-lambda make-render-method () method)))
  (restas:reconnect-all-routes))

Is there a better way? I could implement something better and send a pull request, but I don't quite understand the implementation of the new module system yet.

archimag commented 11 years ago

I don't understand why such an opportunity is needed for a wide range of applications. I think it's pretty is a rare need.

Maybe something like:

(defclass proxy-renderer ()
  ((target :initarg target :accessor proxy-renderer-tardet)))

(defmethod restas:render-object ((proxy proxy-renderer) obj)
  (render-object (proxy-renderer-tardet proxy) obj))

(defparamater *mutable-renderer*
    (make-instance 'proxy-renderer :target #'my-fist-render-method))

(restas:mount-module -my-module- (#:mymodule)
  (:render-method *mutable-renderer*))

(setf (proxy-renderer-tardet *mutable-renderer*)
      #'my-second-render-method)

suit you?

lispegistus commented 11 years ago

Now I have another problem. The render-method keyword argument to restas:start doesn't seem to work. I get a simple-error: "Unknown as render (TITLE Hello BODY World) via NIL".

(gethash :render-method (gethash (find-package '#:blogdemo) restas::*pkgmodules-traits*))

returns nil

archimag commented 11 years ago

Oh, I fixed this a long time, but forgot make commit. Fixed.