iron-import 0.8.7 → 0.8.8

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
- SHA256:
3
- metadata.gz: e0c9cad49ed06dfb8000477555421a8c109c3d13a112619039372c58ea0e3475
4
- data.tar.gz: 9a686352b1c0158fd87fdcda9c021390e6707508dfdc77a9712f6f8a372c32e5
2
+ SHA1:
3
+ metadata.gz: b0adebf4d34c81c82f83ec23454028316ee1ca4c
4
+ data.tar.gz: f40fc82e0d0fa4bad0930ed86c5de98a31adc0fc
5
5
  SHA512:
6
- metadata.gz: 7c38498e700b3bc7174cb5cc5762c15b390cf0f9dbb83bd5af12a43fa702eed58245c43fdba71b1a201fc0c8bab5c79e3aa287c82bd1074cf1a9844318beb702
7
- data.tar.gz: 24b26fec53dfb0b348413754e66b628aa1008eaf98e6c5fbaacb5b546ba7c3bff969e96657b0866ee5f0dc591f1c4a32f2a2cd9b89e7908eb057e22d7b98209d
6
+ metadata.gz: 92ec6f0156219b3d30a144db3b2260bb9593667c1e030c9922f19d0e36a54e8c7b25944f07c1aab4023525e3c734becd56d56c1bbf1b26cefb8955f7872c058e
7
+ data.tar.gz: 99eeeb1b52b4a2d7eaec2c9186d8900467c66ea31e1057055c5ba54dc21e6e50ae1d8b2f007cbd483610c37fe86a2b3beaf30375ceed646fe19abb921b7259a0
data/History.txt CHANGED
@@ -1,3 +1,8 @@
1
+ == 0.8.8 / 2018-07-23
2
+ * Add error when all rows have been filtered or none were found
3
+ * Add Importer#allow_empty! to suppress same
4
+ * Improve error message when unable to detect file/stream format
5
+
1
6
  == 0.8.7 / 2018-05-03
2
7
  * Improve Importer#read_lines and Importer#import to rewind the stream at end of processing
3
8
 
data/Version.txt CHANGED
@@ -1 +1 @@
1
- 0.8.7
1
+ 0.8.8
@@ -29,14 +29,8 @@ class Importer
29
29
  data = nil
30
30
  if is_stream?(source)
31
31
  data = DataReader::for_stream(importer, source)
32
- unless data
33
- importer.add_error("Unable to find format handler for stream")
34
- end
35
32
  else
36
33
  data = DataReader::for_path(importer, source)
37
- unless data
38
- importer.add_error("Unable to find format handler for file #{source}")
39
- end
40
34
  end
41
35
  data
42
36
  end
@@ -107,6 +107,9 @@ class Importer
107
107
 
108
108
  # When true, skips header detection
109
109
  dsl_flag :headerless
110
+ # When true, does not record an error if all rows are filtered or there are none
111
+ # present at all
112
+ dsl_flag :allow_empty
110
113
  # Explicitly sets the row number (1-indexed) where data rows begin,
111
114
  # usually left defaulted to nil to automatically start after the header
112
115
  # row, or on the first row if #headerless! is set.
@@ -398,7 +401,7 @@ class Importer
398
401
 
399
402
  # Verify we got one
400
403
  unless @reader
401
- add_error("Unable to find format handler for format :#{format} on import of #{path_or_stream.class.name} source - aborting")
404
+ add_error("Unable to find format handler for format :#{format || :auto} on import of #{path_or_stream.class.name} source - aborting")
402
405
  return block ? self : false
403
406
  end
404
407
 
@@ -414,14 +417,20 @@ class Importer
414
417
  # Find our column layout, start of data, etc
415
418
  if find_header(raw_rows)
416
419
  # Now, run all the data and add it as a Row instance
420
+ any = false
417
421
  raw_rows.each_with_index do |raw, index|
418
422
  row_num = index + 1
419
423
  if row_num >= @data.start_row
420
- add_row(row_num, raw)
424
+ any = add_row(row_num, raw) || any
421
425
  end
422
426
  end
423
427
  # We've found a workable sheet/table/whatever, stop looking
424
428
  loaded = true
429
+ # Check to see if it contained any valid rows, add an error in case all rows are filtered
430
+ # or there just aren't any
431
+ unless any || has_errors? || allow_empty?
432
+ add_error("No unfiltered rows found - check your column headers")
433
+ end
425
434
  true
426
435
 
427
436
  else
@@ -431,8 +440,8 @@ class Importer
431
440
  end
432
441
  end
433
442
 
434
- # Verify that we found a working set of rows
435
- unless loaded
443
+ # Verify that we found a working set of rows, add an error if needed if not
444
+ unless has_errors? || loaded
436
445
  # If we have any missing headers, note that fact
437
446
  if @missing_headers && @missing_headers.count > 0
438
447
  add_error("Unable to locate required column header for column(s): " + @missing_headers.collect{|c| ":#{c}"}.list_join(', '))
@@ -511,9 +520,9 @@ class Importer
511
520
  # Your block can access the #rows to do whatever
