fat_table 0.9.3 → 0.9.5

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: c938654651af21d509d2a71fe8a7a9c36747f33e04d1be8eab4c020c1400a26e
4
- data.tar.gz: 6d9191d19b79a70a963eb16eaabfaefb2643cd0df7b9dec7a77c5d36f2c836b0
3
+ metadata.gz: 10cc3b0171ec07f74b9e7067c0204c4f828271d18e648bf540664fdaa33b7af9
4
+ data.tar.gz: a95c4ab0fc7bfda4a00df537c2ad06383d67c77eb9b5b5007c9033008459d679
5
5
  SHA512:
6
- metadata.gz: 38371b27911a835c34e7536bc6226b08a20225d399b4feaa6e6bb6e1dbb6031292d10ea6673c4dabb77c2576294188ce6ab6d76186f6d0a8c803a175e2db3907
7
- data.tar.gz: 69a13614617e0423cb7739ec47939841bb96eb5c460d0711528e2163969b70339a14a129a96ed5d68f8908a86479c4ee596799197423cf2cbf3d56f7afe5ba90
6
+ metadata.gz: c3dd880a234f88c9c507b01854397e458b4987786cac9c5667f4697253dc4781b1833976251c95cfdfdd3c4ff6e9d033911e2ba02bcb6d04b92567f1beab8c3b
7
+ data.tar.gz: 6fb2f3309586c0053dc89cddee3509e934943ee70a2ffc105fd1b257eaec1ce81ff3deffce5ae5a389cf4461f716c54d6ae661102530ad4f3624bec34632e74e
@@ -16,4 +16,7 @@ module FatTable
16
16
  # Raised when an external resource is not available due to caller or
17
17
  # programmer error or some failure of the external resource to be available.
18
18
  class TransientError < StandardError; end
19
+
20
+ # Raise when an expected table was not found.
21
+ class NoTable < UserError; end
19
22
  end
@@ -156,14 +156,18 @@ module FatTable
156
156
  File.open(fname, 'r') do |io|
157
157
  from_csv_io(io, **types)
158
158
  end
159
+ rescue NoTable
160
+ raise NoTable, "no table found in CSV file '#{fname}'"
159
161
  end
160
162
 
161
163
  # :category: Constructors
162
164
 
163
165
  # Construct a Table from a CSV string +str+, treated in the same manner as
164
166
  # the input from a CSV file in ::from_org_file.
165
- def self.from_csv_string(str, **types)
166
- from_csv_io(StringIO.new(str), **types)
167
+ def self.from_csv_string(str, has_headers: true, **)
168
+ from_csv_io(StringIO.new(str), has_headers:, **)
169
+ rescue NoTable
170
+ raise NoTable, "no table found in string '#{str[0..20]}...'"
167
171
  end
168
172
 
169
173
  # :category: Constructors
@@ -176,6 +180,8 @@ module FatTable
176
180
  File.open(fname, 'r') do |io|
177
181
  from_org_io(io, **types)
178
182
  end
183
+ rescue NoTable
184
+ raise NoTable, "no table found in file '#{fname}'"
179
185
  end
180
186
 
181
187
  # :category: Constructors
@@ -184,6 +190,8 @@ module FatTable
184
190
  # contents of an org-mode file in ::from_org_file.
185
191
  def self.from_org_string(str, **types)
186
192
  from_org_io(StringIO.new(str), **types)
193
+ rescue NoTable
194
+ raise NoTable, "no table found in string '#{str[0..20]...}'"
187
195
  end
188
196
 
189
197
  # :category: Constructors
@@ -275,9 +283,9 @@ module FatTable
275
283
  # Construct table from an array of hashes or an array of any object that
276
284
  # can respond to #to_h. If an array element is a nil, mark it as a group
277
285
  # boundary in the Table.
278
- def from_array_of_hashes(hashes, hlines: false, **types)
286
+ def from_array_of_hashes(hashes, hlines: false, **)
279
287
  heads = hashes.first.keys
280
- result = new(*heads, **types)
288
+ result = new(*heads, **)
281
289
  hashes.each do |hsh|
282
290
  if hsh.nil?
283
291
  unless hlines
