-
Notifications
You must be signed in to change notification settings - Fork 16
/
culled-info.lyx
390 lines (264 loc) · 6.88 KB
/
culled-info.lyx
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#LyX 1.6.1 created this file. For more info see http://www.lyx.org/
\lyxformat 345
\begin_document
\begin_header
\textclass article
\use_default_options true
\language english
\inputencoding auto
\font_roman default
\font_sans default
\font_typewriter default
\font_default_family default
\font_sc false
\font_osf false
\font_sf_scale 100
\font_tt_scale 100
\graphics default
\paperfontsize default
\use_hyperref false
\papersize default
\use_geometry false
\use_amsmath 1
\use_esint 1
\cite_engine basic
\use_bibtopic false
\paperorientation portrait
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\defskip medskip
\quotes_language english
\papercolumns 1
\papersides 1
\paperpagestyle default
\tracking_changes false
\output_changes false
\author ""
\end_header
\begin_body
\begin_layout Standard
This document holds some of the sections that we've removed from the book
so that they don't just disappear into the ether...
\end_layout
\begin_layout Section
Defining Custom Field Types in Record
\begin_inset CommandInset label
LatexCommand label
name "sub:Defining-Custom-Field-types-record"
\end_inset
\end_layout
\begin_layout Standard
Similar to the example in section
\begin_inset CommandInset ref
LatexCommand ref
reference "sub:Defining-Custom-Field-types-mapper"
\end_inset
, when using Record we would like a DecimalField to represent decimal currency
amounts.
Our implementation starts off looking very similar to the Mapper version,
as shown in listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:DecimalField-constructors"
\end_inset
.
The main difference so far is that the owner type has to be a subtype of
Record instead of Mapper.
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
DecimalField Constructors
\begin_inset CommandInset label
LatexCommand label
name "lst:DecimalField-constructors"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
class DecimalField[OwnerType <: Record[OwnerType]]
\end_layout
\begin_layout Plain Layout
(rec : OwnerType, scale : Int)
\end_layout
\begin_layout Plain Layout
extends Field[BigDecimal,OwnerType] {
\end_layout
\begin_layout Plain Layout
def this(rec : OwnerType, newVal : BigDecimal) = {
\end_layout
\begin_layout Plain Layout
this(rec, newVal.scale)
\end_layout
\begin_layout Plain Layout
set(newVal)
\end_layout
\begin_layout Plain Layout
}
\end_layout
\begin_layout Plain Layout
var rounding = RoundingMode.HALF_EVEN
\end_layout
\end_inset
\end_layout
\begin_layout Standard
The next step is to start defining the methods that are left abstract on
Field.
Our first set deals with some basic housekeeping and presentation layer
details, shown in listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:DecimalField-Methods"
\end_inset
.
The defaultValue method simply defines the value that we use for new instances.
The owner method lets Record know which Record instance owns this field.
asXHtml and toForm define the methods for display and form generation,
respectively.
We use the setFromString method (defined later in this section) to handle
form setting, which keeps the logic clean.
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
DecimalField Methods
\begin_inset CommandInset label
LatexCommand label
name "lst:DecimalField-Methods"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
def defaultValue = (new BigDecimal("0")).setScale(scale)
\end_layout
\begin_layout Plain Layout
def owner = rec
\end_layout
\begin_layout Plain Layout
def asXHtml = Text(value.toString)
\end_layout
\begin_layout Plain Layout
def toForm = text(value.toString, this.setFromString)
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Our final set of methods deals with setting the value of the field, shown
in listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:DecimalField-Set-Methods"
\end_inset
.
The setFromAny method, and its counterpart setFromString, have replaced
the buildSet...
method functionality that we would define in Mapper
\begin_inset Note Note
status open
\begin_layout Plain Layout
Is that correct?
\end_layout
\end_inset
.
setFromAny essentially defers to setFromString for all operations, and
the only special handling we need is for Lists, Options and Boxes.
setFromString is required to catch any exceptions thrown during conversion;
if we have a failure we need to return an Empty as well as calling couldNotSetV
alue to set a flag for validation.
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
DecimalField Set Methods
\begin_inset CommandInset label
LatexCommand label
name "lst:DecimalField-Set-Methods"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
def setFromAny (in : Any) : Box[BigDecimal] =
\end_layout
\begin_layout Plain Layout
in match {
\end_layout
\begin_layout Plain Layout
case n :: _ => setFromString(n.toString)
\end_layout
\begin_layout Plain Layout
case Some(n) => setFromString(n.toString)
\end_layout
\begin_layout Plain Layout
case Full(n) => setFromString(n.toString)
\end_layout
\begin_layout Plain Layout
case None | Empty | Failure(_, _, _) | null => setFromString("0")
\end_layout
\begin_layout Plain Layout
case n => setFromString(n.toString)
\end_layout
\begin_layout Plain Layout
}
\end_layout
\begin_layout Plain Layout
def setFromString (in : String) : Box[BigDecimal] = {
\end_layout
\begin_layout Plain Layout
try {
\end_layout
\begin_layout Plain Layout
Full(this.setAll(new BigDecimal(in)))
\end_layout
\begin_layout Plain Layout
} catch {
\end_layout
\begin_layout Plain Layout
case e : Exception => couldNotSetValue; Empty
\end_layout
\begin_layout Plain Layout
}
\end_layout
\begin_layout Plain Layout
}
\end_layout
\begin_layout Plain Layout
/** Set the value along with proper scale and rounding */
\end_layout
\begin_layout Plain Layout
protected def setAll (in : BigDecimal) = set(in.setScale(scale,rounding))
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Since BigDecimal takes a string as a constructor (in fact, this is the recommend
ed way to initialize it to avoid precision issues with numerics), the setFromStr
ing method is relatively easy to implement.
To make things even simpler, we add the setAll method that takes a BigDecimal
as input and then sets the proper scale and rounding on it.
\end_layout
\begin_layout Standard
\begin_inset Note Note
status open
\begin_layout Plain Layout
Confirm change in behavior from Mapper to not save the original value
\end_layout
\end_inset
\end_layout
\end_body
\end_document