git.fiddlerwoaroof.com
Browse code

chore: add mixin to automate route registration

Edward authored on 18/04/2021 00:00:36
Showing 2 changed files
... ...
@@ -2,6 +2,9 @@
2 2
 ;; views from controllers and make life more interesting.
3 3
 (in-package :araneus)
4 4
 
5
+(defclass mixin ()
6
+  ())
7
+
5 8
 (defgeneric run-route (name params &rest r)
6 9
   (:documentation "specialized on NAME with an EQL-specializer. This generic
7 10
                    function defines the way a specific route is to be processed"))
... ...
@@ -32,6 +35,8 @@
32 35
   (:method :around (app)
33 36
     (call-next-method)
34 37
     app))
38
+(defmethod initialize-instance :after ((instance mixin) &key)
39
+  (araneus:routes instance))
35 40
 
36 41
 (defmacro defroutes (app &body routes)
37 42
   "Define a set of routes for given paths. the ROUTES parameter expects this format:
... ...
@@ -45,7 +50,7 @@
45 50
 (defvar *view-name*)
46 51
 (defun call-current-view (model)
47 52
   "Call the currently running view with a new model.
48
-   
53
+
49 54
    Useful if one view name is specialized many times on different model classes: the controller can
50 55
    pass the container and then the view can recall itself on the contained items."
51 56
   (view *view-name* model))
... ...
@@ -136,6 +141,4 @@
136 141
     (alexandria:with-gensyms (model)
137 142
       `(define-view ,name (,model)
138 143
          (mustache (,template ,lambda-list ,model)
139
-                   ,@body)))))
140
-
141
-
144
+           ,@body)))))
... ...
@@ -6,13 +6,14 @@
6 6
            #:define-view           #:controller      #:view
7 7
            #:run-route             #:mustache-view   #:render-mustache
8 8
            #:define-spinneret-view #:setf1           #:call-current-view
9
-           #:routes #:styles))
9
+           #:routes #:styles
10
+           #:mixin))
10 11
 
11 12
 #+fw.dev
12 13
 (defpackage #:araneus-2
13 14
   (:use #:cl #:fw.lu)
14
-  (:export #:defroutes  #:as-route  #:define-controller #:define-view    
15
-           #:controller #:view      #:run-route #:mustache-view    
15
+  (:export #:defroutes  #:as-route  #:define-controller #:define-view
16
+           #:controller #:view      #:run-route #:mustache-view
16 17
            #:render-mustache        #:define-spinneret-view
17 18
            #:setf1      #:call-current-view
18 19
            #:switch-view))
... ...
@@ -24,5 +25,3 @@
24 25
 (defpackage #:araneus.form
25 26
   (:use #:cl)
26 27
   (:export ))
27
-
28
-