@@ -305,7 +313,7 @@ module FatTable
305
313
  # hlines are stripped from the table, otherwise (:hlines yes) they are
306
314
  # indicated with nil elements in the outer array as expected by this
307
315
  # method when hlines is set true.
308
- def from_array_of_arrays(rows, hlines: false, **types)
316
+ def from_array_of_arrays(rows, hlines: false, **)
309
317
  headers = []
310
318
  if !hlines
311
319
  # Take the first row as headers
@@ -324,7 +332,7 @@ module FatTable
324
332
  headers = (1..rows[0].size).to_a.map { |k| "col_#{k}".as_sym }
325
333
  first_data_row = 0
326
334
  end
327
- result = new(*headers, **types)
335
+ result = new(*headers, **)
328
336
  rows[first_data_row..-1].each do |row|
329
337
  if row.nil?
330
338
  unless hlines
@@ -342,24 +350,35 @@ module FatTable
342
350
  result
343
351
  end
344
352
 
345
- def from_csv_io(io, **types)
346
- result = new(**types)
347
- ::CSV.new(io, headers: true, header_converters: :symbol,
348
- skip_blanks: true).each do |row|
349
- result << row.to_h
353
+ def from_csv_io(io, has_headers: true, **)
354
+ result = new(**)
355
+ if has_headers
356
+ ::CSV.new(io, headers: has_headers, header_converters: :symbol, skip_blanks: true).each do |row|
357
+ result << row.to_h
358
+ end
359
+ else
360
+ nfields = io.readline.split(',').size
361
+ io.seek(0, IO::SEEK_SET)
362
+ heads = (1..nfields).map {|n| "col_#{n}"}
363
+ ::CSV.new(io, headers: heads, skip_blanks: true).each do |row|
364
+ result << row.to_h
365
+ end
350
366
  end
351
367
  result.normalize_boundaries
352
368
  result
369
+ rescue StandardError
370
+ raise NoTable
353
371
  end
354
372
 
355
373
  # Form rows of table by reading the first table found in the org file. The
356
374
  # header row must be marked with an hline (i.e, a row that looks like
357
375
  # '|---+--...--|') and groups of rows may be marked with hlines to
358
376
  # indicate group boundaries.
359
- def from_org_io(io, **types)
377
+ def from_org_io(io, **)
360
378
  table_re = /\A\s*\|/
361
379
  hrule_re = /\A\s*\|[-+]+/
362
380
  rows = []
381
+ ncols = nil
363
382
  table_found = false
364
383
  header_found = false
365
384
  io.each do |line|
@@ -388,10 +407,17 @@ module FatTable
388
407
  break
389
408
  else
390
409
  line = line.sub(/\A\s*\|/, '').sub(/\|\s*\z/, '')
391
- rows << line.split('|').map(&:clean)
410
+ # Don't include any rows with a size different from that
411
+ # established by the header row.
412
+ cols = line.split('|').map(&:clean)
413
+ ncols ||= cols.size
414
+ next unless cols.size == ncols
415
+
416
+ rows << cols
392
417
  end
393
418
  end
394
- from_array_of_arrays(rows, hlines: true, **types)
419
+ raise NoTable unless table_found
420
+ from_array_of_arrays(rows, hlines: true, **)
395
421
  end
396
422
  end
397
423
 
@@ -717,6 +743,7 @@ module FatTable
717
743
  self.explicit_boundaries = explicit_boundaries.uniq.sort
718
744
  end
719
745
  explicit_boundaries
746
+ self
720
747
  end
721
748
 
722
749
  # Return the explicit_boundaries, augmented by an implicit boundary for
@@ -2,5 +2,5 @@
2
2
 
3
3
  module FatTable
4
4
  # The current version of FatTable
5
- VERSION = '0.9.3'
5
+ VERSION = '0.9.5'
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.9.3
4
+ version: 0.9.5
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-05-24 00:00:00.000000000 Z
11
+ date: 2023-10-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -335,7 +335,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
335
335
  - !ruby/object:Gem::Version
336
336
  version: '0'
337
337
  requirements: []
338
- rubygems_version: 3.4.1
338
+ rubygems_version: 3.4.14
339
339
  signing_key:
340
340
  specification_version: 4
341
341
  summary: Provides tools for working with tables as a data type.