Browse code
Updating README, documenting functions and macros
- Minor change to the mustache-view macro to ensure the proper order of
evaluation.
Showing 4 changed files
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,21 @@ |
1 |
+The MIT License (MIT) |
|
2 |
+ |
|
3 |
+Copyright (c) 2016 Ed Langley |
|
4 |
+ |
|
5 |
+Permission is hereby granted, free of charge, to any person obtaining a copy |
|
6 |
+of this software and associated documentation files (the "Software"), to deal |
|
7 |
+in the Software without restriction, including without limitation the rights |
|
8 |
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
9 |
+copies of the Software, and to permit persons to whom the Software is |
|
10 |
+furnished to do so, subject to the following conditions: |
|
11 |
+ |
|
12 |
+The above copyright notice and this permission notice shall be included in all |
|
13 |
+copies or substantial portions of the Software. |
|
14 |
+ |
|
15 |
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
16 |
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
17 |
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
18 |
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
19 |
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
20 |
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
21 |
+SOFTWARE. |
... | ... |
@@ -2,15 +2,28 @@ |
2 | 2 |
;; views from controllers and make life more interesting. |
3 | 3 |
(in-package :araneus) |
4 | 4 |
|
5 |
-(defgeneric controller (name params &key)) |
|
6 |
-(defgeneric view (name model)) |
|
7 |
-(defgeneric run-route (name params &rest r)) |
|
5 |
+(defgeneric run-route (name params &rest r) |
|
6 |
+ (:documentation "specialized on NAME with an EQL-specializer. This generic |
|
7 |
+ function defines the way a specific route is to be processed")) |
|
8 |
+ |
|
9 |
+(defgeneric controller (name params &key) |
|
10 |
+ (:documentation "specialized on NAME with an EQL-specializer. This generic function |
|
11 |
+ picks out the model that the view renders for the user. Normally, |
|
12 |
+ this is specialized using the DEFINE-CONTROLLER macro.")) |
|
13 |
+ |
|
14 |
+(defgeneric view (name model) |
|
15 |
+ (:documentation "specialized on NAME with an EQL-specializer. This generic function |
|
16 |
+ renders the model picked out by the controller. Normally, this is |
|
17 |
+ specialized using the DEFINE-VIEW macr")) |
|
8 | 18 |
|
9 | 19 |
(defmacro setf1 (&body body) |
10 |
- "Make setf a bit nicer" |
|
20 |
+ "Make setf a bit nicer to use with paredit" |
|
11 | 21 |
(list* 'setf (apply #'append body))) |
12 | 22 |
|
13 | 23 |
(defmacro defroutes (app &body routes) |
24 |
+ "Define a set of routes for given paths. the ROUTES parameter expects this format: |
|
25 |
+ ((\"/path/to/{route}\" :method :POST) route-callback) the AS-ROUTE macro helps one |
|
26 |
+ avoid binding function values to the route for flexibility." |
|
14 | 27 |
(alexandria:once-only (app) |
15 | 28 |
(list* 'setf1 |
16 | 29 |
(loop for ((target &key method) callback) in routes |
... | ... |
@@ -18,12 +31,18 @@ |
18 | 31 |
|
19 | 32 |
|
20 | 33 |
(defmacro as-route (name &rest r &key &allow-other-keys) |
34 |
+ "Create a lambda directing requests to the route for NAME. This uses the |
|
35 |
+ generic function RUN-ROUTE internally whose default implementation relies on |
|
36 |
+ appropriate implementations of CONTROLLER and VIEW. The RUN-ROUTE method receives |
|
37 |
+ the parameters ningle passes to other functions as a first parameter, and then it |
|
38 |
+ receives a bunch of arguments according to the arguments passed to this macro." |
|
21 | 39 |
`(lambda (params) (run-route ,name params ,@r))) |
22 | 40 |
|
23 | 41 |
|
24 | 42 |
(defmethod run-route (name params &rest r) |
25 | 43 |
(view name (apply #'controller (list* name params r)))) |
26 | 44 |
|
45 |
+; The default controller just passes its parameters directly to the view |
|
27 | 46 |
(defmethod controller (name params &key) |
28 | 47 |
params) |
29 | 48 |
|
... | ... |
@@ -54,8 +73,10 @@ |
54 | 73 |
collect `(cons ,k ,v))))))) |
55 | 74 |
|
56 | 75 |
(defmacro mustache-view (name lambda-list template &body body) |
57 |
- (alexandria:with-gensyms (model) |
|
58 |
- `(define-view ,name (,model) |
|
59 |
- (mustache (,template ,lambda-list ,model) |
|
60 |
- ,@body)))) |
|
76 |
+ "Define a view that renders a mustache template." |
|
77 |
+ (alexandria:once-only (name lambda-list) |
|
78 |
+ (alexandria:with-gensyms (model) |
|
79 |
+ `(define-view ,name (,model) |
|
80 |
+ (mustache (,template ,lambda-list ,model) |
|
81 |
+ ,@body))))) |
|
61 | 82 |
|