fat_table 0.9.9 → 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 +161 -0
- data/.gitignore +5 -1
- data/.rubocop.yml +9 -2
- data/.simplecov +2 -0
- data/.yardopts +4 -5
- data/CHANGELOG.md +319 -0
- data/CHANGELOG.org +151 -0
- data/Gemfile +7 -8
- data/README.md +2904 -0
- data/README.org +1317 -1050
- data/Rakefile +5 -0
- data/bin/ft_console +1 -0
- data/fat_table.gemspec +2 -0
- data/lib/{ext → core_ext}/array.rb +3 -1
- data/lib/core_ext/numeric_string.rb +194 -0
- data/lib/core_ext.rb +4 -0
- data/lib/fat_table/column.rb +5 -1
- data/lib/fat_table/convert.rb +2 -0
- data/lib/fat_table/evaluator.rb +1 -2
- data/lib/fat_table/footer.rb +1 -1
- data/lib/fat_table/formatters/formatter.rb +27 -54
- data/lib/fat_table/formatters/latex_formatter.rb +2 -2
- data/lib/fat_table/table.rb +13 -11
- data/lib/fat_table/version.rb +1 -1
- data/lib/fat_table.rb +51 -26
- metadata +11 -14
- data/examples/create_trans.sql +0 -14
- data/examples/quick.pdf +0 -0
- data/examples/quick.png +0 -0
- data/examples/quick.ppm +0 -0
- data/examples/quick.tex +0 -8
- data/examples/quick_small.png +0 -0
- data/examples/quicktable.tex +0 -123
- data/examples/trades.db +0 -0
- data/examples/trans.csv +0 -13
- /data/{lib/fat_table/formatters → data}/xcolors.txt +0 -0
data/Rakefile
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'bundler/gem_tasks'
|
|
2
4
|
require 'rspec/core/rake_task'
|
|
3
5
|
require 'rdoc/task'
|
|
@@ -13,4 +15,7 @@ end
|
|
|
13
15
|
|
|
14
16
|
RSpec::Core::RakeTask.new(:spec)
|
|
15
17
|
|
|
18
|
+
require "gem_docs"
|
|
19
|
+
GemDocs.install
|
|
20
|
+
|
|
16
21
|
task :default => [:spec, :rubocop]
|
data/bin/ft_console
CHANGED
data/fat_table.gemspec
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
class Array
|
|
2
4
|
# Map booleans true to 1 and false to 0 so they can be compared in a sort
|
|
3
5
|
# with the <=> operator.
|
|
@@ -20,7 +22,7 @@ class Array
|
|
|
20
22
|
compact.select { |i| i.is_a?(Date) || i.is_a?(DateTime) || i.is_a?(Time) }
|
|
21
23
|
.map(&:to_datetime)
|
|
22
24
|
elsif typ == 'Numeric'
|
|
23
|
-
compact.
|
|
25
|
+
compact.grep(Numeric)
|
|
24
26
|
elsif typ == 'String'
|
|
25
27
|
map(&:to_s)
|
|
26
28
|
elsif typ == 'NilClass'
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# This refinement contains methods that extend the String class to massage
|
|
4
|
+
# strings that represent numbers, such as adding commas, pre- and
|
|
5
|
+
# post-padding, etc.
|
|
6
|
+
module NumericString
|
|
7
|
+
# You can control how NumericString behaves by supplying a new
|
|
8
|
+
# NumericString::Config to the config: parameter of these methods. Calling
|
|
9
|
+
# `NumericString::Config.build` gives you the deafult configuration with
|
|
10
|
+
# whatever overrides you give it. You can also modify an existing config
|
|
11
|
+
# with some overrides. For example:
|
|
12
|
+
#
|
|
13
|
+
# #+begin_src ruby
|
|
14
|
+
# cfg = NumericString::Config.build(group_size: 4)
|
|
15
|
+
# cfg2 = cfg.with(group_char: '_')
|
|
16
|
+
#
|
|
17
|
+
# "1234567.89".add_grouping(config: cfg)
|
|
18
|
+
# => "123,4567.89"
|
|
19
|
+
# "1234567.89".add_grouping(config: cfg2)
|
|
20
|
+
# => '123_4567.89'
|
|
21
|
+
# #+end_src
|
|
22
|
+
Config = Struct.new(
|
|
23
|
+
:group_char,
|
|
24
|
+
:group_size,
|
|
25
|
+
:decimal_char,
|
|
26
|
+
:currency_symbol,
|
|
27
|
+
:pre_pad_char,
|
|
28
|
+
:post_pad_char,
|
|
29
|
+
keyword_init: true,
|
|
30
|
+
) do
|
|
31
|
+
DEFAULTS = {
|
|
32
|
+
group_char: ',',
|
|
33
|
+
group_size: 3,
|
|
34
|
+
decimal_char: '.',
|
|
35
|
+
currency_symbol: '$',
|
|
36
|
+
pre_pad_char: '0',
|
|
37
|
+
post_pad_char: '0',
|
|
38
|
+
}.freeze
|
|
39
|
+
|
|
40
|
+
def self.default
|
|
41
|
+
@default ||= new(**DEFAULTS).freeze
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Build from defaults, overriding selectively
|
|
45
|
+
def self.build(**overrides)
|
|
46
|
+
new(**DEFAULTS.merge(overrides)).freeze
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Clone-with-changes (very Ruby, very nice)
|
|
50
|
+
def with(**overrides)
|
|
51
|
+
self.class.build(
|
|
52
|
+
group_char: overrides.fetch(:group_char, group_char),
|
|
53
|
+
group_size: overrides.fetch(:group_size, group_size),
|
|
54
|
+
decimal_char: overrides.fetch(:decimal_char, decimal_char),
|
|
55
|
+
currency_symbol: overrides.fetch(:currency_symbol, currency_symbol),
|
|
56
|
+
pre_pad_char: overrides.fetch(:pre_pad_char, pre_pad_char),
|
|
57
|
+
post_pad_char: overrides.fetch(:post_pad_char, post_pad_char),
|
|
58
|
+
)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
refine String do
|
|
63
|
+
# If self is a valid decimal number, add grouping commas to the whole
|
|
64
|
+
# part, retaining any fractional part and currency symbol undisturbed.
|
|
65
|
+
# The optional cond: parameter can contain a test to determine if the
|
|
66
|
+
# grouping ought to be performed. If (1) self is not a valid decimal
|
|
67
|
+
# number string, (2) the whole part already contains grouping characters,
|
|
68
|
+
# or (3) cond: is falsey, return self.
|
|
69
|
+
def add_grouping(cond: true, config: Config.default)
|
|
70
|
+
return self unless cond
|
|
71
|
+
return self unless valid_num?(config:)
|
|
72
|
+
|
|
73
|
+
cur, whole, frac = cur_whole_frac(config:)
|
|
74
|
+
return self if whole.include?(config.group_char)
|
|
75
|
+
|
|
76
|
+
whole = whole.split('').reverse
|
|
77
|
+
.each_slice(config.group_size).to_a
|
|
78
|
+
.map { |a| a.reverse.join }
|
|
79
|
+
.reverse
|
|
80
|
+
.join(config.group_char)
|
|
81
|
+
cur + whole + frac
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
alias_method :add_commas, :add_grouping
|
|
85
|
+
|
|
86
|
+
# If self is a valid decimal number, add the currency symbol (per
|
|
87
|
+
# NumericString::Config.currency_symbol) to the front of the number
|
|
88
|
+
# string, retaining any grouping characters undisturbed. The optional
|
|
89
|
+
# cond: parameter can contain a test to determine if the currency symbol
|
|
90
|
+
# ought to be pre-pended. If (1) self is not a valid decimal number
|
|
91
|
+
# string, (2) the currency symbol is already present, or (3) cond: is
|
|
92
|
+
# falsey, return self.
|
|
93
|
+
def add_currency(cond: true, config: Config.default)
|
|
94
|
+
return self unless cond
|
|
95
|
+
return self unless valid_num?(config:)
|
|
96
|
+
|
|
97
|
+
md = match(num_re(config:))
|
|
98
|
+
return self unless md[:cur].blank?
|
|
99
|
+
|
|
100
|
+
config.currency_symbol + self
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def add_pre_digits(n, cond: true, config: Config.default)
|
|
104
|
+
return self unless cond
|
|
105
|
+
return self if n <= 0
|
|
106
|
+
return self unless valid_num?(config:)
|
|
107
|
+
|
|
108
|
+
cur, whole, frac = cur_whole_frac(config:)
|
|
109
|
+
n_pads = [n - whole.delete(config.group_char).size, 0].max
|
|
110
|
+
padding = config.pre_pad_char * n_pads
|
|
111
|
+
"#{cur}#{padding}#{whole}#{frac}"
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def add_post_digits(n, cond: true, config: Config.default)
|
|
115
|
+
return self unless cond
|
|
116
|
+
return self unless valid_num?(config:)
|
|
117
|
+
|
|
118
|
+
cur, whole, frac = cur_whole_frac(config:)
|
|
119
|
+
frac_digs = frac.size - 1 # frac includes the decimal character
|
|
120
|
+
if n >= frac_digs
|
|
121
|
+
n_pads = [n - frac_digs, 0].max
|
|
122
|
+
padding = config.post_pad_char * n_pads
|
|
123
|
+
"#{cur}#{whole}#{frac}#{padding}"
|
|
124
|
+
elsif n.zero?
|
|
125
|
+
# Round up last digit of whole if first digit of frac >= 5
|
|
126
|
+
if frac[1].to_i >= 5
|
|
127
|
+
whole = whole[0..-2] + (whole[-1].to_i + 1).to_s
|
|
128
|
+
end
|
|
129
|
+
# No fractional part
|
|
130
|
+
"#{cur}#{whole}"
|
|
131
|
+
elsif n.negative?
|
|
132
|
+
# This calls for rounding the whole part to nearest 10^n.abs and
|
|
133
|
+
# dropping the frac part.
|
|
134
|
+
ndigs_in_whole = whole.delete(config.group_char).size
|
|
135
|
+
nplaces = [ndigs_in_whole - 1, n.abs].min
|
|
136
|
+
# Replace the right-most nplaces digs with the pre-pad character.
|
|
137
|
+
replace_count = 0
|
|
138
|
+
new_whole = +''
|
|
139
|
+
round_char = whole.delete(config.group_char)[-1]
|
|
140
|
+
rounded = false
|
|
141
|
+
whole.split('').reverse_each do |c|
|
|
142
|
+
if c == config.group_char
|
|
143
|
+
new_whole << c
|
|
144
|
+
elsif replace_count < nplaces
|
|
145
|
+
new_whole << config.pre_pad_char
|
|
146
|
+
round_char = c
|
|
147
|
+
replace_count += 1
|
|
148
|
+
elsif !rounded
|
|
149
|
+
new_whole <<
|
|
150
|
+
if round_char.to_i >= 5
|
|
151
|
+
(c.to_i + 1).to_s
|
|
152
|
+
else
|
|
153
|
+
c
|
|
154
|
+
end
|
|
155
|
+
rounded = true
|
|
156
|
+
else
|
|
157
|
+
new_whole << c
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
"#{cur}#{new_whole.reverse}"
|
|
161
|
+
else
|
|
162
|
+
# We have to shorten the fractional part, which required rounding.
|
|
163
|
+
last_frac_dig = frac[n]
|
|
164
|
+
following_frac_dig = frac[n + 1]
|
|
165
|
+
if following_frac_dig.to_i >= 5
|
|
166
|
+
last_frac_dig = (last_frac_dig.to_i + 1).to_s
|
|
167
|
+
end
|
|
168
|
+
frac = frac[0..(n - 1)] + last_frac_dig
|
|
169
|
+
padding = ''
|
|
170
|
+
"#{cur}#{whole}#{frac}#{padding}"
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
private
|
|
175
|
+
|
|
176
|
+
def num_re(config: Config.default)
|
|
177
|
+
cur_sym = Regexp.quote(config.currency_symbol)
|
|
178
|
+
grp_char = Regexp.quote(config.group_char)
|
|
179
|
+
dec_char = Regexp.quote(config.decimal_char)
|
|
180
|
+
/\A(?<cur>#{cur_sym})?(?<whole>[0-9#{grp_char}]+)(?<frac>#{dec_char}[0-9]*)?\z/
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
# Return the currency, whole and fractional parts of a string with a possible
|
|
184
|
+
# decimal point attached to the frac part if present.
|
|
185
|
+
def cur_whole_frac(config: Config.default)
|
|
186
|
+
match = match(num_re(config:))
|
|
187
|
+
[match[:cur].to_s, match[:whole].to_s, match[:frac].to_s]
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def valid_num?(config: Config.default)
|
|
191
|
+
match?(num_re(config:))
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
end
|
data/lib/core_ext.rb
ADDED
data/lib/fat_table/column.rb
CHANGED
|
@@ -86,7 +86,11 @@ module FatTable
|
|
|
86
86
|
# col.header #=> :prices
|
|
87
87
|
# col.sum #=> 18376.75
|
|
88
88
|
#
|
|
89
|
-
# @param
|
|
89
|
+
# @param header [String, Symbol] the name of the column header
|
|
90
|
+
# @param items [Array<String>, Array<DateTime>, Array<Numeric>, Array<Boolean>] the initial data items in column
|
|
91
|
+
# @param type [String] the column type: 'String', 'Numeric', 'DateTime', 'Boolean', or 'NilClass'
|
|
92
|
+
# @param tolerant [Boolean] whether the column accepts unconvertable items not of its type as Strings
|
|
93
|
+
# @return [Column] the new Column
|
|
90
94
|
def initialize(header:, items: [], type: 'NilClass', tolerant: false)
|
|
91
95
|
@raw_header = header
|
|
92
96
|
@header =
|
data/lib/fat_table/convert.rb
CHANGED
data/lib/fat_table/evaluator.rb
CHANGED
|
@@ -51,13 +51,12 @@ module FatTable
|
|
|
51
51
|
# when a string gets into an otherwise numeric column because the column
|
|
52
52
|
# is set to tolerant.
|
|
53
53
|
#
|
|
54
|
-
# rubocop:disable Security/Eval
|
|
54
|
+
# rubocop:disable-next Security/Eval
|
|
55
55
|
def evaluate(expr = '', locals: {})
|
|
56
56
|
eval(expr, local_vars(binding, locals))
|
|
57
57
|
rescue NoMethodError, TypeError
|
|
58
58
|
nil
|
|
59
59
|
end
|
|
60
|
-
# rubocop:enable Security/Eval
|
|
61
60
|
|
|
62
61
|
private
|
|
63
62
|
|
data/lib/fat_table/footer.rb
CHANGED
|
@@ -67,7 +67,7 @@ module FatTable
|
|
|
67
67
|
bgcolor: 'none',
|
|
68
68
|
hms: false,
|
|
69
69
|
pre_digits: 0,
|
|
70
|
-
post_digits:
|
|
70
|
+
post_digits: 4,
|
|
71
71
|
commas: false,
|
|
72
72
|
currency: false,
|
|
73
73
|
datetime_fmt: '%F %H:%M:%S',
|
|
@@ -1026,63 +1026,37 @@ module FatTable
|
|
|
1026
1026
|
# above. Only device-independent formatting is done here. Device dependent
|
|
1027
1027
|
# formatting (e.g., color) can be done in a subclass of Formatter by
|
|
1028
1028
|
# specializing this method.
|
|
1029
|
+
|
|
1030
|
+
using NumericString
|
|
1031
|
+
|
|
1029
1032
|
def format_numeric(val, istruct)
|
|
1030
1033
|
return istruct[:nil_text] if val.nil?
|
|
1031
1034
|
return val.secs_to_hms if istruct[:hms]
|
|
1032
1035
|
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
if n_zeros.positive?
|
|
1055
|
-
if istruct[:commas]
|
|
1056
|
-
# Insert leading zeros with commas
|
|
1057
|
-
pre_comma_match = whole_part.match(/\A(\d+),/)
|
|
1058
|
-
if pre_comma_match
|
|
1059
|
-
n_partial_zeros = 3 - pre_comma_match[1].size
|
|
1060
|
-
whole_part = "0" * n_partial_zeros + whole_part
|
|
1061
|
-
n_zeros -= n_partial_zeros
|
|
1062
|
-
end
|
|
1063
|
-
zeros = ''
|
|
1064
|
-
if n_zeros.positive?
|
|
1065
|
-
zeros = "0" * n_zeros
|
|
1066
|
-
if n_zeros > 3 && istruct[:commas]
|
|
1067
|
-
zeros = zeros.reverse.gsub!(/([0-9]{3})/, "\\1,").reverse
|
|
1068
|
-
end
|
|
1069
|
-
end
|
|
1070
|
-
"#{zeros},#{whole_part}#{frac_part}"
|
|
1071
|
-
else
|
|
1072
|
-
# Insert leading zeros without commas
|
|
1073
|
-
zeros = "0" * n_zeros
|
|
1074
|
-
"#{zeros}#{whole_part}#{frac_part}"
|
|
1075
|
-
end
|
|
1076
|
-
else
|
|
1077
|
-
"#{whole_part}#{frac_part}"
|
|
1078
|
-
end
|
|
1036
|
+
case val
|
|
1037
|
+
when Integer
|
|
1038
|
+
val.to_s
|
|
1039
|
+
.add_pre_digits(istruct[:pre_digits], cond: istruct[:pre_digits].positive?)
|
|
1040
|
+
.add_commas(cond: istruct[:commas])
|
|
1041
|
+
.add_currency(cond: istruct[:currency])
|
|
1042
|
+
when Rational
|
|
1043
|
+
num = val.numerator.to_s
|
|
1044
|
+
.add_pre_digits(istruct[:pre_digits], cond: istruct[:pre_digits].positive?)
|
|
1045
|
+
.add_commas(cond: istruct[:commas])
|
|
1046
|
+
.add_currency(cond: istruct[:currency])
|
|
1047
|
+
den = val.denominator.to_s
|
|
1048
|
+
.add_pre_digits(istruct[:pre_digits], cond: istruct[:pre_digits].positive?)
|
|
1049
|
+
.add_commas(cond: istruct[:commas])
|
|
1050
|
+
"#{num}/#{den}"
|
|
1051
|
+
when Float, BigDecimal
|
|
1052
|
+
val.to_s
|
|
1053
|
+
.add_pre_digits(istruct[:pre_digits], cond: istruct[:pre_digits].positive?)
|
|
1054
|
+
.add_post_digits(istruct[:post_digits])
|
|
1055
|
+
.add_commas(cond: istruct[:commas])
|
|
1056
|
+
.add_currency(cond: istruct[:currency])
|
|
1079
1057
|
else
|
|
1080
|
-
|
|
1081
|
-
end
|
|
1082
|
-
if istruct[:currency]
|
|
1083
|
-
result = "#{FatTable.currency_symbol}#{result}"
|
|
1058
|
+
raise ArgumentError, "cannot apply format_numeric to #{val} of class #{val.class}"
|
|
1084
1059
|
end
|
|
1085
|
-
result
|
|
1086
1060
|
end
|
|
1087
1061
|
|
|
1088
1062
|
# Apply non-device-dependent string formatting instructions.
|
|
@@ -1234,9 +1208,8 @@ module FatTable
|
|
|
1234
1208
|
# If this Formatter targets a ruby data structure (e.g., AoaFormatter), we
|
|
1235
1209
|
# eval the string to get the object.
|
|
1236
1210
|
#
|
|
1237
|
-
# rubocop:disable Security/Eval
|
|
1211
|
+
# rubocop:disable-next Security/Eval
|
|
1238
1212
|
evaluate? ? eval(result) : result
|
|
1239
|
-
# rubocop:enable Security/Eval
|
|
1240
1213
|
end
|
|
1241
1214
|
|
|
1242
1215
|
private
|
|
@@ -26,8 +26,8 @@ module FatTable
|
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
# Taken from the Rainbow gem's list of valid colors.
|
|
29
|
-
|
|
30
|
-
self.valid_colors = File.readlines(
|
|
29
|
+
color_path = File.expand_path("../../../data/xcolors.txt", __dir__)
|
|
30
|
+
self.valid_colors = File.readlines(color_path, chomp: true)
|
|
31
31
|
|
|
32
32
|
# LaTeX commands to load the needed packages based on the :environement
|
|
33
33
|
# option. For now, just handles the default 'longtable' :environment. The
|
data/lib/fat_table/table.rb
CHANGED
|
@@ -721,7 +721,7 @@ module FatTable
|
|
|
721
721
|
# row in the table as a group boundary. An attempt to add a boundary to
|
|
722
722
|
# an empty table has no effect. We adopt the convention that the last row
|
|
723
723
|
# of the table always marks an implicit boundary even if it is not in the
|
|
724
|
-
#
|
|
724
|
+
# `@explicit_boundaries` array. When we "mark" a boundary, we intend it to
|
|
725
725
|
# be an explicit boundary, even if it marks the last row of the table.
|
|
726
726
|
def mark_boundary(row_num = nil)
|
|
727
727
|
return self if empty?
|
|
@@ -774,7 +774,7 @@ module FatTable
|
|
|
774
774
|
# user's point of view are indexed starting at 0.
|
|
775
775
|
def row_index_to_group_index(row_num)
|
|
776
776
|
boundaries.each_with_index do |b_last, g_num|
|
|
777
|
-
return
|
|
777
|
+
return g_num + 1 if row_num <= b_last
|
|
778
778
|
end
|
|
779
779
|
0
|
|
780
780
|
end
|
|
@@ -821,6 +821,7 @@ module FatTable
|
|
|
821
821
|
# Return a new Table sorting the rows of this Table on the possibly multiple
|
|
822
822
|
# keys given in +sort_heads+ as an Array of Symbols. Append a ! to the
|
|
823
823
|
# symbol name to indicate reverse sorting on that column.
|
|
824
|
+
# Rows with equal sort keys retain their original order in either direction.
|
|
824
825
|
#
|
|
825
826
|
# tab.order_by(:ref, :date) => sorted table
|
|
826
827
|
# tab.order_by(:date!) => reverse sort on :date
|
|
@@ -830,7 +831,7 @@ module FatTable
|
|
|
830
831
|
def order_by(*sort_heads)
|
|
831
832
|
# Sort the rows in order and add to new_rows.
|
|
832
833
|
key_hash = partition_sort_keys(sort_heads)
|
|
833
|
-
new_rows = rows.sort
|
|
834
|
+
new_rows = rows.each_with_index.sort { |(r1, index1), (r2, index2)|
|
|
834
835
|
# Set the sort keys based on direction
|
|
835
836
|
key1 = []
|
|
836
837
|
key2 = []
|
|
@@ -850,8 +851,9 @@ module FatTable
|
|
|
850
851
|
# If there are any nils, <=> will return nil, and we have to use the
|
|
851
852
|
# special comparison method, compare_with_nils, instead.
|
|
852
853
|
result = (key1 <=> key2)
|
|
853
|
-
result
|
|
854
|
-
|
|
854
|
+
result = compare_with_nils(key1, key2) if result.nil?
|
|
855
|
+
result.zero? ? index1 <=> index2 : result
|
|
856
|
+
}.map(&:first)
|
|
855
857
|
|
|
856
858
|
# Add the new_rows to the table, but mark a group boundary at the points
|
|
857
859
|
# where the sort key changes value. NB: I use self.class.new here
|
|
@@ -918,8 +920,8 @@ module FatTable
|
|
|
918
920
|
# access the instance variable @row, as the row number of the row being
|
|
919
921
|
# evaluated, and @group, as the group number of the row being evaluated.
|
|
920
922
|
#
|
|
921
|
-
# 4. a hash in +new_cols+ with one of the special keys,
|
|
922
|
-
# hash}
|
|
923
|
+
# 4. a hash in +new_cols+ with one of the special keys, `+ivars: {literal
|
|
924
|
+
# hash}+`, +before_hook: 'ruby-code'+, or +after_hook: 'ruby-code'+ for
|
|
923
925
|
# defining custom instance variables to be used during evaluation of
|
|
924
926
|
# parameters described in point 3 and hooks of ruby code snippets to be
|
|
925
927
|
# evaluated before and after processing each row.
|
|
@@ -1150,7 +1152,7 @@ module FatTable
|
|
|
1150
1152
|
# exception will be thrown. Duplicates are eliminated from the result. Any
|
|
1151
1153
|
# groups present in either Table are eliminated in the output Table.
|
|
1152
1154
|
def intersect(other)
|
|
1153
|
-
set_operation(other,
|
|
1155
|
+
set_operation(other, :&, distinct: true)
|
|
1154
1156
|
end
|
|
1155
1157
|
|
|
1156
1158
|
# :category: Operators
|
|
@@ -1163,7 +1165,7 @@ module FatTable
|
|
|
1163
1165
|
# will be thrown. Duplicates are not eliminated from the result. Resets
|
|
1164
1166
|
# groups.
|
|
1165
1167
|
def intersect_all(other)
|
|
1166
|
-
set_operation(other,
|
|
1168
|
+
set_operation(other, :&, distinct: false)
|
|
1167
1169
|
end
|
|
1168
1170
|
|
|
1169
1171
|
# :category: Operators
|
|
@@ -1176,7 +1178,7 @@ module FatTable
|
|
|
1176
1178
|
# are eliminated from the result. Any groups present in either Table are
|
|
1177
1179
|
# eliminated in the output Table.
|
|
1178
1180
|
def except(other)
|
|
1179
|
-
set_operation(other,
|
|
1181
|
+
set_operation(other, :-, distinct: true)
|
|
1180
1182
|
end
|
|
1181
1183
|
|
|
1182
1184
|
# :category: Operators
|
|
@@ -1189,7 +1191,7 @@ module FatTable
|
|
|
1189
1191
|
# are /not/ eliminated from the result. Any groups present in either Table
|
|
1190
1192
|
# are eliminated in the output Table.
|
|
1191
1193
|
def except_all(other)
|
|
1192
|
-
set_operation(other,
|
|
1194
|
+
set_operation(other, :-, distinct: false)
|
|
1193
1195
|
end
|
|
1194
1196
|
|
|
1195
1197
|
# An Array of symbols for the valid join types.
|
data/lib/fat_table/version.rb
CHANGED
data/lib/fat_table.rb
CHANGED
|
@@ -1,11 +1,36 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
#
|
|
4
|
-
#
|
|
5
|
-
#
|
|
6
|
-
#
|
|
7
|
-
# variety of
|
|
8
|
-
#
|
|
3
|
+
# Gem Overview (extracted from README.org by gem_docs)
|
|
4
|
+
#
|
|
5
|
+
# * Introduction
|
|
6
|
+
# ~FatTable~ is a gem that treats tables as a data type. It provides methods for
|
|
7
|
+
# constructing tables from a variety of sources, building them row-by-row,
|
|
8
|
+
# extracting rows, columns, and cells, and performing aggregate operations on
|
|
9
|
+
# columns. It also provides a set of SQL-esque methods for manipulating table
|
|
10
|
+
# objects: ~select~ for filtering by columns or for creating new columns, ~where~
|
|
11
|
+
# for filtering by rows, ~order_by~ for sorting rows, ~distinct~ for eliminating
|
|
12
|
+
# duplicate rows, ~group_by~ for aggregating multiple rows into single rows and
|
|
13
|
+
# applying column aggregate methods to ungrouped columns, a collection of ~join~
|
|
14
|
+
# methods for combining tables, and more.
|
|
15
|
+
#
|
|
16
|
+
# Furthermore, ~FatTable~ provides methods for formatting tables and producing
|
|
17
|
+
# output that targets various output media: text, ANSI terminals, ruby data
|
|
18
|
+
# structures, LaTeX tables, Emacs org-mode tables, and more. The formatting
|
|
19
|
+
# methods can specify cell formatting in a way that is uniform across all the
|
|
20
|
+
# output methods and can also decorate the output with any number of footers,
|
|
21
|
+
# including group footers. ~FatTable~ applies formatting directives to the extent
|
|
22
|
+
# they makes sense for the output medium and treats other formatting directives as
|
|
23
|
+
# no-ops.
|
|
24
|
+
#
|
|
25
|
+
# ~FatTable~ can be used to perform operations on data that are naturally best
|
|
26
|
+
# conceived of as tables, which in my experience is quite often. It can also
|
|
27
|
+
# serve as a foundation for providing reporting functions where flexibility
|
|
28
|
+
# about the output medium can be useful. Finally ~FatTable~ can be used within
|
|
29
|
+
# Emacs ~org-mode~ files in code blocks targeting the Ruby language. Org mode
|
|
30
|
+
# tables are presented to a ruby code block as an array of arrays, so ~FatTable~
|
|
31
|
+
# can read them in with its ~.from_aoa~ constructor. A ~FatTable~ table output as an
|
|
32
|
+
# array of arrays with its ~.to_aoa~ output function will be rendered in an
|
|
33
|
+
# org-mode buffer as an org-table, ready for processing by other code blocks.
|
|
9
34
|
module FatTable
|
|
10
35
|
require 'fat_core/symbol'
|
|
11
36
|
require 'fat_core/array'
|
|
@@ -18,9 +43,9 @@ module FatTable
|
|
|
18
43
|
require 'active_support/core_ext'
|
|
19
44
|
require 'active_support/number_helper'
|
|
20
45
|
|
|
46
|
+
require 'core_ext'
|
|
21
47
|
require 'fat_table/version'
|
|
22
48
|
require 'fat_table/patches'
|
|
23
|
-
require 'ext/array'
|
|
24
49
|
require 'fat_table/evaluator'
|
|
25
50
|
require 'fat_table/convert'
|
|
26
51
|
require 'fat_table/column'
|
|
@@ -145,11 +170,11 @@ module FatTable
|
|
|
145
170
|
# methods can be called. If no block is given, the default format for the type
|
|
146
171
|
# will be used. The +options+ are passed along to the FatTable::Formatter
|
|
147
172
|
# created to process the output.
|
|
148
|
-
def self.to_format(table, options
|
|
173
|
+
def self.to_format(table, **options) # :yields: formatter
|
|
149
174
|
if block_given?
|
|
150
|
-
to_any(format, table, options, &Proc.new)
|
|
175
|
+
to_any(format, table, **options, &Proc.new)
|
|
151
176
|
else
|
|
152
|
-
to_any(format, table, options)
|
|
177
|
+
to_any(format, table, **options)
|
|
153
178
|
end
|
|
154
179
|
end
|
|
155
180
|
|
|
@@ -159,15 +184,15 @@ module FatTable
|
|
|
159
184
|
# +FatTable::Formatter+ of the appropriate type on which formatting and footer
|
|
160
185
|
# methods can be called. If no block is given, the default format for the
|
|
161
186
|
# +fmt+ type will be used.
|
|
162
|
-
def self.to_any(fmt, table, options
|
|
187
|
+
def self.to_any(fmt, table, **options)
|
|
163
188
|
fmt = fmt.as_sym
|
|
164
189
|
raise UserError, "unknown format '#{fmt}'" unless FORMATS.include?(fmt)
|
|
165
190
|
|
|
166
191
|
method = "to_#{fmt}"
|
|
167
192
|
if block_given?
|
|
168
|
-
send(method, table, options, &Proc.new)
|
|
193
|
+
send(method, table, **options, &Proc.new)
|
|
169
194
|
else
|
|
170
|
-
send(method, table, options)
|
|
195
|
+
send(method, table, **options)
|
|
171
196
|
end
|
|
172
197
|
end
|
|
173
198
|
|
|
@@ -176,7 +201,7 @@ module FatTable
|
|
|
176
201
|
# default formatting is applied to the +table+'s cells. If a block is given,
|
|
177
202
|
# it yields the +FatTable::Formatter+ to the block on which formatting
|
|
178
203
|
# and footer methods can be called.
|
|
179
|
-
def self.to_psv(table, options
|
|
204
|
+
def self.to_psv(table, **options)
|
|
180
205
|
fmt = Formatter.new(table, options)
|
|
181
206
|
yield fmt if block_given?
|
|
182
207
|
fmt.output
|
|
@@ -186,8 +211,8 @@ module FatTable
|
|
|
186
211
|
# default formatting is applies to the table's cells. If a block is given, it
|
|
187
212
|
# yields an AoaFormatter to the block to which formatting instructions and
|
|
188
213
|
# footers can be added by calling methods on it.
|
|
189
|
-
def self.to_aoa(table, options
|
|
190
|
-
fmt = AoaFormatter.new(table, options)
|
|
214
|
+
def self.to_aoa(table, **options)
|
|
215
|
+
fmt = AoaFormatter.new(table, **options)
|
|
191
216
|
yield fmt if block_given?
|
|
192
217
|
fmt.output
|
|
193
218
|
end
|
|
@@ -197,8 +222,8 @@ module FatTable
|
|
|
197
222
|
# table. If no block is given, default formatting is applies to the table's
|
|
198
223
|
# cells. If a block is given, it yields an AohFormatter to the block to which
|
|
199
224
|
# formatting instructions and footers can be added by calling methods on it.
|
|
200
|
-
def self.to_aoh(table, options
|
|
201
|
-
fmt = AohFormatter.new(table, options)
|
|
225
|
+
def self.to_aoh(table, **options)
|
|
226
|
+
fmt = AohFormatter.new(table, **options)
|
|
202
227
|
yield fmt if block_given?
|
|
203
228
|
fmt.output
|
|
204
229
|
end
|
|
@@ -207,8 +232,8 @@ module FatTable
|
|
|
207
232
|
# default formatting applies to the table's cells. If a block is given, it
|
|
208
233
|
# yields a LaTeXFormatter to the block to which formatting instructions and
|
|
209
234
|
# footers can be added by calling methods on it.
|
|
210
|
-
def self.to_latex(table, options
|
|
211
|
-
fmt = LaTeXFormatter.new(table, options)
|
|
235
|
+
def self.to_latex(table, **options)
|
|
236
|
+
fmt = LaTeXFormatter.new(table, **options)
|
|
212
237
|
yield fmt if block_given?
|
|
213
238
|
fmt.output
|
|
214
239
|
end
|
|
@@ -217,8 +242,8 @@ module FatTable
|
|
|
217
242
|
# is given, default formatting applies to the table's cells. If a block is
|
|
218
243
|
# given, it yields a OrgFormatter to the block to which formatting
|
|
219
244
|
# instructions and footers can be added by calling methods on it.
|
|
220
|
-
def self.to_org(table, options
|
|
221
|
-
fmt = OrgFormatter.new(table, options)
|
|
245
|
+
def self.to_org(table, **options)
|
|
246
|
+
fmt = OrgFormatter.new(table, **options)
|
|
222
247
|
yield fmt if block_given?
|
|
223
248
|
fmt.output
|
|
224
249
|
end
|
|
@@ -227,8 +252,8 @@ module FatTable
|
|
|
227
252
|
# table. If no block is given, default formatting applies to the table's
|
|
228
253
|
# cells. If a block is given, it yields a TermFormatter to the block to which
|
|
229
254
|
# formatting instructions and footers can be added by calling methods on it.
|
|
230
|
-
def self.to_term(table, options
|
|
231
|
-
fmt = TermFormatter.new(table, options)
|
|
255
|
+
def self.to_term(table, **options)
|
|
256
|
+
fmt = TermFormatter.new(table, **options)
|
|
232
257
|
yield fmt if block_given?
|
|
233
258
|
fmt.output
|
|
234
259
|
end
|
|
@@ -237,8 +262,8 @@ module FatTable
|
|
|
237
262
|
# no block is given, default formatting applies to the table's cells. If a
|
|
238
263
|
# block is given, it yields a TextFormatter to the block to which formatting
|
|
239
264
|
# instructions and footers can be added by calling methods on it.
|
|
240
|
-
def self.to_text(table, options
|
|
241
|
-
fmt = TextFormatter.new(table, options)
|
|
265
|
+
def self.to_text(table, **options)
|
|
266
|
+
fmt = TextFormatter.new(table, **options)
|
|
242
267
|
yield fmt if block_given?
|
|
243
268
|
fmt.output
|
|
244
269
|
end
|