512
521
  # logging, reporting etc. is desired.
513
522
  def on_success(&block)
514
- raise 'Invalid block passed to Importer#on_succes: block may accept 0 or 1arguments' if block.arity > 1
523
+ raise 'Invalid block passed to Importer#on_success: block may accept 0 or 1 arguments' if block.arity > 1
515
524
 
516
- if @data.rows.any? && !has_errors?
525
+ if !has_errors?
517
526
  case block.arity
518
527
  when 0 then DslProxy.exec(self, &block)
519
528
  when 1 then DslProxy.exec(self, @data.rows, &block)
@@ -526,9 +535,18 @@ class Importer
526
535
  # Call with a block accepting an array of Column objects and returning
527
536
  # true if the columns in the array should constitute a valid header row. Intended
528
537
  # for use with optional columns to define multiple supported column sets, or
529
- # conditionally required secondary columns. Columns will be passed in in the
530
- # order detected, so you can use ordering to help determine which columns are
531
- # required if that helps.
538
+ # conditionally required secondary columns.
539
+ #
540
+ # Columns will be passed in in the order detected, with full Column info, so you can use ordering
541
+ # to help determine which columns are required if that helps.
542
+ #
543
+ # Sample using the simpler #column accessor method:
544
+ #
545
+ # validate_columns do |cols|
546
+ # unless column(:optional_col).present? || column(:other_optional_col).present?
547
+ # add_error('You must include either Optional Col or Other Optional Col!')
548
+ # end
549
+ # end
532
550
  def validate_columns(&block)
533
551
  raise 'Invalid block passed to Importer#validate_columns: block should accept a single argument' if block.arity != 1
534
552
  @column_validator = block
@@ -537,9 +555,9 @@ class Importer
537
555
  # Call with a block accepting a single Row instance. Just like Column#validate, you
538
556
  # can fail by returning false, calling #add_error(msg) or by raising an exception.
539
557
  # The intent of this method of validation is to allow using the full row context to
540
- # validate
558
+ # validate rather than looking at each column's value in isolation.
541
559
  def validate_rows(&block)
542
- raise 'Invalid block passed to Importer#validate_columns: block should accept a single Row argument' if block.arity != 1
560
+ raise 'Invalid block passed to Importer#validate_rows: block should accept a single Row argument' if block.arity != 1
543
561
  @row_validator = block
544
562
  end
545
563
 
@@ -679,9 +697,9 @@ class Importer
679
697
  end
680
698
  end
681
699
 
682
- # We is good
700
+ # Add the row - may contain errors
683
701
  @data.rows << row
684
- row
702
+ !has_errors?
685
703
  end
686
704
 
687
705
  def rows
@@ -155,13 +155,8 @@ describe Importer do
155
155
  importer = Importer.build do
156
156
  column :foo
157
157
  end
158
- was_run = false
159
-
160
- importer.on_error do
161
- was_run = true
162
- end
163
- was_run.should be_false
164
158
 
159
+ was_run = false
165
160
  importer.add_error('An error')
166
161
  importer.on_error do
167
162
  was_run = true
@@ -175,11 +170,6 @@ describe Importer do
175
170
  end
176
171
 
177
172
  was_run = false
178
- importer.on_success do
179
- was_run = true
180
- end
181
- was_run.should be_false
182
-
183
173
  importer.import_string("foo\n1")
184
174
  importer.has_errors?.should be_false
185
175
  importer.on_success do
@@ -335,4 +325,33 @@ describe Importer do
335
325
  stream.pos.should == 0
336
326
  end
337
327
 
328
+ it 'should report an error when all rows are filtered' do
329
+ importer = Importer.build do
330
+ column :num, :type => :int
331
+ filter do |row|
332
+ row[:num] < 50
333
+ end
334
+ end
335
+
336
+ importer.import_string("num\n1\n2")
337
+ importer.error_summary.should be_nil
338
+
339
+ importer.import_string("num\n100\n101")
340
+ importer.error_summary.should_not be_nil
341
+ importer.error_summary.should include('No unfiltered rows found')
342
+ end
343
+
344
+ it 'should not report an error when all rows filtered but #allow_empty! set' do
345
+ importer = Importer.build do
346
+ allow_empty!
347
+ column :num, :type => :int
348
+ filter do |row|
349
+ row[:num] < 50
350
+ end
351
+ end
352
+
353
+ importer.import_string("num\n100\n101")
354
+ importer.error_summary.should be_nil
355
+ end
356
+
338
357
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iron-import
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.7
4
+ version: 0.8.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Morris
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-03 00:00:00.000000000 Z
11
+ date: 2018-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: iron-extensions
@@ -157,7 +157,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
157
157
  version: '0'
158
158
  requirements: []
159
159
  rubyforge_project:
160
- rubygems_version: 2.7.6
160
+ rubygems_version: 2.6.14
161
161
  signing_key:
162
162
  specification_version: 4
163
163
  summary: CSV, HTML, XLS, and XLSX import processing support