fat_table 0.8.0 → 0.9.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4c16c57274c9f0f67abf924eeae6bce192f82844366199361ac68a7d9e1fdcf4
4
- data.tar.gz: 42ea485003f76a56f66988a2bf4df159580dbbed022b1f637260ff8e1cd5888b
3
+ metadata.gz: 6511a69dea56479777956084ceae10b33d6769b4e9e8fcbb87d50140fa5475d4
4
+ data.tar.gz: b41a1e1a578f1c9f4f4f82ac7fba1413343ec75d8febda7ebb1f12737b9fb6b2
5
5
  SHA512:
6
- metadata.gz: 32a6d03a5effa045ef6db54654aef86d96e90846cd0002ada80eaf8218c573f8a430067119562b8369226f11bc45d1bd0c144ddb8c1947c7fc9256fca55389b0
7
- data.tar.gz: f0a033ff19d04160b4137f1204df322174926be2a0d7f2c442388a358a8ddc4321f5edcc1670346b9b366aa8a55acbe6a23b6b44f0fa9a69bb038e9eee7d24ac
6
+ metadata.gz: fe1da822b9d2ef6995dbfe665f570a3474d955892e062cba559162a07ef43013e9c1f649040fa47529c780aec213a9d74c4ac0684217a608960ebedac9479843
7
+ data.tar.gz: b4ab503c3f992a471bd72a70822455ac6a50a6a72558242841656d514e2c60c9b51bf7a1d052c66a35ecb1ba4cbce30ac6351d30919aa85f3284f0d9cfad1821
@@ -29,6 +29,8 @@ module FatTable
29
29
  # perhaps after removing nils with +.items.compact+.
30
30
  attr_reader :items
31
31
 
32
+ attr_accessor :tolerant
33
+
32
34
  # Valid Column types as strings.
33
35
  TYPES = %w[NilClass Boolean DateTime Numeric String].freeze
34
36
 
@@ -53,6 +53,7 @@ module FatTable
53
53
  class Table
54
54
  # An Array of FatTable::Columns that constitute the table.
55
55
  attr_reader :columns
56
+ attr_reader :heads
56
57
 
57
58
  # Headers of columns that are to be tolerant when they are built.
58
59
  attr_accessor :tolerant_cols
@@ -95,9 +96,10 @@ module FatTable
95
96
  # starts with 'num', 'dat', 'bool', or 'str' to specify Numeric,
96
97
  # DateTime, Boolean, or String types respectively.
97
98
  def initialize(*heads, **types)
99
+ @heads = heads.flatten.map(&:as_sym)
100
+ @types = types
98
101
  @columns = []
99
102
  @tolerant_cols = []
100
- @headers = []
101
103
  # Check for the special 'omni' key
102
104
  @omni_type = 'NilClass'
103
105
  @omni_tol = false
@@ -110,9 +112,14 @@ module FatTable
110
112
  types.delete(:omni)
111
113
  types.delete('omni')
112
114
  end
113
- heads += types.keys
114
- heads.uniq.each do |h|
115
- typ, tol = Table.typ_tol(types[h])
115
+ # heads += types.keys
116
+ (heads.flatten + types.keys).uniq.each do |h|
117
+ if types[h]
118
+ typ, tol = Table.typ_tol(types[h])
119
+ else
120
+ typ = @omni_type
121
+ tol = @omni_tol
122
+ end
116
123
  @tolerant_cols << h.to_s.as_sym if tol
117
124
  @columns << Column.new(header: h.to_s.sub(/~\s*\z/, ''), type: typ,
118
125
  tolerant: tol)
@@ -122,18 +129,23 @@ module FatTable
122
129
 
123
130
  # :category: Constructors
124
131
 
125
- # Return an empty duplicate of self. This allows the library to create an
126
- # empty table that preserves all the instance variables from self. Even
127
- # though FatTable::Table objects have no instance variables, a class that
128
- # inherits from it might.
129
- def empty_dup
130
- dup.__empty!
131
- end
132
-
133
- def __empty!
134
- @columns = []
135
- @explicit_boundaries = []
136
- self
132
+ # Return an new table based on this Table but with empty columns named by
133
+ # the result_cols parameter, by default the this Table's columns. If any
134
+ # of the result_cols have the same name as an existing column, inherit
135
+ # that column's type and tolerance. Also, set any instance variables that
136
+ # might have been set by a subclass instance.
137
+ def empty_dup(result_cols = nil)
138
+ result_cols ||= heads
139
+ result_types = types.select { |k,_v| result_cols.include?(k) }
140
+ result = Table.new(result_cols, **result_types)
141
+ tolerant_cols.each do |h|
142
+ result.tolerant_cols << h
143
+ result.column(h).tolerant = true
144
+ end
145
+ (instance_variables - result.instance_variables).each do |v|
146
+ result.instance_variable_set(instance_variable_get(v))
147
+ end
148
+ result
137
149
  end
