dynarex 1.2.97 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/lib/dynarex.rb +1045 -1040
- metadata +18 -18
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d35a52b2f8e685ee91957c366b83bbb7bf05b237
|
4
|
+
data.tar.gz: 3ccf814b746c54b613568cd2f9d16322df70fd3c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 03edaa4dbc3987611526bb9c37dc4b86db88dc58afdd0eb63901aecf95af3dbc750fed94b2f5057a0058ec47eab70e81e1e3e625226657f08a74bbf4c54d2570
|
7
|
+
data.tar.gz: f429218c0a3ba6f1e80f013b0d1b7cf093b608020c456ad56cb97e1bfa6d511ec97f26521f1c39556ab8ad4f398957eca4ce3387d07c3cb3c5d072a2743d5ef2
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data.tar.gz.sig
CHANGED
Binary file
|
data/lib/dynarex.rb
CHANGED
@@ -1,1043 +1,1048 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
# file: dynarex.rb
|
4
|
-
|
5
|
-
require 'open-uri'
|
6
|
-
require 'dynarex-import'
|
7
|
-
require 'line-tree'
|
8
|
-
require 'rexle'
|
9
|
-
require 'rexle-builder'
|
10
|
-
require 'rexslt'
|
11
|
-
require 'dynarex-xslt'
|
12
|
-
require 'recordx'
|
13
|
-
require 'rxraw-lineparser'
|
14
|
-
require 'yaml'
|
15
|
-
require 'rowx'
|
16
|
-
require 'nokogiri'
|
17
|
-
require 'ostruct'
|
18
|
-
require 'table-formatter'
|
19
|
-
|
20
|
-
|
21
|
-
class
|
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
|
-
end
|
55
|
-
|
56
|
-
def
|
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
|
-
end
|
82
|
-
|
83
|
-
def
|
84
|
-
@
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
end
|
97
|
-
|
98
|
-
def
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
end
|
112
|
-
|
113
|
-
def
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
@
|
121
|
-
end
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
@records
|
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
|
-
def
|
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
|
-
end
|
366
|
-
|
367
|
-
def
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
end
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
def
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
end
|
517
|
-
|
518
|
-
def
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
(
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
end
|
551
|
-
|
552
|
-
def
|
553
|
-
|
554
|
-
end
|
555
|
-
|
556
|
-
def
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
buffer.gsub!(
|
613
|
-
buffer.gsub!(
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
#
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
#
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# file: dynarex.rb
|
4
|
+
|
5
|
+
require 'open-uri'
|
6
|
+
require 'dynarex-import'
|
7
|
+
require 'line-tree'
|
8
|
+
require 'rexle'
|
9
|
+
require 'rexle-builder'
|
10
|
+
require 'rexslt'
|
11
|
+
require 'dynarex-xslt'
|
12
|
+
require 'recordx'
|
13
|
+
require 'rxraw-lineparser'
|
14
|
+
require 'yaml'
|
15
|
+
require 'rowx'
|
16
|
+
require 'nokogiri'
|
17
|
+
require 'ostruct'
|
18
|
+
require 'table-formatter'
|
19
|
+
|
20
|
+
|
21
|
+
class DynarexException < Exception
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
class Dynarex
|
26
|
+
|
27
|
+
attr_accessor :format_mask, :delimiter, :xslt_schema, :schema,
|
28
|
+
:order, :type, :limit_by, :xslt
|
29
|
+
|
30
|
+
|
31
|
+
#Create a new dynarex document from 1 of the following options:
|
32
|
+
#* a local file path
|
33
|
+
#* a URL
|
34
|
+
#* a schema string
|
35
|
+
# Dynarex.new 'contacts[title,description]/contact(name,age,dob)'
|
36
|
+
#* an XML string
|
37
|
+
# Dynarex.new '<contacts><summary><schema>contacts/contact(name,age,dob)</schema></summary><records/></contacts>'
|
38
|
+
|
39
|
+
def initialize(rawx=nil)
|
40
|
+
#puts Rexle.version
|
41
|
+
@delimiter = ''
|
42
|
+
openx(rawx.clone) if rawx
|
43
|
+
if @order == 'descending' then
|
44
|
+
@records = records_to_h(:descending)
|
45
|
+
rebuild_doc
|
46
|
+
end
|
47
|
+
#jr240913 @dirty_flag = false
|
48
|
+
end
|
49
|
+
|
50
|
+
def add(x)
|
51
|
+
@doc.root.add x
|
52
|
+
@dirty_flag = true
|
53
|
+
self
|
54
|
+
end
|
55
|
+
|
56
|
+
def all()
|
57
|
+
@doc.root.xpath("records/*").map {|x| recordx_to_record x}
|
58
|
+
end
|
59
|
+
|
60
|
+
def delimiter=(separator)
|
61
|
+
|
62
|
+
@delimiter = separator
|
63
|
+
|
64
|
+
if separator.length > 0 then
|
65
|
+
@summary[:delimiter] = separator
|
66
|
+
else
|
67
|
+
@summary.delete :delimiter
|
68
|
+
end
|
69
|
+
|
70
|
+
@format_mask = @format_mask.to_s.gsub(/\s/, separator)
|
71
|
+
@summary[:format_mask] = @format_mask
|
72
|
+
end
|
73
|
+
|
74
|
+
def foreign_import(options={})
|
75
|
+
o = {xml: '', schema: ''}.merge(options)
|
76
|
+
h = {xml: o[:xml], schema: @schema, foreign_schema: o[:schema]}
|
77
|
+
buffer = DynarexImport.new(h).to_xml
|
78
|
+
|
79
|
+
openx(buffer)
|
80
|
+
self
|
81
|
+
end
|
82
|
+
|
83
|
+
def fields
|
84
|
+
@fields
|
85
|
+
end
|
86
|
+
|
87
|
+
def format_mask=(s)
|
88
|
+
@format_mask = s
|
89
|
+
@summary[:format_mask] = @format_mask
|
90
|
+
end
|
91
|
+
|
92
|
+
def insert(raw_params)
|
93
|
+
record = make_record(raw_params)
|
94
|
+
@doc.root.element('records/*').insert_before record
|
95
|
+
@dirty_flag = true
|
96
|
+
end
|
97
|
+
|
98
|
+
def inspect()
|
99
|
+
"<object #%s>" % [self.object_id]
|
100
|
+
end
|
101
|
+
|
102
|
+
def order=(value)
|
103
|
+
|
104
|
+
self.summary.merge!({order: value})
|
105
|
+
if @order == 'ascending' and value == 'descending' then
|
106
|
+
sort_records
|
107
|
+
elsif @order == 'descending' and value == 'ascending'
|
108
|
+
sort_records
|
109
|
+
end
|
110
|
+
@order = value
|
111
|
+
end
|
112
|
+
|
113
|
+
def schema=(s)
|
114
|
+
openx s
|
115
|
+
end
|
116
|
+
|
117
|
+
def type=(v)
|
118
|
+
@order = 'descending' if v == 'feed'
|
119
|
+
@type = v
|
120
|
+
@summary[:type] = v
|
121
|
+
end
|
122
|
+
|
123
|
+
def limit_by=(val)
|
124
|
+
@limit_by = val.to_i
|
125
|
+
end
|
126
|
+
|
127
|
+
# Returns the hash representation of the document summary.
|
128
|
+
#
|
129
|
+
def summary
|
130
|
+
@summary
|
131
|
+
end
|
132
|
+
|
133
|
+
# Return a Hash (which can be edited) containing all records.
|
134
|
+
#
|
135
|
+
def records
|
136
|
+
|
137
|
+
load_records if @dirty_flag == true
|
138
|
+
|
139
|
+
if block_given? then
|
140
|
+
yield(@records)
|
141
|
+
rebuild_doc
|
142
|
+
@dirty_flag = true
|
143
|
+
else
|
144
|
+
@records
|
145
|
+
end
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
# Returns a ready-only snapshot of records as a simple Hash.
|
150
|
+
#
|
151
|
+
def flat_records
|
152
|
+
load_records if @dirty_flag == true
|
153
|
+
@flat_records
|
154
|
+
end
|
155
|
+
|
156
|
+
alias to_h flat_records
|
157
|
+
alias to_a flat_records
|
158
|
+
|
159
|
+
# Returns an array snapshot of OpenStruct records
|
160
|
+
#
|
161
|
+
def ro_records
|
162
|
+
flat_records.map {|record| OpenStruct.new record }
|
163
|
+
end
|
164
|
+
|
165
|
+
# Returns all records as a string format specified by the summary format_mask field.
|
166
|
+
|
167
|
+
def to_doc
|
168
|
+
@doc
|
169
|
+
end
|
170
|
+
|
171
|
+
def to_s
|
172
|
+
|
173
|
+
xsl_buffer =<<EOF
|
174
|
+
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
|
175
|
+
<xsl:output encoding="UTF-8"
|
176
|
+
method="text"
|
177
|
+
indent="no"
|
178
|
+
omit-xml-declaration="yes"/>
|
179
|
+
|
180
|
+
<xsl:template match="*">
|
181
|
+
<xsl:for-each select="records/*">[!regex_values]<xsl:text>
|
182
|
+
</xsl:text>
|
183
|
+
</xsl:for-each>
|
184
|
+
</xsl:template>
|
185
|
+
</xsl:stylesheet>
|
186
|
+
EOF
|
187
|
+
|
188
|
+
|
189
|
+
#format_mask = XPath.first(@doc.root, 'summary/format_mask/text()').to_s
|
190
|
+
#format_mask = @doc.root.element('summary/format_mask/text()')
|
191
|
+
|
192
|
+
raw_summary_fields = self.summary[:schema][/^\w+\[([^\]]+)\]/,1]
|
193
|
+
sumry = ''
|
194
|
+
|
195
|
+
if raw_summary_fields then
|
196
|
+
summary_fields = raw_summary_fields.split(',') # .map(&:to_sym)
|
197
|
+
sumry = summary_fields.map {|x| x.strip!; x + ': ' + \
|
198
|
+
self.summary[x.to_sym]}.join("\n") + "\n"
|
199
|
+
end
|
200
|
+
|
201
|
+
if @raw_header then
|
202
|
+
declaration = @raw_header
|
203
|
+
else
|
204
|
+
|
205
|
+
smry_fields = %i(schema)
|
206
|
+
if self.delimiter.length > 0 then
|
207
|
+
smry_fields << :delimiter
|
208
|
+
else
|
209
|
+
smry_fields << :format_mask
|
210
|
+
end
|
211
|
+
s = smry_fields.map {|x| "%s=\"%s\"" % \
|
212
|
+
[x, self.send(x).gsub('"', '\"') ]}.join ' '
|
213
|
+
#declaration = "<?dynarex %s ?>" % s
|
214
|
+
declaration = %Q(<?dynarex %s?>\n) % s
|
215
|
+
end
|
216
|
+
|
217
|
+
header = declaration + sumry
|
218
|
+
|
219
|
+
if self.summary[:rawdoc_type] == 'rowx' then
|
220
|
+
a = self.fields.map do |field|
|
221
|
+
"<xsl:if test=\"%s != ''\">
|
222
|
+
<xsl:text>\n</xsl:text>%s: <xsl:value-of select='%s'/>
|
223
|
+
</xsl:if>" % ([field]*3)
|
224
|
+
end
|
225
|
+
|
226
|
+
xslt_format = a.join
|
227
|
+
|
228
|
+
xsl_buffer.sub!(/\[!regex_values\]/, xslt_format)
|
229
|
+
xslt = Nokogiri::XSLT(xsl_buffer)
|
230
|
+
out = xslt.transform(Nokogiri::XML(@doc.to_s))
|
231
|
+
|
232
|
+
header + "\n--+\n" + out.text
|
233
|
+
|
234
|
+
elsif self.delimiter.length > 0 then
|
235
|
+
|
236
|
+
tfo = TableFormatter.new border: false, nowrap: true, divider: self.delimiter
|
237
|
+
tfo.source = self.to_h.map{|x| x.values}
|
238
|
+
header + tfo.display
|
239
|
+
|
240
|
+
else
|
241
|
+
|
242
|
+
format_mask = self.format_mask
|
243
|
+
format_mask.gsub!(/\[[^!\]]+\]/) {|x| x[1] }
|
244
|
+
xslt_format = format_mask.gsub(/\s(?=\[!\w+\])/,'<xsl:text> </xsl:text>')
|
245
|
+
.gsub(/\[!(\w+)\]/, '<xsl:value-of select="\1"/>')
|
246
|
+
|
247
|
+
xsl_buffer.sub!(/\[!regex_values\]/, xslt_format)
|
248
|
+
xslt = Nokogiri::XSLT(xsl_buffer)
|
249
|
+
|
250
|
+
out = xslt.transform(Nokogiri::XML(self.to_xml))
|
251
|
+
header + "\n" + out.text
|
252
|
+
end
|
253
|
+
|
254
|
+
#xsl_buffer.sub!(/\[!regex_values\]/, xslt_format)
|
255
|
+
#xslt = Nokogiri::XSLT(xsl_buffer)
|
256
|
+
#out = xslt.transform(Nokogiri::XML(@doc.to_s))
|
257
|
+
#jr250811 puts 'xsl_buffer: ' + xsl_buffer
|
258
|
+
#jr250811 puts 'doc_to_s: ' + @doc.to_s
|
259
|
+
#out.text
|
260
|
+
#jr231211 Rexslt.new(xsl_buffer, @doc.to_s).to_s
|
261
|
+
|
262
|
+
end
|
263
|
+
|
264
|
+
def to_xml(opt={})
|
265
|
+
opt = {pretty: true} if opt == :pretty
|
266
|
+
display_xml(opt)
|
267
|
+
end
|
268
|
+
|
269
|
+
#Save the document to a local file.
|
270
|
+
|
271
|
+
def save(filepath=nil, options={})
|
272
|
+
|
273
|
+
opt = {pretty: true}.merge options
|
274
|
+
filepath ||= @local_filepath
|
275
|
+
@local_filepath = filepath
|
276
|
+
xml = display_xml(opt)
|
277
|
+
buffer = block_given? ? yield(xml) : xml
|
278
|
+
File.open(filepath,'w'){|f| f.write buffer }
|
279
|
+
end
|
280
|
+
|
281
|
+
#Parses 1 or more lines of text to create or update existing records.
|
282
|
+
|
283
|
+
def parse(x=nil)
|
284
|
+
if x.is_a? String then
|
285
|
+
buffer = x.clone
|
286
|
+
buffer = yield if block_given?
|
287
|
+
string_parse buffer
|
288
|
+
else
|
289
|
+
foreign_import x
|
290
|
+
end
|
291
|
+
end
|
292
|
+
|
293
|
+
|
294
|
+
alias import parse
|
295
|
+
|
296
|
+
#Create a record from a hash containing the field name, and the field value.
|
297
|
+
# dynarex = Dynarex.new 'contacts/contact(name,age,dob)'
|
298
|
+
# dynarex.create name: Bob, age: 52
|
299
|
+
|
300
|
+
def create(arg, id=nil)
|
301
|
+
raise 'Dynarex#create(): input error: no arg provided' unless arg
|
302
|
+
#jr291012 rebuild_doc()
|
303
|
+
#jr291012 (load_records; rebuild_doc) if @dirty_flag == true
|
304
|
+
methods = {Hash: :hash_create, String: :create_from_line}
|
305
|
+
send (methods[arg.class.to_s.to_sym]), arg, id
|
306
|
+
|
307
|
+
#jr291012load_records
|
308
|
+
@dirty_flag = true
|
309
|
+
|
310
|
+
self
|
311
|
+
end
|
312
|
+
|
313
|
+
#Create a record from a string, given the dynarex document contains a format mask.
|
314
|
+
# dynarex = Dynarex.new 'contacts/contact(name,age,dob)'
|
315
|
+
# dynarex.create_from_line 'Tracy 37 15-Jun-1972'
|
316
|
+
|
317
|
+
def create_from_line(line, id=nil)
|
318
|
+
t = @format_mask.to_s.gsub(/\[!(\w+)\]/, '(.*)').sub(/\[/,'\[').sub(/\]/,'\]')
|
319
|
+
line.match(/#{t}/).captures
|
320
|
+
|
321
|
+
a = line.match(/#{t}/).captures
|
322
|
+
h = Hash[@fields.zip(a)]
|
323
|
+
create h
|
324
|
+
self
|
325
|
+
end
|
326
|
+
|
327
|
+
def default_key=(id)
|
328
|
+
@default_key = id.to_sym
|
329
|
+
@summary[:default_key] = id
|
330
|
+
@fields << id.to_sym
|
331
|
+
end
|
332
|
+
|
333
|
+
|
334
|
+
#Updates a record from an id and a hash containing field name and field value.
|
335
|
+
# dynarex.update 4, name: Jeff, age: 38
|
336
|
+
|
337
|
+
def update(id, params={})
|
338
|
+
fields = capture_fields(params)
|
339
|
+
|
340
|
+
|
341
|
+
# for each field update each record field
|
342
|
+
record = @doc.root.element("records/#{@record_name}[@id='#{id.to_s}']")
|
343
|
+
fields.each {|k,v| record.element(k.to_s).text = v if v}
|
344
|
+
record.add_attribute(last_modified: Time.now.to_s)
|
345
|
+
|
346
|
+
@dirty_flag = true
|
347
|
+
|
348
|
+
self
|
349
|
+
|
350
|
+
end
|
351
|
+
|
352
|
+
|
353
|
+
#Delete a record.
|
354
|
+
# dyarex.delete 3 # deletes record with id 3
|
355
|
+
|
356
|
+
def delete(x)
|
357
|
+
|
358
|
+
if x.to_i.to_s == x.to_s and x[/[0-9]/] then
|
359
|
+
@doc.root.delete("records/*[@id='#{x}']")
|
360
|
+
else
|
361
|
+
@doc.delete x
|
362
|
+
end
|
363
|
+
@dirty_flag = true
|
364
|
+
self
|
365
|
+
end
|
366
|
+
|
367
|
+
def element(x)
|
368
|
+
@doc.root.element x
|
369
|
+
end
|
370
|
+
|
371
|
+
def sort_by!(&element_blk)
|
372
|
+
refresh_doc
|
373
|
+
a = @doc.root.xpath('records/*').sort_by &element_blk
|
374
|
+
@doc.root.delete('records')
|
375
|
+
|
376
|
+
records = Rexle::Element.new 'records'
|
377
|
+
|
378
|
+
a.each {|record| records.add record}
|
379
|
+
|
380
|
+
@doc.root.add records
|
381
|
+
|
382
|
+
load_records
|
383
|
+
self
|
384
|
+
end
|
385
|
+
|
386
|
+
def rebuild_doc(state=:internal)
|
387
|
+
|
388
|
+
reserved_keywords = (
|
389
|
+
Object.public_methods | \
|
390
|
+
Kernel.public_methods | \
|
391
|
+
public_methods + [:method_missing]
|
392
|
+
)
|
393
|
+
|
394
|
+
xml = RexleBuilder.new
|
395
|
+
|
396
|
+
a = xml.send @root_name do
|
397
|
+
|
398
|
+
xml.summary do
|
399
|
+
|
400
|
+
@summary.each do |key,value|
|
401
|
+
|
402
|
+
xml.send key, value.gsub('>','>')\
|
403
|
+
.gsub('<','<')\
|
404
|
+
.gsub(/(&\s|&[a-zA-Z\.]+;?)/) {|x| x[-1] == ';' ? x : x.sub('&','&')}
|
405
|
+
|
406
|
+
end
|
407
|
+
end
|
408
|
+
|
409
|
+
records = @records.to_a
|
410
|
+
|
411
|
+
if records then
|
412
|
+
|
413
|
+
records.reverse! if @order == 'descending' and state == :external
|
414
|
+
|
415
|
+
xml.records do
|
416
|
+
|
417
|
+
records.each do |k, item|
|
418
|
+
#p 'foo ' + item.inspect
|
419
|
+
xml.send(@record_name, {id: item[:id], created: item[:created], \
|
420
|
+
last_modified: item[:last_modified]}, '') do
|
421
|
+
item[:body].each do |name,value|
|
422
|
+
#name = name.to_s.prepend('._').to_sym if reserved_keywords.include? name
|
423
|
+
name = ('._' + name.to_s).to_sym if reserved_keywords.include? name
|
424
|
+
val = value.send(value.is_a?(String) ? :to_s : :to_yaml)
|
425
|
+
xml.send(name, val.gsub('>','>')\
|
426
|
+
.gsub('<','<')\
|
427
|
+
.gsub(/(&\s|&[a-zA-Z\.]+;?)/) do |x|
|
428
|
+
x[-1] == ';' ? x : x.sub('&','&')
|
429
|
+
end
|
430
|
+
)
|
431
|
+
end
|
432
|
+
end
|
433
|
+
end
|
434
|
+
|
435
|
+
end
|
436
|
+
else
|
437
|
+
xml.records
|
438
|
+
end # end of if @records
|
439
|
+
end
|
440
|
+
|
441
|
+
doc = Rexle.new(a)
|
442
|
+
|
443
|
+
if @xslt then
|
444
|
+
doc.instructions = [['xml-stylesheet',
|
445
|
+
"title='XSL_formatting' type='text/xsl' href='#{@xslt}'"]]
|
446
|
+
end
|
447
|
+
|
448
|
+
return doc if state != :internal
|
449
|
+
@doc = doc
|
450
|
+
end
|
451
|
+
|
452
|
+
def record(id)
|
453
|
+
recordx_to_record @doc.root.element("records/*[@id='#{id}']")
|
454
|
+
end
|
455
|
+
|
456
|
+
alias find record
|
457
|
+
|
458
|
+
def record_exists?(id)
|
459
|
+
!@doc.root.element("records/*[@id='#{id}']").nil?
|
460
|
+
end
|
461
|
+
|
462
|
+
def to_xslt(opt={})
|
463
|
+
|
464
|
+
h = {limit: -1}.merge(opt)
|
465
|
+
xslt_schema = @xslt_schema || self.summary[:xslt_schema]
|
466
|
+
raise 'to_xsl(): xslt_schema nil' unless xslt_schema
|
467
|
+
|
468
|
+
xslt = DynarexXSLT.new(schema: @schema, xslt_schema: @xslt_schema ).to_xslt
|
469
|
+
return xslt
|
470
|
+
end
|
471
|
+
|
472
|
+
def to_rss(opt={}, xslt=nil)
|
473
|
+
|
474
|
+
unless xslt then
|
475
|
+
|
476
|
+
h = {limit: 11}.merge(opt)
|
477
|
+
doc = Rexle.new(self.to_xslt)
|
478
|
+
|
479
|
+
e = doc.element('//xsl:apply-templates[2]')
|
480
|
+
doc2 = Rexle.new "<xsl:sort order='descending' data-type='number' select='@id'/>"
|
481
|
+
e.add doc2.root
|
482
|
+
|
483
|
+
e2 = doc.root.element('xsl:template[3]')
|
484
|
+
item = e2.element('item')
|
485
|
+
new_item = item.deep_clone
|
486
|
+
item.delete
|
487
|
+
|
488
|
+
xslif = Rexle.new("<xsl:if test='position() < #{h[:limit]}'/>").root
|
489
|
+
|
490
|
+
|
491
|
+
pubdate = Rexle.new("<pubDate><xsl:value-of select='pubDate'></xsl:value-of></pubDate>").root
|
492
|
+
new_item.add pubdate
|
493
|
+
xslif.add new_item
|
494
|
+
|
495
|
+
e2.add xslif.root
|
496
|
+
xslt = doc.xml
|
497
|
+
end
|
498
|
+
|
499
|
+
doc = self.to_doc
|
500
|
+
doc.root.xpath('records/*').each do |x|
|
501
|
+
raw_dt = DateTime.parse x.attributes[:created]
|
502
|
+
dt = raw_dt.strftime("%a, %d %b %Y %H:%M:%S %z")
|
503
|
+
x.add Rexle::Element.new('pubDate').add_text dt.to_s
|
504
|
+
end
|
505
|
+
|
506
|
+
#File.open('dynarex.xsl','w'){|f| f.write xslt}
|
507
|
+
#File.open('dynarex.xml','w'){|f| f.write doc.xml}
|
508
|
+
#xml = Rexslt.new(xslt, doc.xml).to_s
|
509
|
+
#=begin
|
510
|
+
xslt = Nokogiri::XSLT(xslt)
|
511
|
+
out = xslt.transform(Nokogiri::XML(doc.root.xml))
|
512
|
+
#=end
|
513
|
+
|
514
|
+
#Rexle.new("<rss version='2.0'>%s</rss>" % xml).xml(pretty: true)
|
515
|
+
Rexle.new("<rss version='2.0'>%s</rss>" % out).xml(pretty: true)
|
516
|
+
end
|
517
|
+
|
518
|
+
def xpath(x)
|
519
|
+
@doc.root.xpath x
|
520
|
+
end
|
521
|
+
|
522
|
+
def xslt=(value)
|
523
|
+
|
524
|
+
self.summary.merge!({xslt: value})
|
525
|
+
@xslt = value
|
526
|
+
end
|
527
|
+
|
528
|
+
private
|
529
|
+
|
530
|
+
def add_id(a)
|
531
|
+
@default_key = :uid
|
532
|
+
@summary[:default_key] = 'uid'
|
533
|
+
@fields << :uid
|
534
|
+
a.each.with_index{|x,i| x << (i+1).to_s}
|
535
|
+
end
|
536
|
+
|
537
|
+
def create_find(fields)
|
538
|
+
|
539
|
+
methods = fields.map do |field|
|
540
|
+
"def find_by_#{field}(value) findx_by('#{field}', value) end\n" + \
|
541
|
+
"def find_all_by_#{field}(value) findx_all_by('#{field}', value) end"
|
542
|
+
end
|
543
|
+
self.instance_eval(methods.join("\n"))
|
544
|
+
end
|
545
|
+
|
546
|
+
def findx_by(field, value)
|
547
|
+
(load_records; rebuild_doc) if @dirty_flag == true
|
548
|
+
r = @doc.root.element("records/*[#{field}='#{value}']")
|
549
|
+
r ? recordx_to_record(r) : nil
|
550
|
+
end
|
551
|
+
|
552
|
+
def findx_all_by(field, value)
|
553
|
+
@doc.root.xpath("records/*[#{field}='#{value}']").map {|x| recordx_to_record x}
|
554
|
+
end
|
555
|
+
|
556
|
+
def recordx_to_record(recordx)
|
557
|
+
RecordX.new(Hash[*@fields.zip(recordx.xpath("*/text()")).flatten], self, recordx.attributes[:id])
|
558
|
+
end
|
559
|
+
|
560
|
+
def hash_create(raw_params={}, id=nil)
|
561
|
+
|
562
|
+
record = make_record(raw_params)
|
563
|
+
@doc.root.element('records').add record
|
564
|
+
|
565
|
+
end
|
566
|
+
|
567
|
+
def capture_fields(params)
|
568
|
+
fields = Hash[@fields.map {|x| [x,nil]}]
|
569
|
+
fields.keys.each {|key| fields[key] = params[key.to_sym] if params.has_key? key.to_sym}
|
570
|
+
fields
|
571
|
+
end
|
572
|
+
|
573
|
+
def display_xml(options={})
|
574
|
+
|
575
|
+
opt = {unescape_html: false}.merge options
|
576
|
+
load_records if @dirty_flag == true
|
577
|
+
doc = rebuild_doc(:external)
|
578
|
+
if opt[:unescape_html] == true then
|
579
|
+
doc.content(opt) #jr230711 pretty: true
|
580
|
+
else
|
581
|
+
doc.xml(opt) #jr230711 pretty: true
|
582
|
+
end
|
583
|
+
end
|
584
|
+
|
585
|
+
def make_record(raw_params)
|
586
|
+
|
587
|
+
id = (@doc.root.xpath('max(records/*/attribute::id)') || '0').succ unless id
|
588
|
+
raw_params.merge!(uid: id) if @default_key == :uid
|
589
|
+
params = Hash[raw_params.keys.map(&:to_sym).zip(raw_params.values)]
|
590
|
+
|
591
|
+
fields = capture_fields(params)
|
592
|
+
record = Rexle::Element.new @record_name
|
593
|
+
|
594
|
+
fields.each do |k,v|
|
595
|
+
element = Rexle::Element.new(k.to_s)
|
596
|
+
element.root.text = v.to_s.gsub('<','<').gsub('>','>') if v
|
597
|
+
record.add element if record
|
598
|
+
end
|
599
|
+
|
600
|
+
|
601
|
+
|
602
|
+
attributes = {id: id.to_s, created: Time.now.to_s, last_modified: nil}
|
603
|
+
attributes.each {|k,v| record.add_attribute(k, v)}
|
604
|
+
|
605
|
+
record
|
606
|
+
end
|
607
|
+
|
608
|
+
alias refresh_doc display_xml
|
609
|
+
|
610
|
+
def string_parse(buffer)
|
611
|
+
|
612
|
+
buffer.gsub!("\r",'')
|
613
|
+
buffer.gsub!(/\n-{4,}\n/,"\n\n")
|
614
|
+
buffer.gsub!(/---\n/m, "--- ")
|
615
|
+
|
616
|
+
buffer.gsub!(/.>/) {|x| x[0] != '?' ? x.sub(/>/,'>') : x }
|
617
|
+
buffer.gsub!(/<./) {|x| x[1] != '?' ? x.sub(/</,'<') : x }
|
618
|
+
|
619
|
+
@raw_header = buffer[/<\?dynarex[^>]+>/]
|
620
|
+
|
621
|
+
if buffer[/<\?/] then
|
622
|
+
|
623
|
+
raw_stylesheet = buffer.slice!(/<\?xml-stylesheet[^>]+>/)
|
624
|
+
@xslt = raw_stylesheet[/href=["']([^"']+)/,1] if raw_stylesheet
|
625
|
+
@raw_header = buffer.slice!(/<\?dynarex[^>]+>/) + "\n"
|
626
|
+
|
627
|
+
header = @raw_header[/<?dynarex (.*)?>/,1]
|
628
|
+
|
629
|
+
r1 = /([\w\-]+\s*\=\s*'[^']*)'/
|
630
|
+
r2 = /([\w\-]+\s*\=\s*"[^"]*)"/
|
631
|
+
|
632
|
+
r = header.scan(/#{r1}|#{r2}/).map(&:compact).flatten
|
633
|
+
r.each do |x|
|
634
|
+
attr, val = x.split(/\s*=\s*["']/,2)
|
635
|
+
self.method((attr + '=').to_sym).call(unescape val)
|
636
|
+
end
|
637
|
+
|
638
|
+
end
|
639
|
+
|
640
|
+
# if records already exist find the max id
|
641
|
+
i = @doc.root.xpath('max(records/*/attribute::id)').to_i
|
642
|
+
|
643
|
+
raw_summary = schema[/\[([^\]]+)/,1]
|
644
|
+
#rowx = buffer[/--\+.*/m]
|
645
|
+
|
646
|
+
#buffer = rowx if rowx
|
647
|
+
|
648
|
+
#jr061013 raw_lines = buffer.gsub(/^\s*#[^\n]+/,'').lines.to_a
|
649
|
+
raw_lines = buffer.lines.to_a
|
650
|
+
|
651
|
+
if raw_summary then
|
652
|
+
|
653
|
+
a_summary = raw_summary.split(',').map(&:strip)
|
654
|
+
|
655
|
+
@summary ||= {}
|
656
|
+
raw_lines.shift while raw_lines.first.strip.empty?
|
657
|
+
|
658
|
+
# fetch any summary lines
|
659
|
+
while not raw_lines.empty? and \
|
660
|
+
raw_lines.first[/#{a_summary.join('|')}:\s+\S+/] do
|
661
|
+
|
662
|
+
label, val = raw_lines.shift.chomp.match(/(\w+):\s+([^$]+)$/).captures
|
663
|
+
@summary[label.to_sym] = val
|
664
|
+
end
|
665
|
+
|
666
|
+
self.xslt = @summary[:xslt] || @summary[:xsl] if @summary[:xslt]\
|
667
|
+
or @summary[:xsl]
|
668
|
+
end
|
669
|
+
|
670
|
+
|
671
|
+
if @type == 'checklist' then
|
672
|
+
|
673
|
+
# extract the brackets from the line
|
674
|
+
|
675
|
+
checked = []
|
676
|
+
raw_lines.map! do |x|
|
677
|
+
raw_checked, raw_line = x.partition(/\]/).values_at 0,2
|
678
|
+
checked << (raw_checked[/x/] ? true : false)
|
679
|
+
raw_line
|
680
|
+
end
|
681
|
+
|
682
|
+
end
|
683
|
+
|
684
|
+
if @order == 'descending' then
|
685
|
+
rl = raw_lines
|
686
|
+
|
687
|
+
if rl.first =~ /--/ then
|
688
|
+
raw_lines = [rl[0]] + rl[1..-1].each_slice(@fields.count).inject([])\
|
689
|
+
{|r,x| r += x.reverse }.reverse
|
690
|
+
else
|
691
|
+
raw_lines = rl.each_slice(@fields.count).inject([])\
|
692
|
+
{|r,x| r += x.reverse }.reverse
|
693
|
+
end
|
694
|
+
checked.reverse! if @type == 'checklist'
|
695
|
+
|
696
|
+
end
|
697
|
+
|
698
|
+
@summary[:recordx_type] = 'dynarex'
|
699
|
+
@summary[:schema] = @schema
|
700
|
+
@summary[:format_mask] = @format_mask
|
701
|
+
|
702
|
+
raw_lines.shift while raw_lines.first.strip.empty?
|
703
|
+
|
704
|
+
lines = case raw_lines.first.chomp
|
705
|
+
|
706
|
+
when '---'
|
707
|
+
|
708
|
+
yaml = YAML.load raw_lines.join("\n")
|
709
|
+
|
710
|
+
yamlize = lambda {|x| (x.is_a? Array) ? x.to_yaml : x}
|
711
|
+
|
712
|
+
yprocs = {
|
713
|
+
Hash: lambda {|y|
|
714
|
+
y.map do |k,v|
|
715
|
+
procs = {Hash: proc {|x| x.values}, Array: proc {v}}
|
716
|
+
values = procs[v.class.to_s.to_sym].call(v).map(&yamlize)
|
717
|
+
[k, *values]
|
718
|
+
end
|
719
|
+
},
|
720
|
+
Array: lambda {|y| y.map {|x2| x2.map(&yamlize)} }
|
721
|
+
}
|
722
|
+
|
723
|
+
yprocs[yaml.class.to_s.to_sym].call yaml
|
724
|
+
|
725
|
+
when '--+'
|
726
|
+
|
727
|
+
self.summary[:rawdoc_type] = 'rowx'
|
728
|
+
raw_lines.shift
|
729
|
+
|
730
|
+
a3 = raw_lines.join.strip.split(/\n\n(?=\w+:)/)
|
731
|
+
|
732
|
+
# get the fields
|
733
|
+
a4 = a3.map{|x| x.scan(/^\w+(?=:)/)}.flatten(1).uniq
|
734
|
+
|
735
|
+
abbrv_fields = a4.all? {|x| x.length == 1}
|
736
|
+
|
733
737
|
a5 = a3.map do |xlines|
|
734
|
-
|
738
|
+
|
735
739
|
missing_fields = a4 - xlines.scan(/^\w+(?=:)/)
|
736
|
-
|
740
|
+
|
737
741
|
r = xlines.split(/\n(\w+:.*)/m)
|
738
|
-
missing_fields.map!{|x| x + ":"}
|
739
|
-
key = (abbrv_fields ? @fields[0].to_s[0] : @fields.first.to_s) + ':'
|
740
|
-
|
741
|
-
if missing_fields.include? key
|
742
|
-
r.unshift key
|
743
|
-
missing_fields.delete key
|
744
|
-
end
|
745
|
-
|
746
|
-
r += missing_fields
|
747
|
-
r.join("\n")
|
748
|
-
|
749
|
-
end
|
750
|
-
|
751
|
-
xml = RowX.new(a5.join("\n").strip, level: 0).to_xml
|
752
|
-
|
753
|
-
a2 = Rexle.new(xml).root.xpath('item').inject([]) do |r,x|
|
754
|
-
|
755
|
-
r << @fields.map do |field|
|
756
|
-
x.text(abbrv_fields ? field.to_s.chr : field.to_s )
|
757
|
-
end
|
758
|
-
|
759
|
-
end
|
760
|
-
|
761
|
-
a2.compact!
|
762
|
-
|
763
|
-
# if there is no field value for the first field then
|
764
|
-
# the default_key is invalid. The default_key is changed to an ID.
|
765
|
-
if a2.detect {|x| x.first == ''} then
|
766
|
-
add_id(a2)
|
767
|
-
else
|
768
|
-
|
769
|
-
a3 = a2.map(&:first)
|
770
|
-
add_id(a2) if a3 != a3.uniq
|
771
|
-
|
772
|
-
end
|
773
|
-
|
774
|
-
a2
|
775
|
-
|
776
|
-
else
|
777
|
-
|
778
|
-
raw_lines = raw_lines.join("\n").gsub(/^\s*#[^\n]+/,'').lines.to_a
|
779
|
-
a2 = raw_lines.map.with_index do |x,i|
|
780
|
-
|
781
|
-
next if x[/^\s+$|\n\s*#/]
|
782
|
-
|
783
|
-
begin
|
784
|
-
|
785
|
-
field_names, field_values = RXRawLineParser.new(@format_mask).parse(x)
|
786
|
-
rescue
|
787
|
-
raise "input file parser error at line " + (i + 1).to_s + ' --> ' + x
|
788
|
-
end
|
789
|
-
field_values
|
790
|
-
end
|
791
|
-
|
792
|
-
a2.compact!
|
793
|
-
a3 = a2.compact.map(&:first)
|
794
|
-
add_id(a2) if a3 != a3.uniq
|
795
|
-
|
796
|
-
a2
|
797
|
-
end
|
798
|
-
|
799
|
-
a = lines.map.with_index do |x,i|
|
800
|
-
created = Time.now.to_s
|
801
|
-
|
802
|
-
h = Hash[
|
803
|
-
@fields.zip(
|
804
|
-
x.map do |t|
|
805
|
-
|
806
|
-
t.to_s[/^---(?:\s|\n)/] ? YAML.load(t[/^---(?:\s|\n)(.*)/,1]) : unescape(t.to_s)
|
807
|
-
end
|
808
|
-
)
|
809
|
-
]
|
810
|
-
h[@fields.last] = checked[i].to_s if @type == 'checklist'
|
811
|
-
[h[@default_key], {id: '', created: created, last_modified: '', body: h}]
|
812
|
-
end
|
813
|
-
|
814
|
-
h2 = Hash[a]
|
815
|
-
|
816
|
-
#replace the existing records hash
|
817
|
-
h = @records
|
818
|
-
i = 0
|
819
|
-
h2.each do |key,item|
|
820
|
-
if h.has_key? key then
|
821
|
-
|
822
|
-
# overwrite the previous item and change the timestamps
|
823
|
-
h[key][:last_modified] = item[:created]
|
824
|
-
item[:body].each do |k,v|
|
825
|
-
h[key][:body][k.to_sym] = v
|
826
|
-
end
|
827
|
-
else
|
828
|
-
item[:id] = (@order == 'descending' ? (h2.count) - i : i+ 1).to_s
|
829
|
-
i += 1
|
830
|
-
h[key] = item.clone
|
831
|
-
end
|
832
|
-
end
|
833
|
-
|
834
|
-
h.each {|key, item| h.delete(key) if not h2.has_key? key}
|
835
|
-
#refresh_doc
|
836
|
-
#load_records
|
837
|
-
@flat_records = @records.values.map{|x| x[:body]}
|
838
|
-
@flat_records = @flat_records.take @limit_by if @limit_by
|
839
|
-
|
840
|
-
rebuild_doc
|
841
|
-
self
|
842
|
-
end
|
843
|
-
|
844
|
-
def unescape(s)
|
845
|
-
s.gsub('<', '<').gsub('>','>')
|
846
|
-
end
|
847
|
-
|
848
|
-
def dynarex_new(s)
|
849
|
-
@schema = s
|
850
|
-
ptrn = %r((\w+)\[?([^\]]+)?\]?\/(\w+)\(([^\)]+)\))
|
851
|
-
|
852
|
-
if s.match(ptrn) then
|
853
|
-
@root_name, raw_summary, record_name, raw_fields = s.match(ptrn).captures
|
854
|
-
summary, fields = [raw_summary || '',raw_fields].map {|x| x.split(/,/).map &:strip}
|
855
|
-
create_find fields
|
856
|
-
|
857
|
-
reserved = %w(require parent)
|
858
|
-
raise 'reserved keyword' if (fields & reserved).any?
|
859
|
-
|
860
|
-
else
|
861
|
-
ptrn = %r((\w+)\[?([^\]]+)?\]?)
|
862
|
-
@root_name, raw_summary = s.match(ptrn).captures
|
863
|
-
summary = raw_summary.split(/,/).map &:strip
|
864
|
-
|
865
|
-
end
|
866
|
-
|
867
|
-
format_mask = fields ? fields.map {|x| "[!%s]" % x}.join(' ') : ''
|
868
|
-
|
869
|
-
@summary = Hash[summary.zip([''] * summary.length).flatten.each_slice(2)\
|
870
|
-
.map{|x1,x2| [x1.to_sym,x2]}]
|
871
|
-
@summary.merge!({recordx_type: 'dynarex', format_mask: format_mask, schema: s})
|
872
|
-
@records = {}
|
873
|
-
@flat_records = {}
|
874
|
-
|
875
|
-
rebuild_doc
|
876
|
-
|
877
|
-
end
|
878
|
-
|
879
|
-
def attach_record_methods()
|
880
|
-
create_find @fields
|
881
|
-
end
|
882
|
-
|
883
|
-
def openx(s)
|
884
|
-
|
885
|
-
if s[/</] then # xml
|
886
|
-
|
887
|
-
buffer = s
|
888
|
-
elsif s[/[\[\(]/] # schema
|
889
|
-
dynarex_new(s)
|
890
|
-
elsif s[/^https?:\/\//] then # url
|
891
|
-
buffer = Kernel.open(s, 'UserAgent' => 'Dynarex-Reader'){|x| x.read}
|
892
|
-
else # local file
|
893
|
-
@local_filepath = s
|
894
|
-
|
895
|
-
|
896
|
-
|
897
|
-
|
898
|
-
|
899
|
-
|
900
|
-
|
901
|
-
|
902
|
-
|
903
|
-
|
904
|
-
|
905
|
-
@
|
906
|
-
@
|
907
|
-
|
908
|
-
|
909
|
-
|
910
|
-
|
911
|
-
@
|
912
|
-
|
913
|
-
|
914
|
-
|
915
|
-
|
916
|
-
|
917
|
-
|
918
|
-
|
919
|
-
|
920
|
-
@
|
921
|
-
|
922
|
-
|
923
|
-
|
924
|
-
|
925
|
-
|
926
|
-
|
927
|
-
|
928
|
-
|
929
|
-
|
930
|
-
|
931
|
-
@default_key = @doc.root.
|
932
|
-
|
933
|
-
|
934
|
-
|
935
|
-
|
936
|
-
|
937
|
-
|
938
|
-
|
939
|
-
|
940
|
-
|
941
|
-
|
942
|
-
|
943
|
-
|
944
|
-
|
945
|
-
|
946
|
-
|
947
|
-
|
948
|
-
|
949
|
-
|
950
|
-
|
951
|
-
|
952
|
-
|
953
|
-
|
954
|
-
|
955
|
-
|
956
|
-
|
957
|
-
|
958
|
-
@
|
959
|
-
|
960
|
-
|
961
|
-
|
962
|
-
|
963
|
-
|
964
|
-
|
965
|
-
|
966
|
-
|
967
|
-
|
968
|
-
|
969
|
-
|
970
|
-
|
971
|
-
|
972
|
-
|
973
|
-
|
974
|
-
|
975
|
-
|
976
|
-
|
977
|
-
|
978
|
-
|
979
|
-
|
980
|
-
|
981
|
-
|
982
|
-
|
983
|
-
|
984
|
-
|
985
|
-
|
986
|
-
|
987
|
-
|
988
|
-
|
989
|
-
|
990
|
-
|
991
|
-
|
992
|
-
|
993
|
-
|
994
|
-
|
995
|
-
|
996
|
-
|
997
|
-
|
998
|
-
|
999
|
-
|
1000
|
-
|
1001
|
-
|
1002
|
-
|
1003
|
-
|
1004
|
-
|
1005
|
-
|
1006
|
-
|
1007
|
-
|
1008
|
-
|
1009
|
-
|
1010
|
-
<xsl:
|
1011
|
-
|
1012
|
-
|
1013
|
-
|
1014
|
-
|
1015
|
-
|
1016
|
-
</xsl:
|
1017
|
-
|
1018
|
-
<
|
1019
|
-
|
1020
|
-
|
1021
|
-
|
1022
|
-
<xsl:
|
1023
|
-
</xsl:text>
|
1024
|
-
</xsl:
|
1025
|
-
</
|
1026
|
-
</xsl:text>
|
1027
|
-
</xsl:
|
1028
|
-
|
1029
|
-
|
1030
|
-
|
1031
|
-
|
1032
|
-
|
1033
|
-
@
|
1034
|
-
|
1035
|
-
|
1036
|
-
|
1037
|
-
|
1038
|
-
|
1039
|
-
|
1040
|
-
|
1041
|
-
|
1042
|
-
|
1043
|
-
|
742
|
+
missing_fields.map!{|x| x + ":"}
|
743
|
+
key = (abbrv_fields ? @fields[0].to_s[0] : @fields.first.to_s) + ':'
|
744
|
+
|
745
|
+
if missing_fields.include? key
|
746
|
+
r.unshift key
|
747
|
+
missing_fields.delete key
|
748
|
+
end
|
749
|
+
|
750
|
+
r += missing_fields
|
751
|
+
r.join("\n")
|
752
|
+
|
753
|
+
end
|
754
|
+
|
755
|
+
xml = RowX.new(a5.join("\n").strip, level: 0).to_xml
|
756
|
+
|
757
|
+
a2 = Rexle.new(xml).root.xpath('item').inject([]) do |r,x|
|
758
|
+
|
759
|
+
r << @fields.map do |field|
|
760
|
+
x.text(abbrv_fields ? field.to_s.chr : field.to_s )
|
761
|
+
end
|
762
|
+
|
763
|
+
end
|
764
|
+
|
765
|
+
a2.compact!
|
766
|
+
|
767
|
+
# if there is no field value for the first field then
|
768
|
+
# the default_key is invalid. The default_key is changed to an ID.
|
769
|
+
if a2.detect {|x| x.first == ''} then
|
770
|
+
add_id(a2)
|
771
|
+
else
|
772
|
+
|
773
|
+
a3 = a2.map(&:first)
|
774
|
+
add_id(a2) if a3 != a3.uniq
|
775
|
+
|
776
|
+
end
|
777
|
+
|
778
|
+
a2
|
779
|
+
|
780
|
+
else
|
781
|
+
|
782
|
+
raw_lines = raw_lines.join("\n").gsub(/^\s*#[^\n]+/,'').lines.to_a
|
783
|
+
a2 = raw_lines.map.with_index do |x,i|
|
784
|
+
|
785
|
+
next if x[/^\s+$|\n\s*#/]
|
786
|
+
|
787
|
+
begin
|
788
|
+
|
789
|
+
field_names, field_values = RXRawLineParser.new(@format_mask).parse(x)
|
790
|
+
rescue
|
791
|
+
raise "input file parser error at line " + (i + 1).to_s + ' --> ' + x
|
792
|
+
end
|
793
|
+
field_values
|
794
|
+
end
|
795
|
+
|
796
|
+
a2.compact!
|
797
|
+
a3 = a2.compact.map(&:first)
|
798
|
+
add_id(a2) if a3 != a3.uniq
|
799
|
+
|
800
|
+
a2
|
801
|
+
end
|
802
|
+
|
803
|
+
a = lines.map.with_index do |x,i|
|
804
|
+
created = Time.now.to_s
|
805
|
+
|
806
|
+
h = Hash[
|
807
|
+
@fields.zip(
|
808
|
+
x.map do |t|
|
809
|
+
|
810
|
+
t.to_s[/^---(?:\s|\n)/] ? YAML.load(t[/^---(?:\s|\n)(.*)/,1]) : unescape(t.to_s)
|
811
|
+
end
|
812
|
+
)
|
813
|
+
]
|
814
|
+
h[@fields.last] = checked[i].to_s if @type == 'checklist'
|
815
|
+
[h[@default_key], {id: '', created: created, last_modified: '', body: h}]
|
816
|
+
end
|
817
|
+
|
818
|
+
h2 = Hash[a]
|
819
|
+
|
820
|
+
#replace the existing records hash
|
821
|
+
h = @records
|
822
|
+
i = 0
|
823
|
+
h2.each do |key,item|
|
824
|
+
if h.has_key? key then
|
825
|
+
|
826
|
+
# overwrite the previous item and change the timestamps
|
827
|
+
h[key][:last_modified] = item[:created]
|
828
|
+
item[:body].each do |k,v|
|
829
|
+
h[key][:body][k.to_sym] = v
|
830
|
+
end
|
831
|
+
else
|
832
|
+
item[:id] = (@order == 'descending' ? (h2.count) - i : i+ 1).to_s
|
833
|
+
i += 1
|
834
|
+
h[key] = item.clone
|
835
|
+
end
|
836
|
+
end
|
837
|
+
|
838
|
+
h.each {|key, item| h.delete(key) if not h2.has_key? key}
|
839
|
+
#refresh_doc
|
840
|
+
#load_records
|
841
|
+
@flat_records = @records.values.map{|x| x[:body]}
|
842
|
+
@flat_records = @flat_records.take @limit_by if @limit_by
|
843
|
+
|
844
|
+
rebuild_doc
|
845
|
+
self
|
846
|
+
end
|
847
|
+
|
848
|
+
def unescape(s)
|
849
|
+
s.gsub('<', '<').gsub('>','>')
|
850
|
+
end
|
851
|
+
|
852
|
+
def dynarex_new(s)
|
853
|
+
@schema = s
|
854
|
+
ptrn = %r((\w+)\[?([^\]]+)?\]?\/(\w+)\(([^\)]+)\))
|
855
|
+
|
856
|
+
if s.match(ptrn) then
|
857
|
+
@root_name, raw_summary, record_name, raw_fields = s.match(ptrn).captures
|
858
|
+
summary, fields = [raw_summary || '',raw_fields].map {|x| x.split(/,/).map &:strip}
|
859
|
+
create_find fields
|
860
|
+
|
861
|
+
reserved = %w(require parent)
|
862
|
+
raise 'reserved keyword' if (fields & reserved).any?
|
863
|
+
|
864
|
+
else
|
865
|
+
ptrn = %r((\w+)\[?([^\]]+)?\]?)
|
866
|
+
@root_name, raw_summary = s.match(ptrn).captures
|
867
|
+
summary = raw_summary.split(/,/).map &:strip
|
868
|
+
|
869
|
+
end
|
870
|
+
|
871
|
+
format_mask = fields ? fields.map {|x| "[!%s]" % x}.join(' ') : ''
|
872
|
+
|
873
|
+
@summary = Hash[summary.zip([''] * summary.length).flatten.each_slice(2)\
|
874
|
+
.map{|x1,x2| [x1.to_sym,x2]}]
|
875
|
+
@summary.merge!({recordx_type: 'dynarex', format_mask: format_mask, schema: s})
|
876
|
+
@records = {}
|
877
|
+
@flat_records = {}
|
878
|
+
|
879
|
+
rebuild_doc
|
880
|
+
|
881
|
+
end
|
882
|
+
|
883
|
+
def attach_record_methods()
|
884
|
+
create_find @fields
|
885
|
+
end
|
886
|
+
|
887
|
+
def openx(s)
|
888
|
+
|
889
|
+
if s[/</] then # xml
|
890
|
+
|
891
|
+
buffer = s
|
892
|
+
elsif s[/[\[\(]/] # schema
|
893
|
+
dynarex_new(s)
|
894
|
+
elsif s[/^https?:\/\//] then # url
|
895
|
+
buffer = Kernel.open(s, 'UserAgent' => 'Dynarex-Reader'){|x| x.read}
|
896
|
+
else # local file
|
897
|
+
@local_filepath = s
|
898
|
+
raise DynarexException, 'file not found: ' + s
|
899
|
+
buffer = File.read s
|
900
|
+
end
|
901
|
+
|
902
|
+
if buffer then
|
903
|
+
raw_stylesheet = buffer.slice!(/<\?xml-stylesheet[^>]+>/)
|
904
|
+
@xslt = raw_stylesheet[/href=["']([^"']+)/,1] if raw_stylesheet
|
905
|
+
|
906
|
+
@doc = Rexle.new(buffer) unless @doc
|
907
|
+
end
|
908
|
+
|
909
|
+
@schema = @doc.root.text('summary/schema')
|
910
|
+
@root_name = @doc.root.name
|
911
|
+
@summary = summary_to_h
|
912
|
+
|
913
|
+
@order = @summary[:order] if @summary.has_key? :order
|
914
|
+
|
915
|
+
@default_key = @doc.root.element('summary/default_key/text()')
|
916
|
+
@format_mask = @doc.root.element('summary/format_mask/text()')
|
917
|
+
|
918
|
+
@fields = @schema[/([^(]+)\)$/,1].split(/\s*,\s*/).map(&:to_sym)
|
919
|
+
|
920
|
+
@fields << @default_key if @default_key and \
|
921
|
+
!@fields.include? @default_key.to_sym
|
922
|
+
|
923
|
+
if @schema and @schema.match(/(\w+)\(([^\)]+)/) then
|
924
|
+
@record_name, raw_fields = @schema.match(/(\w+)\(([^\)]+)/).captures
|
925
|
+
@fields = raw_fields.split(',').map{|x| x.strip.to_sym} unless @fields
|
926
|
+
end
|
927
|
+
|
928
|
+
if @fields then
|
929
|
+
|
930
|
+
@default_key = @fields[0] unless @default_key
|
931
|
+
# load the record query handler methods
|
932
|
+
attach_record_methods
|
933
|
+
else
|
934
|
+
|
935
|
+
#jr080912 @default_key = @doc.root.xpath('records/*/*').first.name
|
936
|
+
@default_key = @doc.root.element('records/./.[1]').name
|
937
|
+
end
|
938
|
+
|
939
|
+
@summary[:default_key] = @default_key.to_s
|
940
|
+
|
941
|
+
if @doc.root.xpath('records/*').length > 0 then
|
942
|
+
@record_name = @doc.root.element('records/*[1]').name
|
943
|
+
#jr240913 load_records
|
944
|
+
@dirty_flag = true
|
945
|
+
end
|
946
|
+
|
947
|
+
end
|
948
|
+
|
949
|
+
def load_records
|
950
|
+
|
951
|
+
@records = records_to_h
|
952
|
+
|
953
|
+
@records = @records.take @limit_by if @limit_by
|
954
|
+
|
955
|
+
@records.instance_eval do
|
956
|
+
def delete_item(i)
|
957
|
+
self.delete self.keys[i]
|
958
|
+
end
|
959
|
+
end
|
960
|
+
|
961
|
+
#Returns a ready-only snapshot of records as a simple Hash.
|
962
|
+
@flat_records = @records.values.map{|x| x[:body]}
|
963
|
+
@dirty_flag = false
|
964
|
+
end
|
965
|
+
|
966
|
+
def display()
|
967
|
+
puts @doc.to_s
|
968
|
+
end
|
969
|
+
|
970
|
+
def records_to_h(state=:ascending)
|
971
|
+
|
972
|
+
i = @doc.root.xpath('max(records/*/attribute::id)') || 0
|
973
|
+
#jr090813 fields = @doc.root.text('summary/schema')[/\(.*\)/].scan(/\w+/)
|
974
|
+
records = @doc.root.xpath('records/*')
|
975
|
+
|
976
|
+
recs = (state == :descending ? records.reverse : records)
|
977
|
+
a = recs.inject({}) do |result,row|
|
978
|
+
|
979
|
+
created = Time.now.to_s
|
980
|
+
last_modified = ''
|
981
|
+
|
982
|
+
if row.attributes[:id] then
|
983
|
+
id = row.attributes[:id]
|
984
|
+
else
|
985
|
+
i += 1; id = i.to_s
|
986
|
+
end
|
987
|
+
|
988
|
+
created = row.attributes[:created] if row.attributes[:created]
|
989
|
+
last_modified = row.attributes[:last_modified] if row.attributes[:last_modified]
|
990
|
+
|
991
|
+
body = @fields.inject({}) do |r,field|
|
992
|
+
|
993
|
+
node = row.element field.to_s
|
994
|
+
|
995
|
+
if node then
|
996
|
+
text = node.text ? node.text.unescape : ''
|
997
|
+
|
998
|
+
r.merge node.name.to_sym => (text[/^---(?:\s|\n)/] ? YAML.load(text[/^---(?:\s|\n)(.*)/,1]) : text)
|
999
|
+
else
|
1000
|
+
r
|
1001
|
+
end
|
1002
|
+
end
|
1003
|
+
|
1004
|
+
result.merge body[@default_key.to_sym] => {id: id, created: created, last_modified: last_modified, body: body}
|
1005
|
+
end
|
1006
|
+
|
1007
|
+
end
|
1008
|
+
|
1009
|
+
def sort_records
|
1010
|
+
xsl =<<XSL
|
1011
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
1012
|
+
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
|
1013
|
+
|
1014
|
+
<xsl:template match="*">
|
1015
|
+
<xsl:element name="{name()}"><xsl:text>
|
1016
|
+
</xsl:text>
|
1017
|
+
<xsl:copy-of select="summary"/><xsl:text>
|
1018
|
+
</xsl:text>
|
1019
|
+
<xsl:apply-templates select="records"/>
|
1020
|
+
</xsl:element>
|
1021
|
+
</xsl:template>
|
1022
|
+
<xsl:template match="records">
|
1023
|
+
<records><xsl:text>
|
1024
|
+
</xsl:text>
|
1025
|
+
<xsl:for-each select="child::*">
|
1026
|
+
<xsl:sort order="descending"/>
|
1027
|
+
<xsl:text> </xsl:text><xsl:copy-of select="."/><xsl:text>
|
1028
|
+
</xsl:text>
|
1029
|
+
</xsl:for-each>
|
1030
|
+
</records><xsl:text>
|
1031
|
+
</xsl:text>
|
1032
|
+
</xsl:template>
|
1033
|
+
|
1034
|
+
</xsl:stylesheet>
|
1035
|
+
XSL
|
1036
|
+
|
1037
|
+
@doc = Rexle.new(Rexslt.new(xsl, self.to_xml).to_s)
|
1038
|
+
@dirty_flag = true
|
1039
|
+
end
|
1040
|
+
|
1041
|
+
def summary_to_h
|
1042
|
+
|
1043
|
+
@doc.root.xpath('summary/*').inject({}) do |r,node|
|
1044
|
+
r.merge node.name.to_s.to_sym => node.text.to_s
|
1045
|
+
end
|
1046
|
+
end
|
1047
|
+
|
1048
|
+
end
|