-
Notifications
You must be signed in to change notification settings - Fork 3
/
command.dylan
210 lines (179 loc) · 5.56 KB
/
command.dylan
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
module: change
author: Hannes Mehnert <hannes@mehnert.org>
define abstract class <command> (<object>)
constant slot arguments :: <list>, init-keyword: arguments:;
end;
define class <add-command> (<command>)
end;
define class <remove-command> (<command>)
end;
define class <edit-command> (<command>)
end;
define method reverse-command (c :: <add-command>)
make(<remove-command>, arguments: c.arguments);
end;
define method reverse-command (c :: <remove-command>)
make(<add-command>, arguments: c.arguments);
end;
define method reverse-command (c :: <edit-command>)
make(<edit-command>,
arguments: list(c.arguments[0],
map(reverse-triple, c.arguments[1])));
end;
define method reverse-triple (t :: <triple>)
make(<triple>,
slot-name: t.slot-name,
old-value: t.new-value,
new-value: t.old-value);
end;
define method execute (command :: <add-command>)
add-to-list;
end;
define method unexecute (command :: <add-command>)
remove-from-list;
end;
define method execute (command :: <remove-command>)
remove-from-list;
end;
define method unexecute (command :: <remove-command>)
add-to-list;
end;
define method execute (command :: <edit-command>)
set-slots;
end;
define method unexecute (command :: <edit-command>)
unset-slots;
end;
define method print-xml (command :: <command>, #key base-url)
let object = command.arguments[0];
let type = get-url-from-type(object.object-class);
with-xml()
a(concatenate(type, " ", show(object)),
href => concatenate(if (base-url) base-url else "" end, "/", type, "-detail?", type, "=", get-reference(object)))
end;
end;
define method print-change (command :: <command>, #key base-url) => (res :: <string>)
let object = command.arguments[0];
let type = get-url-from-type(object.object-class);
concatenate(type, " ", show(object), " ",
if (base-url) base-url else "" end,
"/", type, "-detail?", type, "=", get-reference(object))
end;
define method print-xml (command :: <add-command>, #key base-url)
list(with-xml()
text("Added ")
end,
next-method());
end;
define method print-change (command :: <add-command>, #key base-url)
concatenate("Added ", next-method());
end;
define method print-xml (command :: <remove-command>, #key base-url)
list(with-xml()
text("Removed ")
end,
next-method())
end;
define method print-change (command :: <remove-command>, #key base-url)
concatenate("Removed ", next-method());
end;
define method print-xml (command :: <edit-command>, #key base-url)
list(with-xml()
text("Edited ")
end,
next-method(),
with-xml()
text(", changed following slots: ")
end,
with-xml()
ul {
do(map(print-xml, command.arguments[1]))
}
end)
end;
define method print-change (command :: <edit-command>, #key base-url)
apply(concatenate, "Edited ", next-method(), ", changed following slots:\n",
map(print-change, command.arguments[1]));
end;
define method print-xml (triple :: <triple>, #key base-url)
with-xml()
li { text(concatenate(triple.slot-name,
" from \"", show(triple.old-value),
"\" to \"", show(triple.new-value), "\""))
}
end;
end;
define method print-change (triple :: <triple>, #key base-url)
concatenate(triple.slot-name, " from \"", show(triple.old-value),
"\" to \"", show(triple.new-value), "\"\n");
end;
define method add-to-list (object :: <object>, list :: <collection>)
//only add if not in list
unless (any?(method(x) x = object end, list))
if (check(object))
list := sort!(add!(list, object))
end;
end
end;
define method remove-from-list (object :: <object>, list :: <collection>)
list := remove!(list, object);
end;
define method add-to-list (object :: <object>, table :: <table>)
if (check(object))
table[key(object)] := object;
end;
end;
define method remove-from-list (object :: <object>, list :: <table>)
remove-key!(list, key(object));
end;
define method set-slots (object :: <object>, slots :: <list>)
map(method(x)
set-slot(x.slot-name, object, x.new-value)
end, slots);
let handler <web-error>
= method(e :: <web-error>, next-handler :: <function>)
unset-slots(object, slots);
next-handler();
end;
check(object, test-result: 1);
//check for consistency, on error, do a rollback
end;
define method unset-slots (object :: <object>, slots :: <list>)
map(method(x)
set-slot(x.slot-name, object, x.old-value)
end, slots);
let handler <web-error>
= method(e :: <web-error>, next-handler :: <function>)
set-slots(object, slots);
next-handler();
end;
check(object, test-result: 1);
//check for consistency, on error, do a rollback
end;
define method set-slot (name :: <string>,
object :: <object>,
value :: <object>)
local method find-slot (slots)
block(return)
for (slot in slots)
if (slot.slot-name = name)
return(slot);
end;
end;
#f;
end;
end;
let slot = find-slot(data-slots(object.object-class));
unless (slot)
slot := find-slot(reference-slots(object.object-class));
end;
if (slot)
slot.slot-setter-method(value, object)
end;
end;
define method undo (command)
apply(unexecute(command), command.arguments);
end;
define method redo (command)
apply(execute(command), command.arguments);
end;