138
150
 
139
151
  # :category: Constructors
@@ -264,7 +276,8 @@ module FatTable
264
276
  # can respond to #to_h. If an array element is a nil, mark it as a group
265
277
  # boundary in the Table.
266
278
  def from_array_of_hashes(hashes, hlines: false, **types)
267
- result = new(**types)
279
+ heads = hashes.first.keys
280
+ result = new(*heads, **types)
268
281
  hashes.each do |hsh|
269
282
  if hsh.nil?
270
283
  unless hlines
@@ -293,7 +306,6 @@ module FatTable
293
306
  # indicated with nil elements in the outer array as expected by this
294
307
  # method when hlines is set true.
295
308
  def from_array_of_arrays(rows, hlines: false, **types)
296
- result = new(**types)
297
309
  headers = []
298
310
  if !hlines
299
311
  # Take the first row as headers
@@ -312,6 +324,7 @@ module FatTable
312
324
  headers = (1..rows[0].size).to_a.map { |k| "col_#{k}".as_sym }
313
325
  first_data_row = 0
314
326
  end
327
+ result = new(*headers, **types)
315
328
  rows[first_data_row..-1].each do |row|
316
329
  if row.nil?
317
330
  unless hlines
@@ -419,6 +432,7 @@ module FatTable
419
432
  def force_string!(*keys)
420
433
  keys.each do |h|
421
434
  raise UserError, "force_string!: #{h} not a column in table" unless column(h)
435
+
422
436
  column(h).force_string!
423
437
  end
424
438
  self
@@ -845,6 +859,7 @@ module FatTable
845
859
  unless expr.is_a?(String)
846
860
  raise "must call FatTable::Table\#order_with with a single string expression"
847
861
  end
862
+
848
863
  rev = false
849
864
  if expr.match?(/\s*!\s*\z/)
850
865
  rev = true
@@ -957,8 +972,15 @@ module FatTable
957
972
  before: before_hook,
958
973
  after: after_hook)
959
974
  # Compute the new Table from this Table
960
- result = empty_dup
975
+ result_cols =
976
+ if cols.include?(:omni)
977
+ (headers + new_cols.keys - [:omni])
978
+ else
979
+ (cols + new_cols.keys)
980
+ end
981
+ result = empty_dup(result_cols)
961
982
  normalize_boundaries
983
+
962
984
  rows.each_with_index do |old_row, old_k|
963
985
  # Set the group number in the before hook and run the hook with the
964
986
  # local variables set to the row before the new row is evaluated.
@@ -1028,15 +1050,6 @@ module FatTable
1028
1050
  def where(expr)
1029
1051
  expr = expr.to_s
1030
1052
  result = empty_dup
1031
- headers.each do |h|
1032
- col =
1033
- if tolerant_col?(h)
1034
- Column.new(header: h, tolerant: true)
1035
- else
1036
- Column.new(header: h)
1037
- end
1038
- result.add_column(col)
1039
- end
1040
1053
  ev = Evaluator.new(ivars: { row: 0, group: 0 })
1041
1054
  rows.each_with_index do |row, k|
1042
1055
  grp = row_index_to_group_index(k)
@@ -1094,10 +1107,13 @@ module FatTable
1094
1107
  # boundaries of the constituent tables. Preserves and adjusts the group
1095
1108
  # boundaries of the constituent table.
1096
1109
  def union_all(other)
1097
- set_operation(other, :+,
1098
- distinct: false,
1099
- add_boundaries: true,
1100
- inherit_boundaries: true)
1110
+ set_operation(
1111
+ other,
1112
+ :+,
1113
+ distinct: false,
1114
+ add_boundaries: true,
1115
+ inherit_boundaries: true
1116
+ )
1101
1117
  end
1102
1118
 
1103
1119
  # :category: Operators
@@ -1489,7 +1505,8 @@ module FatTable
1489
1505
  groups = sorted_tab.rows.group_by do |r|
1490
1506
  group_cols.map { |k| r[k] }
1491
1507
  end
1492
- result = empty_dup
1508
+ grp_types = types.select { |k, _v| group_cols.include?(k) }
1509
+ result = Table.new(*group_cols, **grp_types)
1493
1510
  groups.each_pair do |_vals, grp_rows|
1494
1511
  result << row_from_group(grp_rows, group_cols, agg_cols)
1495
1512
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module FatTable
4
4
  # The current version of FatTable
5
- VERSION = '0.8.0'
5
+ VERSION = '0.9.0'
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fat_table
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel E. Doherty
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-04-20 00:00:00.000000000 Z
11
+ date: 2023-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler