fat_table 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/ruby-with-dbs.yml +34 -53
- data/.gitignore +1 -0
- data/CHANGELOG.md +88 -72
- data/CHANGELOG.org +6 -0
- data/Gemfile +3 -2
- data/README.md +171 -228
- data/README.org +127 -180
- data/lib/core_ext/array.rb +1 -1
- data/lib/core_ext/numeric_string.rb +0 -2
- data/lib/fat_table/evaluator.rb +1 -2
- data/lib/fat_table/formatters/formatter.rb +1 -2
- data/lib/fat_table/table.rb +5 -3
- data/lib/fat_table/version.rb +1 -1
- metadata +2 -2
data/README.md
CHANGED
|
@@ -1,55 +1,59 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
- [
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
- [
|
|
7
|
-
- [
|
|
8
|
-
- [
|
|
9
|
-
|
|
10
|
-
- [
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
- [
|
|
14
|
-
- [
|
|
15
|
-
|
|
16
|
-
- [
|
|
17
|
-
|
|
18
|
-
- [
|
|
19
|
-
- [From
|
|
20
|
-
- [From
|
|
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
|
-
- [
|
|
1
|
+
<!-- Generated by gem_docs -->
|
|
2
|
+
|
|
3
|
+
- [Version](#org67e2e62)
|
|
4
|
+
- [Introduction](#orgae63911)
|
|
5
|
+
- [Installation](#orgdc3a993)
|
|
6
|
+
- [Using in a gem](#orgf6469f4)
|
|
7
|
+
- [Manually install](#org0580462)
|
|
8
|
+
- [Require](#orgf2420d2)
|
|
9
|
+
- [Usage](#orgbf23591)
|
|
10
|
+
- [Quick Start](#orge8dc4e8)
|
|
11
|
+
- [A Word About the Examples](#orge568f6d)
|
|
12
|
+
- [Anatomy of a Table](#org3949293)
|
|
13
|
+
- [Columns](#orge22bb40)
|
|
14
|
+
- [Type Keywords Arguments](#org598b66e)
|
|
15
|
+
- [Headers](#orgd5de944)
|
|
16
|
+
- [Groups](#org82e3428)
|
|
17
|
+
- [Constructing Tables](#org7a63b8f)
|
|
18
|
+
- [Empty Tables](#orgcd1538f)
|
|
19
|
+
- [From CSV or Org Mode files or strings](#org62da1b6)
|
|
20
|
+
- [From Arrays of Arrays](#org173f3e1)
|
|
21
|
+
- [From Arrays of Hashes](#org3ebe654)
|
|
22
|
+
- [From SQL queries](#org2ff6d2c)
|
|
23
|
+
- [Marking Groups in Input](#org1ff1a1a)
|
|
24
|
+
- [Accessing Parts of Tables](#org83ae075)
|
|
25
|
+
- [Rows](#orga23a354)
|
|
26
|
+
- [Columns](#org146e5e1)
|
|
27
|
+
- [Cells](#org47d348d)
|
|
28
|
+
- [Other table attributes](#org5b09143)
|
|
29
|
+
- [Operations on Tables](#orgb6dc455)
|
|
30
|
+
- [Example Input Tables](#org8beca72)
|
|
31
|
+
- [Select](#org9538bf9)
|
|
32
|
+
- [Where](#orgd6eb231)
|
|
33
|
+
- [Order\_by](#org3608389)
|
|
34
|
+
- [Order\_with](#org8893b2e)
|
|
35
|
+
- [Group\_by](#org56e5398)
|
|
36
|
+
- [Join](#org26cdd4f)
|
|
37
|
+
- [Set Operations](#orgaea3e4e)
|
|
38
|
+
- [Uniq (aka Distinct)](#org9288f10)
|
|
39
|
+
- [Remove groups with degroup!](#org03677a6)
|
|
40
|
+
- [Formatting Tables](#org503c8fa)
|
|
41
|
+
- [Available Formatter Output Targets](#org36d8f82)
|
|
42
|
+
- [Formatting Directives](#org42c7c80)
|
|
43
|
+
- [The `format` and `format_for` methods](#orgb60b896)
|
|
44
|
+
- [Footers](#orgd9669e6)
|
|
45
|
+
- [Invoking Formatters](#org155d27a)
|
|
46
|
+
- [Development](#orgb2b7054)
|
|
47
|
+
- [Contributing](#org73344ae)
|
|
48
|
+
|
|
49
|
+
Version: 1.1.0
|
|
46
50
|
|
|
47
51
|
[](
|
|
48
52
|
https://github.com/ddoherty03/fat_table/actions/workflows/ruby-with-dbs.yml
|
|
49
53
|
)
|
|
50
54
|
|
|
51
55
|
|
|
52
|
-
<a id="
|
|
56
|
+
<a id="org67e2e62"></a>
|
|
53
57
|
|
|
54
58
|
# Version
|
|
55
59
|
|
|
@@ -58,11 +62,11 @@ https://github.com/ddoherty03/fat_table/actions/workflows/ruby-with-dbs.yml
|
|
|
58
62
|
```
|
|
59
63
|
|
|
60
64
|
```
|
|
61
|
-
Current version is: 1.
|
|
65
|
+
Current version is: 1.1.0
|
|
62
66
|
```
|
|
63
67
|
|
|
64
68
|
|
|
65
|
-
<a id="
|
|
69
|
+
<a id="orgae63911"></a>
|
|
66
70
|
|
|
67
71
|
# Introduction
|
|
68
72
|
|
|
@@ -73,12 +77,12 @@ Furthermore, `FatTable` provides methods for formatting tables and producing out
|
|
|
73
77
|
`FatTable` can be used to perform operations on data that are naturally best conceived of as tables, which in my experience is quite often. It can also serve as a foundation for providing reporting functions where flexibility about the output medium can be useful. Finally `FatTable` can be used within Emacs `org-mode` files in code blocks targeting the Ruby language. Org mode tables are presented to a ruby code block as an array of arrays, so `FatTable` can read them in with its `.from_aoa` constructor. A `FatTable` table output as an array of arrays with its `.to_aoa` output function will be rendered in an org-mode buffer as an org-table, ready for processing by other code blocks.
|
|
74
78
|
|
|
75
79
|
|
|
76
|
-
<a id="
|
|
80
|
+
<a id="orgdc3a993"></a>
|
|
77
81
|
|
|
78
82
|
# Installation
|
|
79
83
|
|
|
80
84
|
|
|
81
|
-
<a id="
|
|
85
|
+
<a id="orgf6469f4"></a>
|
|
82
86
|
|
|
83
87
|
## Using in a gem
|
|
84
88
|
|
|
@@ -99,7 +103,7 @@ gem.add_runtime_dependency 'fat_table'
|
|
|
99
103
|
```
|
|
100
104
|
|
|
101
105
|
```
|
|
102
|
-
|
|
106
|
+
#<ArgumentError: wrong number of arguments (given 0, expected 1+)>
|
|
103
107
|
```
|
|
104
108
|
|
|
105
109
|
And then execute:
|
|
@@ -109,7 +113,7 @@ $ bundle
|
|
|
109
113
|
```
|
|
110
114
|
|
|
111
115
|
|
|
112
|
-
<a id="
|
|
116
|
+
<a id="org0580462"></a>
|
|
113
117
|
|
|
114
118
|
## Manually install
|
|
115
119
|
|
|
@@ -120,7 +124,7 @@ $ gem install fat_table
|
|
|
120
124
|
```
|
|
121
125
|
|
|
122
126
|
|
|
123
|
-
<a id="
|
|
127
|
+
<a id="orgf2420d2"></a>
|
|
124
128
|
|
|
125
129
|
## Require
|
|
126
130
|
|
|
@@ -131,12 +135,12 @@ require 'fat_table'
|
|
|
131
135
|
```
|
|
132
136
|
|
|
133
137
|
|
|
134
|
-
<a id="
|
|
138
|
+
<a id="orgbf23591"></a>
|
|
135
139
|
|
|
136
140
|
# Usage
|
|
137
141
|
|
|
138
142
|
|
|
139
|
-
<a id="
|
|
143
|
+
<a id="orge8dc4e8"></a>
|
|
140
144
|
|
|
141
145
|
## Quick Start
|
|
142
146
|
|
|
@@ -277,7 +281,7 @@ magick quick.png -resize 600x800 quick_small.png
|
|
|
277
281
|

|
|
278
282
|
|
|
279
283
|
|
|
280
|
-
<a id="
|
|
284
|
+
<a id="orge568f6d"></a>
|
|
281
285
|
|
|
282
286
|
## A Word About the Examples
|
|
283
287
|
|
|
@@ -340,12 +344,12 @@ locals: _ __ _dir_ _ex_ _file_ _in_ _out_ _pry_ lib str version
|
|
|
340
344
|
If you use `puts table.to_term`, you can see the effect of the color formatting directives.
|
|
341
345
|
|
|
342
346
|
|
|
343
|
-
<a id="
|
|
347
|
+
<a id="org3949293"></a>
|
|
344
348
|
|
|
345
349
|
## Anatomy of a Table
|
|
346
350
|
|
|
347
351
|
|
|
348
|
-
<a id="
|
|
352
|
+
<a id="orge22bb40"></a>
|
|
349
353
|
|
|
350
354
|
### Columns
|
|
351
355
|
|
|
@@ -360,7 +364,7 @@ If you use `puts table.to_term`, you can see the effect of the color formatting
|
|
|
360
364
|
By default, when a `Table` is constructed from an external source, all `Columns` start out having a type of `NilClass`, that is, their type is as yet undetermined. When a string or object is added to a `Column` and it can be converted into one of the permissible types, it fixes the type of the column, and all further items added to the `Column` must either be `nil` (indicating no value) or be capable of being coerced to the column's type. Otherwise, `FatTable` raises an `IncompatibleTypeError` exception.
|
|
361
365
|
|
|
362
366
|
|
|
363
|
-
<a id="
|
|
367
|
+
<a id="org598b66e"></a>
|
|
364
368
|
|
|
365
369
|
### Type Keywords Arguments
|
|
366
370
|
|
|
@@ -389,7 +393,7 @@ Items of input must be either one of the permissible ruby objects or strings. If
|
|
|
389
393
|
- **NilClass:** Until the input contains a non-blank string that can be parsed as one of the other types, it has this type, meaning that the type is still open. A column comprised completely of blank strings or `nils` will retain the `NilClass` type.
|
|
390
394
|
|
|
391
395
|
|
|
392
|
-
<a id="
|
|
396
|
+
<a id="orgd5de944"></a>
|
|
393
397
|
|
|
394
398
|
### Headers
|
|
395
399
|
|
|
@@ -407,19 +411,19 @@ If no sensible headers can be discerned from the input, headers of the form `:co
|
|
|
407
411
|
You should avoid the use of the column names `:omni` and `:sort_key` because they have special meanings in the `select` and `order_with` commands, respectively.
|
|
408
412
|
|
|
409
413
|
|
|
410
|
-
<a id="
|
|
414
|
+
<a id="org82e3428"></a>
|
|
411
415
|
|
|
412
416
|
### Groups
|
|
413
417
|
|
|
414
418
|
The rows of a `FatTable` table can be divided into groups, either from markers in the input or as a result of certain operations. There is only one level of grouping, so `FatTable` has no concept of sub-groups. Groups can be shown on output with rules or "hlines" that underline the last row in each group, and you can decorate the output with group footers that summarize the rows in each group.
|
|
415
419
|
|
|
416
420
|
|
|
417
|
-
<a id="
|
|
421
|
+
<a id="org7a63b8f"></a>
|
|
418
422
|
|
|
419
423
|
## Constructing Tables
|
|
420
424
|
|
|
421
425
|
|
|
422
|
-
<a id="
|
|
426
|
+
<a id="orgcd1538f"></a>
|
|
423
427
|
|
|
424
428
|
### Empty Tables
|
|
425
429
|
|
|
@@ -449,15 +453,6 @@ The rows of a `FatTable` table can be divided into groups, either from markers i
|
|
|
449
453
|
+========+========+============+===+===+
|
|
450
454
|
```
|
|
451
455
|
|
|
452
|
-
```
|
|
453
|
-
+======+======+============+===+===+
|
|
454
|
-
| A | B | C | D | E |
|
|
455
|
-
+------+------+------------+---+---+
|
|
456
|
-
| 1 | 2 | 2017-01-21 | F | |
|
|
457
|
-
| 3.14 | 2.17 | 2016-01-21 | T | |
|
|
458
|
-
+======+======+============+===+===+
|
|
459
|
-
```
|
|
460
|
-
|
|
461
456
|
You can continue to add rows to the table:
|
|
462
457
|
|
|
463
458
|
```ruby
|
|
@@ -479,17 +474,6 @@ The rows of a `FatTable` table can be divided into groups, either from markers i
|
|
|
479
474
|
| 3/5 | | | | | 335/113 |
|
|
480
475
|
+========+========+============+===+===+=========+
|
|
481
476
|
```
|
|
482
|
-
|
|
483
|
-
```
|
|
484
|
-
+======+======+============+===+===+=========+
|
|
485
|
-
| A | B | C | D | E | F |
|
|
486
|
-
+------+------+------------+---+---+---------+
|
|
487
|
-
| 1 | 2 | 2017-01-21 | F | | |
|
|
488
|
-
| 3.14 | 2.17 | 2016-01-21 | T | | |
|
|
489
|
-
+------+------+------------+---+---+---------+
|
|
490
|
-
| 3/5 | | | | | 335/113 |
|
|
491
|
-
+======+======+============+===+===+=========+
|
|
492
|
-
```
|
|
493
477
|
|
|
494
478
|
2. With Headers
|
|
495
479
|
|
|
@@ -505,10 +489,6 @@ The rows of a `FatTable` table can be divided into groups, either from markers i
|
|
|
505
489
|
[:a, :b, :c, :d]
|
|
506
490
|
```
|
|
507
491
|
|
|
508
|
-
```
|
|
509
|
-
[:a, :b, :c, :d]
|
|
510
|
-
```
|
|
511
|
-
|
|
512
492
|
```ruby
|
|
513
493
|
tab << { a: 1, b: 2, c: "<2017-01-21>", d: 'f', e: '' }
|
|
514
494
|
tab << { a: 3.14, b: 2.17, c: '[2016-01-21 Thu]', d: 'Y', e: nil }
|
|
@@ -523,18 +503,6 @@ The rows of a `FatTable` table can be divided into groups, either from markers i
|
|
|
523
503
|
| 3.1400 | 2.1700 | 2016-01-21 | T | |
|
|
524
504
|
+========+========+============+===+===+
|
|
525
505
|
```
|
|
526
|
-
|
|
527
|
-
```
|
|
528
|
-
+======+======+============+===+===+
|
|
529
|
-
| A | B | C | D | E |
|
|
530
|
-
+------+------+------------+---+---+
|
|
531
|
-
| 1 | 2 | 2017-01-21 | F | |
|
|
532
|
-
| 3.14 | 2.17 | 2016-01-21 | T | |
|
|
533
|
-
+------+------+------------+---+---+
|
|
534
|
-
| 1 | 2 | 2017-01-21 | F | |
|
|
535
|
-
| 3.14 | 2.17 | 2016-01-21 | T | |
|
|
536
|
-
+======+======+============+===+===+
|
|
537
|
-
```
|
|
538
506
|
|
|
539
507
|
3. Forcing String Type
|
|
540
508
|
|
|
@@ -563,18 +531,6 @@ The rows of a `FatTable` table can be divided into groups, either from markers i
|
|
|
563
531
|
+========+========+============+===+=============+===+
|
|
564
532
|
```
|
|
565
533
|
|
|
566
|
-
```
|
|
567
|
-
+===+===+============+===+=============+===+
|
|
568
|
-
| A | B | C | D | Zip | E |
|
|
569
|
-
+---+---+------------+---+-------------+---+
|
|
570
|
-
| 1 | 2 | 2017-01-21 | F | 18552 | |
|
|
571
|
-
| 3 | 2 | 2016-01-21 | T | | |
|
|
572
|
-
| | | | | 01879--7884 | |
|
|
573
|
-
| | | | | 90210 | |
|
|
574
|
-
| | | | | | |
|
|
575
|
-
+===+===+============+===+=============+===+
|
|
576
|
-
```
|
|
577
|
-
|
|
578
534
|
In addition, at any time after creating a table, you can force the String type on any number of columns with the `force_string!` method. When you do so, all exisiting items in the column are converted to strings with the #to\_s method.
|
|
579
535
|
|
|
580
536
|
```ruby
|
|
@@ -599,21 +555,9 @@ The rows of a `FatTable` table can be divided into groups, either from markers i
|
|
|
599
555
|
| | | | | 90210 | |
|
|
600
556
|
+========+========+============+===+=======+===+
|
|
601
557
|
```
|
|
602
|
-
|
|
603
|
-
```
|
|
604
|
-
+======+======+============+===+=======+===+
|
|
605
|
-
| A | B | C | D | Zip | E |
|
|
606
|
-
+------+------+------------+---+-------+---+
|
|
607
|
-
| 1 | 2 | 2017-01-21 | F | 18552 | |
|
|
608
|
-
| 3.14 | 2.17 | 2016-01-21 | T | | |
|
|
609
|
-
| | | | | 01879 | |
|
|
610
|
-
| | | | | 66210 | |
|
|
611
|
-
| | | | | 90210 | |
|
|
612
|
-
+======+======+============+===+=======+===+
|
|
613
|
-
```
|
|
614
558
|
|
|
615
559
|
|
|
616
|
-
<a id="
|
|
560
|
+
<a id="org62da1b6"></a>
|
|
617
561
|
|
|
618
562
|
### From CSV or Org Mode files or strings
|
|
619
563
|
|
|
@@ -670,7 +614,7 @@ tab4 = FatTable.from_org_string(org_body)
|
|
|
670
614
|
```
|
|
671
615
|
|
|
672
616
|
|
|
673
|
-
<a id="
|
|
617
|
+
<a id="org173f3e1"></a>
|
|
674
618
|
|
|
675
619
|
### From Arrays of Arrays
|
|
676
620
|
|
|
@@ -771,7 +715,7 @@ tab4 = FatTable.from_org_string(org_body)
|
|
|
771
715
|
4. `FatTable` passes back to org-mode an array of arrays using the `.to_aoa` method. In an `org-mode` buffer, these are rendered as tables. We'll often apply `.to_aoa` at the end of example blocks in this `README` to render the results as a table inside this file. As we'll see below, `.to_aoa` can also take a block to which formatting and footer directives can be attached.
|
|
772
716
|
|
|
773
717
|
|
|
774
|
-
<a id="
|
|
718
|
+
<a id="org3ebe654"></a>
|
|
775
719
|
|
|
776
720
|
### From Arrays of Hashes
|
|
777
721
|
|
|
@@ -808,7 +752,7 @@ tab = FatTable.from_aoh(aoh).to_aoa
|
|
|
808
752
|
| T002 | 2016-11-01 | P | 7.7500 | 200 |
|
|
809
753
|
| T003 | 2016-11-01 | P | 7.5000 | 800 |
|
|
810
754
|
| T004 | 2016-11-01 | S | 7.5500 | 6811 |
|
|
811
|
-
| T005 |
|
|
755
|
+
| T005 | 2026-11-23 | S | 7.5000 | 4000 |
|
|
812
756
|
| T006 | 2016-11-01 | S | 7.6000 | 1000 |
|
|
813
757
|
| T007 | 2016-11-01 | S | 7.6500 | 200 |
|
|
814
758
|
| T008 | 2016-11-01 | P | 7.6500 | 2771 |
|
|
@@ -825,7 +769,7 @@ tab = FatTable.from_aoh(aoh).to_aoa
|
|
|
825
769
|
Notice, again, that the values can either be ruby objects, such as `Date.today`, or strings that can be parsed into one of the permissible column types.
|
|
826
770
|
|
|
827
771
|
|
|
828
|
-
<a id="
|
|
772
|
+
<a id="org2ff6d2c"></a>
|
|
829
773
|
|
|
830
774
|
### From SQL queries
|
|
831
775
|
|
|
@@ -891,7 +835,7 @@ Consult `Sequel's` documentation for details on its connection methods. <http://
|
|
|
891
835
|
The `.connect` function need only be called once, and the database handle it creates will be used for all subsequent `.from_sql` calls until `.connect` is called again.
|
|
892
836
|
|
|
893
837
|
|
|
894
|
-
<a id="
|
|
838
|
+
<a id="org1ff1a1a"></a>
|
|
895
839
|
|
|
896
840
|
### Marking Groups in Input
|
|
897
841
|
|
|
@@ -901,21 +845,21 @@ The `.connect` function need only be called once, and the database handle it cre
|
|
|
901
845
|
|
|
902
846
|
2. When Reading in Tables
|
|
903
847
|
|
|
904
|
-
`FatTable` tables has a concept of "groups" of rows that play a role in many of the methods for operating on them as explained [below](#
|
|
848
|
+
`FatTable` tables has a concept of "groups" of rows that play a role in many of the methods for operating on them as explained [below](#org82e3428).
|
|
905
849
|
|
|
906
850
|
The `.from_aoa` and `.from_aoh` functions take an optional keyword parameter `hlines:` that, if set to `true`, causes them to mark group boundaries in the table wherever a row Array (for `.from_aoa`) or Hash (for `.from_aoh`) is followed by a `nil`. Each boundary means that the rows above it and after the header or prior group boundary all belong to a group. By default `hlines` is false for both functions so neither expects hlines in its input.
|
|
907
851
|
|
|
908
|
-
In the case of `.from_aoa`, if `hlines:` is set true, the input must also include a `nil` in the second element of the outer array to indicate that the first row is to be used as headers. Otherwise, it will synthesize headers of the form `:col_1`, `:col_2`,
|
|
852
|
+
In the case of `.from_aoa`, if `hlines:` is set true, the input must also include a `nil` in the second element of the outer array to indicate that the first row is to be used as headers. Otherwise, it will synthesize headers of the form `:col_1`, `:col_2`, … `:col_n`.
|
|
909
853
|
|
|
910
854
|
In org mode table text passed to `.from_org_file` and `.from_org_string`, you *must* mark the header row by following it with an hrule and you *may* mark group boundaries with an hrule. In org mode tables, hlines are table rows beginning with something like `|---`. The `.from_org_...` functions always recognizes hlines in the input, so it takes no `hlines:` keyword parameter.
|
|
911
855
|
|
|
912
856
|
|
|
913
|
-
<a id="
|
|
857
|
+
<a id="org83ae075"></a>
|
|
914
858
|
|
|
915
859
|
## Accessing Parts of Tables
|
|
916
860
|
|
|
917
861
|
|
|
918
|
-
<a id="
|
|
862
|
+
<a id="orga23a354"></a>
|
|
919
863
|
|
|
920
864
|
### Rows
|
|
921
865
|
|
|
@@ -924,14 +868,14 @@ A `FatTable` table is an Enumerable, yielding each row of the table as a Hash ke
|
|
|
924
868
|
You can also use indexing to access a row of the table by number. Using an integer index returns a Hash of the given row. Thus, `tab[20]` returns the 21st data row of the table, while `tab[0]` returns the first row and tab[-1] returns the last row.
|
|
925
869
|
|
|
926
870
|
|
|
927
|
-
<a id="
|
|
871
|
+
<a id="org146e5e1"></a>
|
|
928
872
|
|
|
929
873
|
### Columns
|
|
930
874
|
|
|
931
875
|
If the index provided to `[]` is a string or a symbol, it returns an Array of the items of the column with that header. Thus, `tab[:ref]` returns an Array of all the items of the table's `:ref` column.
|
|
932
876
|
|
|
933
877
|
|
|
934
|
-
<a id="
|
|
878
|
+
<a id="org47d348d"></a>
|
|
935
879
|
|
|
936
880
|
### Cells
|
|
937
881
|
|
|
@@ -945,7 +889,7 @@ tab[:date][13] # => The Date in the 14th row; indexes can be in either order.
|
|
|
945
889
|
```
|
|
946
890
|
|
|
947
891
|
|
|
948
|
-
<a id="
|
|
892
|
+
<a id="org5b09143"></a>
|
|
949
893
|
|
|
950
894
|
### Other table attributes
|
|
951
895
|
|
|
@@ -978,29 +922,26 @@ puts "Column :shares says its type is '#{tab.type(:shares)}' and that is a #{tab
|
|
|
978
922
|
```
|
|
979
923
|
|
|
980
924
|
```
|
|
981
|
-
=> false
|
|
982
925
|
Column :shares says its type is 'Numeric' and that is a BigDecimal
|
|
983
|
-
=> nil
|
|
984
|
-
:org_babel_ruby_eoe
|
|
985
926
|
```
|
|
986
927
|
|
|
987
928
|
|
|
988
|
-
<a id="
|
|
929
|
+
<a id="orgb6dc455"></a>
|
|
989
930
|
|
|
990
931
|
## Operations on Tables
|
|
991
932
|
|
|
992
933
|
Once you have one or more tables, you will likely want to perform operations on them. The operations provided by `FatTable` are the subject of this section. Before getting into the operations, though, there are a couple of issues that cut across all or many of the operations.
|
|
993
934
|
|
|
994
|
-
First, tables are by
|
|
935
|
+
First, tables are by-and-large immutable objects: each operation creates a new table without affecting the input tables. The only exceptions are the `degroup!` operation, which mutates the receiver table by removing its group boundaries, and `force_string!` (explained above at [Forcing String Type](#orgb468e9c)), which forces columns to have the String type despite what the automatic typing rules determine.
|
|
995
936
|
|
|
996
937
|
Second, because each operation returns a `FatTable::Table` object, the operations are chainable.
|
|
997
938
|
|
|
998
939
|
Third, `FatTable::Table` objects can have "groups" of rows within the table. These can be decorated with hlines and group footers on output. Some operations result in marking group boundaries in the result table, others remove group boundaries that may have existed in the input table. Operations that either create or remove groups will be noted below.
|
|
999
940
|
|
|
1000
|
-
Finally, the operations are for the most part patterned on SQL table operations, but when expressions play a role, you write them using ruby syntax rather than SQL.
|
|
941
|
+
Finally, the operations are, for the most part, patterned on SQL table operations, but when expressions play a role, you write them using ruby syntax rather than SQL.
|
|
1001
942
|
|
|
1002
943
|
|
|
1003
|
-
<a id="
|
|
944
|
+
<a id="org8beca72"></a>
|
|
1004
945
|
|
|
1005
946
|
### Example Input Tables
|
|
1006
947
|
|
|
@@ -1123,7 +1064,7 @@ tab2.to_aoa
|
|
|
1123
1064
|
```
|
|
1124
1065
|
|
|
1125
1066
|
|
|
1126
|
-
<a id="
|
|
1067
|
+
<a id="org9538bf9"></a>
|
|
1127
1068
|
|
|
1128
1069
|
### Select
|
|
1129
1070
|
|
|
@@ -1356,28 +1297,28 @@ With the `select` method, you can select columns to appear in the output table,
|
|
|
1356
1297
|
```
|
|
1357
1298
|
| Ref | Traded On | Price | Shares | Cost | Today |
|
|
1358
1299
|
|------+------------+--------+--------+--------+------------|
|
|
1359
|
-
| T001 | 2016-11-01 | 7.7000 | 100 | 3.1416 |
|
|
1360
|
-
| T002 | 2016-11-01 | 7.7500 | 200 | 3.1416 |
|
|
1361
|
-
| T003 | 2016-11-01 | 7.5000 | 800 | 3.1416 |
|
|
1362
|
-
| T003 | 2016-11-01 | 7.5000 | 800 | 3.1416 |
|
|
1300
|
+
| T001 | 2016-11-01 | 7.7000 | 100 | 3.1416 | 2026-11-23 |
|
|
1301
|
+
| T002 | 2016-11-01 | 7.7500 | 200 | 3.1416 | 2026-11-23 |
|
|
1302
|
+
| T003 | 2016-11-01 | 7.5000 | 800 | 3.1416 | 2026-11-23 |
|
|
1303
|
+
| T003 | 2016-11-01 | 7.5000 | 800 | 3.1416 | 2026-11-23 |
|
|
1363
1304
|
|------+------------+--------+--------+--------+------------|
|
|
1364
|
-
| T004 | 2016-11-01 | 7.5500 | 6811 | 3.1416 |
|
|
1365
|
-
| T005 | 2016-11-01 | 7.5000 | 4000 | 3.1416 |
|
|
1366
|
-
| T006 | 2016-11-01 | 7.6000 | 1000 | 3.1416 |
|
|
1367
|
-
| T006 | 2016-11-01 | 7.6000 | 1000 | 3.1416 |
|
|
1368
|
-
| T007 | 2016-11-01 | 7.6500 | 200 | 3.1416 |
|
|
1369
|
-
| T008 | 2016-11-01 | 7.6500 | 2771 | 3.1416 |
|
|
1370
|
-
| T009 | 2016-11-01 | 7.6000 | 9550 | 3.1416 |
|
|
1305
|
+
| T004 | 2016-11-01 | 7.5500 | 6811 | 3.1416 | 2026-11-23 |
|
|
1306
|
+
| T005 | 2016-11-01 | 7.5000 | 4000 | 3.1416 | 2026-11-23 |
|
|
1307
|
+
| T006 | 2016-11-01 | 7.6000 | 1000 | 3.1416 | 2026-11-23 |
|
|
1308
|
+
| T006 | 2016-11-01 | 7.6000 | 1000 | 3.1416 | 2026-11-23 |
|
|
1309
|
+
| T007 | 2016-11-01 | 7.6500 | 200 | 3.1416 | 2026-11-23 |
|
|
1310
|
+
| T008 | 2016-11-01 | 7.6500 | 2771 | 3.1416 | 2026-11-23 |
|
|
1311
|
+
| T009 | 2016-11-01 | 7.6000 | 9550 | 3.1416 | 2026-11-23 |
|
|
1371
1312
|
|------+------------+--------+--------+--------+------------|
|
|
1372
|
-
| T010 | 2016-11-01 | 7.5500 | 3175 | 3.1416 |
|
|
1373
|
-
| T011 | 2016-11-02 | 7.4250 | 100 | 3.1416 |
|
|
1374
|
-
| T012 | 2016-11-02 | 7.5500 | 4700 | 3.1416 |
|
|
1375
|
-
| T012 | 2016-11-02 | 7.5500 | 4700 | 3.1416 |
|
|
1376
|
-
| T013 | 2016-11-02 | 7.3500 | 53100 | 3.1416 |
|
|
1313
|
+
| T010 | 2016-11-01 | 7.5500 | 3175 | 3.1416 | 2026-11-23 |
|
|
1314
|
+
| T011 | 2016-11-02 | 7.4250 | 100 | 3.1416 | 2026-11-23 |
|
|
1315
|
+
| T012 | 2016-11-02 | 7.5500 | 4700 | 3.1416 | 2026-11-23 |
|
|
1316
|
+
| T012 | 2016-11-02 | 7.5500 | 4700 | 3.1416 | 2026-11-23 |
|
|
1317
|
+
| T013 | 2016-11-02 | 7.3500 | 53100 | 3.1416 | 2026-11-23 |
|
|
1377
1318
|
|------+------------+--------+--------+--------+------------|
|
|
1378
|
-
| T014 | 2016-11-02 | 7.4500 | 5847 | 3.1416 |
|
|
1379
|
-
| T015 | 2016-11-02 | 7.7500 | 500 | 3.1416 |
|
|
1380
|
-
| T016 | 2016-11-02 | 8.2500 | 100 | 3.1416 |
|
|
1319
|
+
| T014 | 2016-11-02 | 7.4500 | 5847 | 3.1416 | 2026-11-23 |
|
|
1320
|
+
| T015 | 2016-11-02 | 7.7500 | 500 | 3.1416 | 2026-11-23 |
|
|
1321
|
+
| T016 | 2016-11-02 | 8.2500 | 100 | 3.1416 | 2026-11-23 |
|
|
1381
1322
|
```
|
|
1382
1323
|
|
|
1383
1324
|
5. Custom Instance Variables and Hooks
|
|
@@ -1434,7 +1375,7 @@ With the `select` method, you can select columns to appear in the output table,
|
|
|
1434
1375
|
As the example illustrates, `.select` transmits any group boundaries in its input table to the result table.
|
|
1435
1376
|
|
|
1436
1377
|
|
|
1437
|
-
<a id="
|
|
1378
|
+
<a id="orgd6eb231"></a>
|
|
1438
1379
|
|
|
1439
1380
|
### Where
|
|
1440
1381
|
|
|
@@ -1461,7 +1402,7 @@ tab1.where('@row.even? && (g10 || qp10)').to_term
|
|
|
1461
1402
|
```
|
|
1462
1403
|
|
|
1463
1404
|
|
|
1464
|
-
<a id="
|
|
1405
|
+
<a id="org3608389"></a>
|
|
1465
1406
|
|
|
1466
1407
|
### Order\_by
|
|
1467
1408
|
|
|
@@ -1501,12 +1442,14 @@ tab1.order_by(:code, :date!).to_aoa
|
|
|
1501
1442
|
|
|
1502
1443
|
The interesting thing about `order_by` is that, while it ignores groups in its input, it adds group boundaries in the output table at those rows where the sort keys change. Thus, in each group, `:code` and `:date` are the same, and when either changes, `order_by` inserts a group boundary.
|
|
1503
1444
|
|
|
1445
|
+
Sorting with `order_by` is stable: keys that are equal maintain the order they had in the unsorted table.
|
|
1446
|
+
|
|
1504
1447
|
|
|
1505
|
-
<a id="
|
|
1448
|
+
<a id="org8893b2e"></a>
|
|
1506
1449
|
|
|
1507
1450
|
### Order\_with
|
|
1508
1451
|
|
|
1509
|
-
The `order_with` method is a convenient combination of `select` and `order_by`. It takes a single string expression as an argument to serve as a sort key
|
|
1452
|
+
The `order_with` method is a convenient combination of `select` and `order_by`. It takes a single string expression as an argument to serve as a sort key—one that would be valid as a select expression—but with an optional trailing `!` to indicate reverse sort. The resulting table has an additional column called `:sort_key` with the expression evaluated for each row, and the table is sorted as with `order_by` on that column.
|
|
1510
1453
|
|
|
1511
1454
|
```ruby
|
|
1512
1455
|
tab1.order_with('price * shares').to_aoa
|
|
@@ -1552,7 +1495,7 @@ tab1.order_with('price * shares').to_aoa
|
|
|
1552
1495
|
```
|
|
1553
1496
|
|
|
1554
1497
|
|
|
1555
|
-
<a id="
|
|
1498
|
+
<a id="org56e5398"></a>
|
|
1556
1499
|
|
|
1557
1500
|
### Group\_by
|
|
1558
1501
|
|
|
@@ -1600,7 +1543,7 @@ Here is a list of all the aggregate methods available. If the description restri
|
|
|
1600
1543
|
Perhaps surprisingly, the `group_by` method ignores any groups in its input and results in no group boundaries in the output since each group formed by the implicit `order_by` on the grouping columns is collapsed into a single row.
|
|
1601
1544
|
|
|
1602
1545
|
|
|
1603
|
-
<a id="
|
|
1546
|
+
<a id="org26cdd4f"></a>
|
|
1604
1547
|
|
|
1605
1548
|
### Join
|
|
1606
1549
|
|
|
@@ -1825,7 +1768,7 @@ Perhaps surprisingly, the `group_by` method ignores any groups in its input and
|
|
|
1825
1768
|
```
|
|
1826
1769
|
|
|
1827
1770
|
|
|
1828
|
-
<a id="
|
|
1771
|
+
<a id="orgaea3e4e"></a>
|
|
1829
1772
|
|
|
1830
1773
|
### Set Operations
|
|
1831
1774
|
|
|
@@ -2110,7 +2053,7 @@ tab2.to_aoa
|
|
|
2110
2053
|
```
|
|
2111
2054
|
|
|
2112
2055
|
|
|
2113
|
-
<a id="
|
|
2056
|
+
<a id="org9288f10"></a>
|
|
2114
2057
|
|
|
2115
2058
|
### Uniq (aka Distinct)
|
|
2116
2059
|
|
|
@@ -2142,7 +2085,7 @@ tab1.uniq.to_aoa
|
|
|
2142
2085
|
```
|
|
2143
2086
|
|
|
2144
2087
|
|
|
2145
|
-
<a id="
|
|
2088
|
+
<a id="org03677a6"></a>
|
|
2146
2089
|
|
|
2147
2090
|
### Remove groups with degroup!
|
|
2148
2091
|
|
|
@@ -2177,7 +2120,7 @@ tab1.degroup!.to_aoa
|
|
|
2177
2120
|
```
|
|
2178
2121
|
|
|
2179
2122
|
|
|
2180
|
-
<a id="
|
|
2123
|
+
<a id="org503c8fa"></a>
|
|
2181
2124
|
|
|
2182
2125
|
## Formatting Tables
|
|
2183
2126
|
|
|
@@ -2188,7 +2131,7 @@ In addition, you can add any number of footers to a table, which appear at the e
|
|
|
2188
2131
|
If the target output medium does not support a formatting directive or the directive does not make sense, it is simply ignored. For example, you can output an `org-mode` table as a String, and since `org-mode` does not support colors, any color directives are ignored. Some of the output targets are not strings, but ruby data structures, and for them, things such as alignment are irrelevant.
|
|
2189
2132
|
|
|
2190
2133
|
|
|
2191
|
-
<a id="
|
|
2134
|
+
<a id="org36d8f82"></a>
|
|
2192
2135
|
|
|
2193
2136
|
### Available Formatter Output Targets
|
|
2194
2137
|
|
|
@@ -2232,7 +2175,7 @@ If the target output medium does not support a formatting directive or the direc
|
|
|
2232
2175
|
|
|
2233
2176
|
2. To Org
|
|
2234
2177
|
|
|
2235
|
-
This formatter is designed to format tables in a manner consistent with the way tables are drawn within Emacs Org Mode. It also uses nothing by ASCII characters to draw the table, but, the
|
|
2178
|
+
This formatter is designed to format tables in a manner consistent with the way tables are drawn within Emacs Org Mode. It also uses nothing by ASCII characters to draw the table, but, the outer border lines are represented by a `|` character rather than `+`; inner intersections of lines are still represented as `+` as for text. Embelishments such as color, bold, and so forth are ignored. When working in Org Mode, note that Emacs will convert an Array of Arrays into an Org Mode table, so when constructing tables programmatically, it may be better to use the `to_aoa` formatter shown below.
|
|
2236
2179
|
|
|
2237
2180
|
```ruby
|
|
2238
2181
|
tab_a.to_org
|
|
@@ -2278,7 +2221,7 @@ If the target output medium does not support a formatting directive or the direc
|
|
|
2278
2221
|
|
|
2279
2222
|
4. To LaTeX
|
|
2280
2223
|
|
|
2281
|
-
This formatter outputs a table in the form suitable for inclusion in a LaTeX document using the `
|
|
2224
|
+
This formatter outputs a table in the form suitable for inclusion in a LaTeX document using the `longtable` package. Natualy it allows embelishments such as color and text styles to the full extent of LaTeX's formatting prowess.
|
|
2282
2225
|
|
|
2283
2226
|
```ruby
|
|
2284
2227
|
tab_b.to_latex
|
|
@@ -2323,11 +2266,11 @@ If the target output medium does not support a formatting directive or the direc
|
|
|
2323
2266
|
```
|
|
2324
2267
|
|
|
2325
2268
|
|
|
2326
|
-
<a id="
|
|
2269
|
+
<a id="org42c7c80"></a>
|
|
2327
2270
|
|
|
2328
2271
|
### Formatting Directives
|
|
2329
2272
|
|
|
2330
|
-
The formatting methods explained in the next section all take formatting directives as strings in which letters and other characters signify what formatting applies. For example, we may apply the formatting directive 'R,$' to numbers in a certain part of the table. Each of those characters, and in some cases a whole substring, is a single directive. They can appear in any order, so
|
|
2273
|
+
The formatting methods explained in the next section all take formatting directives as strings in which letters and other characters signify what formatting applies. For example, we may apply the formatting directive 'R,$' to numbers in a certain part of the table. Each of those characters, and in some cases a whole substring, is a single directive. They can appear in any order, so `$R`, and `,$R` are equivalent.
|
|
2331
2274
|
|
|
2332
2275
|
Here is a list of all the formatting directives that apply to each cell type:
|
|
2333
2276
|
|
|
@@ -2335,68 +2278,68 @@ Here is a list of all the formatting directives that apply to each cell type:
|
|
|
2335
2278
|
|
|
2336
2279
|
For a string element, or any an element of any type (since these are applied after the element has been converted to a String), the following instructions are valid.
|
|
2337
2280
|
|
|
2338
|
-
-
|
|
2339
|
-
-
|
|
2340
|
-
-
|
|
2341
|
-
-
|
|
2342
|
-
-
|
|
2343
|
-
-
|
|
2344
|
-
-
|
|
2345
|
-
-
|
|
2346
|
-
-
|
|
2347
|
-
-
|
|
2348
|
-
-
|
|
2281
|
+
- **`u`:** convert the element to all lowercase [default false],
|
|
2282
|
+
- **`U`:** convert the element to all uppercase [default false],
|
|
2283
|
+
- **`t`:** title case the element, that is, upcase the initial letter in each word and lower case the other letters [default false],
|
|
2284
|
+
- **`B`, `~B`:** make the element bold, or turn off bold [default =~B=]
|
|
2285
|
+
- **`I`, `~I`:** make the element italic, or turn off italic [default =~I=]
|
|
2286
|
+
- **`R`:** align the element on the right of the column [default off]
|
|
2287
|
+
- **`L`:** align the element on the left of the column [default on]
|
|
2288
|
+
- **`C`:** align the element in the center of the column [default off]
|
|
2289
|
+
- **`c[<color_spec>]`:** render the element in the given color; the `<color_spec>` can have the form fgcolor, fgcolor.bgcolor, or .bgcolor, to set the foreground or background colors respectively, and each of those can be an ANSI or X11 color name in addition to the special color, 'none', which keeps the output's default color [default none].
|
|
2290
|
+
- **`_`:** underline the element, or turn off underline [default off]
|
|
2291
|
+
- **`*`:** cause the element to blink, or turn off blink [default off]
|
|
2349
2292
|
|
|
2350
|
-
For example, the directive 'tCc[red.yellow]' would title-case the element, center it, and color it red on a yellow background. The directives that are boolean have negating forms so that, for example, if bold is turned on for all columns of a given type, it can be countermanded in formatting directives for particular columns.
|
|
2293
|
+
For example, the directive '`tCc[red.yellow]`' would title-case the element, center it, and color it red on a yellow background. The directives that are boolean have negating forms so that, for example, if bold is turned on for all columns of a given type, it can be countermanded in formatting directives for particular columns.
|
|
2351
2294
|
|
|
2352
2295
|
2. Numeric
|
|
2353
2296
|
|
|
2354
2297
|
For a numeric element, all the instructions valid for string are available, in addition to the following:
|
|
2355
2298
|
|
|
2356
|
-
-
|
|
2357
|
-
-
|
|
2358
|
-
-
|
|
2359
|
-
-
|
|
2299
|
+
- **`,`, `~,`:** insert grouping commas, or do not insert grouping commas (default `~,`),
|
|
2300
|
+
- **`$`, `~$`:** format the number as currency according to the locale, or not (default `~$`),
|
|
2301
|
+
- **`m.n`:** include at least m digits before the decimal point, padding on the left with zeroes as needed, and round the number to the n decimal places and include n digits after the decimal point, padding on the right with zeroes as needed. If n is negative, the value will be rounded to the left of the decimal point: e.g., if n is -2, the number will be rounded to the nearest hundred, if -3, to the nearest thousand, etc. (default `0.0`)
|
|
2302
|
+
- **`H`:** convert the number (assumed to be in units of seconds) to `HH:MM:SS.ss` form. So a column that is the result of subtracting two :datetime forms will result in a :numeric expressed as seconds and can be displayed in hours, minutes, and seconds with this formatting instruction. If this directive is included, all other numeric directives will be ignored. (default off)
|
|
2360
2303
|
|
|
2361
|
-
For example, the directive
|
|
2304
|
+
For example, the directive `R5.0c[blue]` would right-align the numeric element, pad it on the left with zeros, and color it blue.
|
|
2362
2305
|
|
|
2363
2306
|
3. DateTime
|
|
2364
2307
|
|
|
2365
2308
|
For a `DateTime`, all the instructions valid for string are available, in addition to the following:
|
|
2366
2309
|
|
|
2367
|
-
-
|
|
2368
|
-
-
|
|
2310
|
+
- **`d[fmt]`:** apply the format to a `Date` or a `DateTime` that is a whole day, that is that has no or zero hour, minute, and second components, where fmt is a valid format string for `Date#strftime`, otherwise, the datetime will be formatted as an ISO 8601 string, `YYYY-MM-DD`.
|
|
2311
|
+
- **`D[fmt]`:** apply the format to a datetime that has at least a non-zero hour component where fmt is a valid format string for `Date#strftime`, otherwise, the datetime will be formatted as an ISO 8601 string, `YYYY-MM-DD HH:MM:SS`.
|
|
2369
2312
|
|
|
2370
|
-
For example,
|
|
2313
|
+
For example, `c[pink]d[%b %-d, %Y]C`, would format a date element like `Sep 22, 1957`, center it, and color it pink.
|
|
2371
2314
|
|
|
2372
2315
|
4. Boolean
|
|
2373
2316
|
|
|
2374
2317
|
For a boolean cell, all the instructions valid for string are available, in addition to the following:
|
|
2375
2318
|
|
|
2376
|
-
-
|
|
2377
|
-
-
|
|
2378
|
-
-
|
|
2379
|
-
-
|
|
2380
|
-
-
|
|
2319
|
+
- **`Y`:** print true as `Y` and false as `N`,
|
|
2320
|
+
- **`T`:** print true as `T` and false as `F` (this is the default),
|
|
2321
|
+
- **`X`:** print true as `X` and false as an empty string `""`,
|
|
2322
|
+
- **`b[<xxx>,<yyy>]`:** print true as the arbitrary string given as `<xxx>` and false as the arbitrary string given as `<yyy>`,
|
|
2323
|
+
- **`c[<tcolor>,<fcolor>]`:** color a true element with `<tcolor>` and a false element with `<fcolor>`. Each of the colors may be specified in the same manner as colors for strings described above.
|
|
2381
2324
|
|
|
2382
|
-
For example, the directive
|
|
2325
|
+
For example, the directive `b[Yeppers,Nope]c[green.pink,red.pink]` would render a true boolean as `Yeppers` colored green on pink and render a false boolean as `Nope` colored red on pink. See [Yeppers](https://www.youtube.com/watch?v=oLdFFD8II8U) for additional information.
|
|
2383
2326
|
|
|
2384
2327
|
5. NilClass
|
|
2385
2328
|
|
|
2386
2329
|
By default, `nil` elements are rendered as blank cells, but you can make them visible with the following, and in that case, all the formatting instructions valid for strings are also available:
|
|
2387
2330
|
|
|
2388
|
-
-
|
|
2331
|
+
- **`n[<niltext>]`:** render a `nil` item with the given arbitrary string `<niltext>` (default `""`).
|
|
2389
2332
|
|
|
2390
|
-
For example, you might want to use
|
|
2333
|
+
For example, you might want to use `n[-]Cc[purple]` to make =nil=s visible as a centered purple hyphen.
|
|
2391
2334
|
|
|
2392
2335
|
|
|
2393
|
-
<a id="
|
|
2336
|
+
<a id="orgb60b896"></a>
|
|
2394
2337
|
|
|
2395
2338
|
### The `format` and `format_for` methods
|
|
2396
2339
|
|
|
2397
2340
|
Formatters take only two kinds of methods, those that attach footers to a table, which are discussed in the next section, and those that specify formatting for table cells, which are the subject of this section.
|
|
2398
2341
|
|
|
2399
|
-
To set formatting directives for all locations in a table at once, use the `format` method; to set formatting directives for a particular location in the table, use the `format_for` method, giving the location as the first parameter. See below at
|
|
2342
|
+
To set formatting directives for all locations in a table at once, use the `format` method; to set formatting directives for a particular location in the table, use the `format_for` method, giving the location as the first parameter. See below at for an explanation of all the locations available.
|
|
2400
2343
|
|
|
2401
2344
|
Other than that first parameter, the two methods take the same types of parameters. The remaining parameters are hash-like parameters that use either a column name or a type as the key and a string with the formatting directives to apply as the value. If a key represents neither a column name nor a valid type, it is silently ignored. The following example says to set the formatting for all locations in the table and to format all numeric fields as strings that are rounded to whole numbers (the '0.0' part), that are right-aligned (the 'R' part), and have grouping commas inserted (the ',' part). But the `:id` column is numeric, and the second parameter overrides the formatting for numerics in general and calls for the `:id` column to be padded to three digits with zeros on the left (the '3.0' part) and to be centered (the 'C' part).
|
|
2402
2345
|
|
|
@@ -2430,16 +2373,16 @@ But the `format_for` methods affected two "locations": the "body" and the "heade
|
|
|
2430
2373
|
|
|
2431
2374
|
All the other cells in the table, namely the cells in the `:join_date` column, had the default formatting applied.
|
|
2432
2375
|
|
|
2433
|
-
1. Table
|
|
2376
|
+
1. Locations within a Table
|
|
2434
2377
|
|
|
2435
2378
|
In the `format_for` formatting method, the first argument names a "location." The table is divided into several locations for which separate formatting directives may be given. These locations are identified by the following symbols:
|
|
2436
2379
|
|
|
2437
|
-
-
|
|
2438
|
-
-
|
|
2439
|
-
-
|
|
2440
|
-
-
|
|
2441
|
-
-
|
|
2442
|
-
-
|
|
2380
|
+
- **`:header`:** the first row of the output table containing the headers,
|
|
2381
|
+
- **`:footer`:** all rows of the table's footers,
|
|
2382
|
+
- **`:gfooter`:** all rows of the table's group footers,
|
|
2383
|
+
- **`:body`:** all the data rows of the table, that is, those that are neither part of the header, footers, or group footers,
|
|
2384
|
+
- **`:bfirst`:** the first row of the table's body, and
|
|
2385
|
+
- **`:gfirst`:** the first row in each group in the table's body.
|
|
2443
2386
|
|
|
2444
2387
|
2. Location priority
|
|
2445
2388
|
|
|
@@ -2489,13 +2432,13 @@ All the other cells in the table, namely the cells in the `:join_date` column, h
|
|
|
2489
2432
|
The `string: 'R'` directive causes all the cells to be right-aligned except `:id` which specifies centering for the `:id` column only. The `n[N/A]` directive for specifies how nil are displayed in the numeric column, `:salary`, but not for other nils, such as in the last row of the `:join_date` column.
|
|
2490
2433
|
|
|
2491
2434
|
|
|
2492
|
-
<a id="
|
|
2435
|
+
<a id="orgd9669e6"></a>
|
|
2493
2436
|
|
|
2494
2437
|
### Footers
|
|
2495
2438
|
|
|
2496
2439
|
1. Adding Footers
|
|
2497
2440
|
|
|
2498
|
-
You can call the `foot`, `gfoot`, `footer,` or `gfooter`, methods on `Formatter` objects to add footers and group footers.
|
|
2441
|
+
You can call the `foot`, `gfoot`, `footer,` or `gfooter`, methods on `Formatter` objects to add footers and group footers. All of these methods return the `FatTable::Footer` object so constructed. It can be used to access the values and other attributes of the footer computed. Their signatures are:
|
|
2499
2442
|
|
|
2500
2443
|
- **`foot(label: label, label_col: nil, **agg_cols)`:** where `label` is a label to be placed in the column with header `label_col`, or, if ommitted, in the first cell of the footer (unless that column is named as one of the `agg_cols`, in which case the label is ignored), and `**agg_cols` is zero or more hash-like parameters with a column symbol as a key and a valid aggregate as the value. This causes a table-wide header to be added at the bottom of the table applying `agg`, to the `agg_cols`. A table can have any number of footers attached, and they will appear at the bottom of the output table in the order they are given.
|
|
2501
2444
|
|
|
@@ -2546,16 +2489,16 @@ All the other cells in the table, namely the cells in the `:join_date` column, h
|
|
|
2546
2489
|
|
|
2547
2490
|
When adding a footer with the above methods, you can specify an aggregator for each column named in the `agg_cols` parameter. There are several candidates for what you can use for an aggregator:
|
|
2548
2491
|
|
|
2549
|
-
- **Symbol:** one of the following built-in aggregators:
|
|
2492
|
+
- **Symbol:** one of the following built-in aggregators: `:first`, `:last`, `:range`, `:sum`, `:count`, `:min`, `:max`, `:avg`, `:var`, `:pvar`, `:dev`, `:pdev`, `:any?`, `:all?`, `:none?`, and `:one?`.
|
|
2550
2493
|
- The symbols ending in a question mark are valid only for boolean columns;
|
|
2551
|
-
-
|
|
2552
|
-
-
|
|
2553
|
-
-
|
|
2554
|
-
-
|
|
2494
|
+
- `:count`, `:first`, and `:last` work with any column type,
|
|
2495
|
+
- `:min`, `:max`, and `:range` work with all types except boolean;
|
|
2496
|
+
- `:sum`, works only with numeric columns, and
|
|
2497
|
+
- `:avg`, `:var`, `:dev`, `:pvar`, and `:pdev` work with numeric or datetime columns. In the case of datetime columns, these aggregators convert the dates to julian date numbers, perform the calculation, then convert the result back to a datetime object. Apart from the built-in aggregators, you could define your own by opening the `FatTable::Column` class and adding a suitable instance method. In that case, the symbol could also refer to the method you defined.
|
|
2555
2498
|
- **String:** using a string as an aggrgegator can result in:
|
|
2556
2499
|
- the string being converted to an object matching the type of the column (for example, using '$1,888' in a numeric column puts the constant number 1888 in the footer field, using '1957-09-22' puts the fixed date in the field, etc.)
|
|
2557
2500
|
- if the string cannot be parsed as a valid object matching the column's type, it is placed literally in the footer field (for example, using '(estimated)' can be used to add additional information to the footer)
|
|
2558
|
-
- **Ruby object:** you can put a
|
|
2501
|
+
- **Ruby object:** you can put a `Numeric` in a numeric footer field, a `Date` or `DateTime` object in a datetime footer field, or a true or false in a boolean footer field;
|
|
2559
2502
|
- **A Lambda:** finally, you can provide a lambda for performing arbitrary calculations and placing the result in the footer field. The number of arguments the lambda takes can vary:
|
|
2560
2503
|
- If the lambda is used in an ordinary footer column, it can take 0, 1, or 2 arguments: (1) the first argument, if given, will be set to the FatTable::Column object for that column and (2) the second argument, if given, will be set to the Footer object itself.
|
|
2561
2504
|
- If the lambda is used in a group footer column, it can 0, 1, 2, or 3 arguments: (1) the first argument, if given, will be set to the group's 0-based index number, (2) the second argument, if given, will be set to a FatTable::Column object consisting of those items in the group's column, and (3) the third argument, if given, will be set to the Footer object itself.
|
|
@@ -2695,7 +2638,7 @@ All the other cells in the table, namely the cells in the `:join_date` column, h
|
|
|
2695
2638
|
+-------------+-------+-----+------------+--------+-------------+
|
|
2696
2639
|
| Average | | 31 | | 31,429 | 29-DEC-2005 |
|
|
2697
2640
|
+-------------+-------+-----+------------+--------+-------------+
|
|
2698
|
-
| Report Date | | 8 | | |
|
|
2641
|
+
| Report Date | | 8 | | | 23-NOV-2026 |
|
|
2699
2642
|
+=============+=======+=====+============+========+=============+
|
|
2700
2643
|
```
|
|
2701
2644
|
|
|
@@ -2725,7 +2668,7 @@ All the other cells in the table, namely the cells in the `:join_date` column, h
|
|
|
2725
2668
|
+----------------+-------+-----+------------+--------+-------------+
|
|
2726
2669
|
| Average | | 31 | | 31,429 | 29-DEC-2005 |
|
|
2727
2670
|
+----------------+-------+-----+------------+--------+-------------+
|
|
2728
|
-
| Lottery Winner | | 8 | | |
|
|
2671
|
+
| Lottery Winner | | 8 | | | 4 |
|
|
2729
2672
|
+================+=======+=====+============+========+=============+
|
|
2730
2673
|
```
|
|
2731
2674
|
|
|
@@ -2809,7 +2752,7 @@ All the other cells in the table, namely the cells in the `:join_date` column, h
|
|
|
2809
2752
|
```
|
|
2810
2753
|
|
|
2811
2754
|
|
|
2812
|
-
<a id="
|
|
2755
|
+
<a id="org155d27a"></a>
|
|
2813
2756
|
|
|
2814
2757
|
### Invoking Formatters
|
|
2815
2758
|
|
|
@@ -2945,7 +2888,7 @@ As the examples show, one way to invoke the formatting methods is simply to call
|
|
|
2945
2888
|
```
|
|
2946
2889
|
|
|
2947
2890
|
|
|
2948
|
-
<a id="
|
|
2891
|
+
<a id="orgb2b7054"></a>
|
|
2949
2892
|
|
|
2950
2893
|
# Development
|
|
2951
2894
|
|
|
@@ -2954,7 +2897,7 @@ After checking out the repo, run \`bin/setup\` to install dependencies. Then, ru
|
|
|
2954
2897
|
To install this gem onto your local machine, run \`bundle exec rake install\`.
|
|
2955
2898
|
|
|
2956
2899
|
|
|
2957
|
-
<a id="
|
|
2900
|
+
<a id="org73344ae"></a>
|
|
2958
2901
|
|
|
2959
2902
|
# Contributing
|
|
2960
2903
|
|