ndr_import 3.1.5 → 3.1.6

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
  SHA1:
3
- metadata.gz: 154d097959db5f976eb388923cb0ec092ca7a082
4
- data.tar.gz: 5189ab2452d9faa4757d0217677edffb5e6b9f89
3
+ metadata.gz: 5d79b720417de07bd9f4db6de53c08600ee190ac
4
+ data.tar.gz: d267da10cc8446fe3272b6aab0e6792e6d77b3e2
5
5
  SHA512:
6
- metadata.gz: 604fe52d17f3741882d0402cd523db3461aa846f437c65f3afaad9e1ea9d7f080d46efa363175a027eedcf2502544f99b0cf144d70b28770e3d9d86c8adcc99d
7
- data.tar.gz: ed7332ec167e80547d684a9b8df986f65cbf82de9c3f42e60fa49a8c5dfe060a6507fb0b9a213fa812ab1137121b5acaf390de614cef9fcd12cc6a58feb50f65
6
+ metadata.gz: 47a472805cd80bede14e404c026dbfdaa4a0b1f7cd1cd48f33d4c83fc27e66b0b41f91f3a64b4fdd77486f302d2ee58a0fd2c3280a9d93e128f1fc55aace7634
7
+ data.tar.gz: 515cd22f7ae8a0c2b5faa8b3d2b9f5aac08932772637bd189bfe791c3446f2daa42317a87993af8dd642969b289ef0ab880bc647a795ea9e7a9c06f89c6359ad
data/code_safety.yml CHANGED
@@ -75,7 +75,7 @@ file safety:
75
75
  lib/ndr_import/file/excel.rb:
76
76
  comments:
77
77
  reviewed_by: joshpencheon
78
- safe_revision: 80ce15cc5ca15019400d4cd24c3353191bec00cc
78
+ safe_revision: 334d53bb9e8698bba3d92b4dbd71b36faacae629
79
79
  lib/ndr_import/file/pdf.rb:
80
80
  comments:
81
81
  reviewed_by: timgentry
@@ -163,7 +163,7 @@ file safety:
163
163
  lib/ndr_import/version.rb:
164
164
  comments: another check?
165
165
  reviewed_by: joshpencheon
166
- safe_revision: 03305d1e8a6fab607565886bdd7ce4d63a0cdba1
166
+ safe_revision: 749aa9bb66f6e07b52acab1096f6f866034fc676
167
167
  ndr_import.gemspec:
168
168
  comments:
169
169
  reviewed_by: josh.pencheon
@@ -179,7 +179,7 @@ file safety:
179
179
  test/file/excel_test.rb:
180
180
  comments:
181
181
  reviewed_by: joshpencheon
182
- safe_revision: 80ce15cc5ca15019400d4cd24c3353191bec00cc
182
+ safe_revision: 334d53bb9e8698bba3d92b4dbd71b36faacae629
183
183
  test/file/pdf_test.rb:
184
184
  comments:
185
185
  reviewed_by: timgentry
@@ -340,6 +340,14 @@ file safety:
340
340
  comments:
341
341
  reviewed_by: timgentry
342
342
  safe_revision: 8c30f89f0562ab120769c166d4e93ff839c055f7
343
+ test/resources/sheet_streaming.xls:
344
+ comments:
345
+ reviewed_by: joshpencheon
346
+ safe_revision: 334d53bb9e8698bba3d92b4dbd71b36faacae629
347
+ test/resources/sheet_streaming.xlsx:
348
+ comments:
349
+ reviewed_by: joshpencheon
350
+ safe_revision: 334d53bb9e8698bba3d92b4dbd71b36faacae629
343
351
  test/resources/standard_mappings.yml:
344
352
  comments:
345
353
  reviewed_by: timgentry
@@ -18,8 +18,8 @@ module NdrImport
18
18
  return enum_for(:tables) unless block_given?
19
19
 
20
20
  workbook = load_workbook(@filename)
21
- workbook.each_with_pagename do |tablename, sheet|
22
- yield tablename, excel_rows(workbook, sheet)
21
+ workbook.sheets.each do |sheet_name|
22
+ yield sheet_name, excel_rows(workbook, sheet_name)
23
23
  end
24
24
  end
25
25
 
@@ -49,37 +49,38 @@ module NdrImport
49
49
  private
50
50
 
51
51
  # Iterate through the sheet line by line, yielding each one in turn.
52
- def excel_rows(workbook, sheet, &block)
53
- return enum_for(:excel_rows, workbook, sheet) unless block
52
+ def excel_rows(workbook, sheet_name, &block)
53
+ return enum_for(:excel_rows, workbook, sheet_name) unless block
54
54
 
55
55
  if workbook.is_a?(Roo::Excelx)
56
56
  # FIXME: xlsx_rows(sheet, &block) should produce the same output as xls_rows
57
- xls_rows(sheet, &block)
57
+ xls_rows(workbook, sheet_name, &block)
58
58
  else
59
- xls_rows(sheet, &block)
59
+ xls_rows(workbook, sheet_name, &block)
60
60
  end
61
61
  end
62
62
 
63
63
  # Iterate through an xls sheet line by line, yielding each one in turn.
64
- def xls_rows(sheet)
65
- return enum_for(:xls_rows, sheet) unless block_given?
66
- return unless sheet.first_row # NULL Sheet (TODO: perhaps XLSX only, use xlsx_rows...)
67
-
68
- sheet.first_row.upto(sheet.last_row) do |row|
69
- line = []
70
- sheet.first_column.upto(sheet.last_column) do |col|
71
- line << cast_excel_value(sheet.cell(row, col))
72
- end
73
- yield line
64
+ def xls_rows(workbook, sheet_name)
65
+ return enum_for(:xls_rows, workbook, sheet_name) unless block_given?
66
+
67
+ activate_sheet!(workbook, sheet_name)
68
+ return unless workbook.first_row
69
+ (workbook.first_row..workbook.last_row).each do |row|
70
+ activate_sheet!(workbook, sheet_name)
71
+ columns = (workbook.first_column..workbook.last_column)
72
+ yield columns.map { |col| cast_excel_value(workbook.cell(row, col)) }
74
73
  end
75
74
  end
76
75
 
77
76
  # Iterate through an xlsx sheet line by line, yielding each one in turn.
78
77
  # This method uses streaming https://github.com/roo-rb/roo#excel-xlsx-and-xlsm-support
79
- def xlsx_rows(sheet)
80
- return enum_for(:xlsx_rows, sheet) unless block_given?
78
+ def xlsx_rows(workbook, sheet_name)
79
+ return enum_for(:xlsx_rows, workbook, sheet_name) unless block_given?
80
+
81
+ activate_sheet!(workbook, sheet_name)
81
82
 
82
- sheet.each_row_streaming(:pad_cells => true) do |row|
83
+ workbook.each_row_streaming(:pad_cells => true) do |row|
83
84
  yield row.map { |cell| cast_excel_value(cell.value) }
84
85
  end
85
86
  end
@@ -125,6 +126,14 @@ module NdrImport
125
126
  FileUtils.mkdir_p(SafeFile.safepath_to_string(SafeFile.dirname(dest)))
126
127
  FileUtils.cp(SafeFile.safepath_to_string(source), SafeFile.safepath_to_string(dest))
127
128
  end
129
+
130
+ # Sets `sheet_name' to be the default (i.e. active) sheet in `workbook'.
131
+ # This setting applies to the Roo object, so needs to re-applied if (for example)
132
+ # streaming rows from multiple sheets simultaneously - something that was problematic
133
+ # with Workbook#each_with_pagename.
134
+ def activate_sheet!(workbook, sheet_name)
135
+ workbook.sheet(sheet_name)
136
+ end
128
137
  end
129
138
 
130
139
  Registry.register(Excel, 'xls', 'xlsx')
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
  # This stores the current version of the NdrImport gem
3
3
  module NdrImport
4
- VERSION = '3.1.5'.freeze
4
+ VERSION = '3.1.6'.freeze
5
5
  end
@@ -33,9 +33,28 @@ module NdrImport
33
33
  file_path = @permanent_test_files.join('blank_tab_test.xlsx')
34
34
  handler = NdrImport::File::Excel.new(file_path, nil)
35
35
 
36
- handler.tables.each do |_tablename, sheet|
37
- assert_instance_of Enumerator, sheet
38
- assert sheet.to_a.is_a?(Array)
36
+ sheets = handler.tables.map { |_tablename, sheet| sheet }
37
+
38
+ assert_equal %w(column_a column_b column_c column_d), sheets[0].next
39
+ assert_equal %w(11111 ABC123 8888888888 2), sheets[0].next
40
+ assert_equal %w(column_a column_b column_c column_d), sheets[1].next
41
+ assert_equal %w(11111 ABC123 8888888888 3), sheets[1].next
42
+ assert_raises(StopIteration) { sheets[2].next }
43
+ end
44
+
45
+ %w(sheet_streaming.xlsx sheet_streaming.xls).each do |filename|
46
+ test "should be able to stream from multiple sheets at once from #{filename}" do
47
+ file_path = @permanent_test_files.join(filename)
48
+ handler = NdrImport::File::Excel.new(file_path, nil)
49
+
50
+ sheets = handler.tables.map { |_tablename, sheet| sheet }
51
+
52
+ assert_equal %w(1A1 1B1), sheets[0].next
53
+ assert_raises(StopIteration) { sheets[2].next }
54
+ assert_equal %w(2A1 2B1), sheets[1].next
55
+ assert_equal %w(2A2 2B2), sheets[1].next
56
+ assert_equal %w(1A2 1B2), sheets[0].next
57
+ assert_raises(StopIteration) { sheets[2].next }
39
58
  end
40
59
  end
41
60
 
Binary file
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ndr_import
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.5
4
+ version: 3.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - NCRS Development Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-09 00:00:00.000000000 Z
11
+ date: 2016-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -372,6 +372,8 @@ files:
372
372
  - test/resources/not_a_word_file.doc
373
373
  - test/resources/sample_xls.xls
374
374
  - test/resources/sample_xlsx.xlsx
375
+ - test/resources/sheet_streaming.xls
376
+ - test/resources/sheet_streaming.xlsx
375
377
  - test/resources/standard_mappings.yml
376
378
  - test/resources/txt_file_xls_extension.xls
377
379
  - test/resources/txt_file_xlsx_extension.xlsx
@@ -453,6 +455,8 @@ test_files:
453
455
  - test/resources/not_a_word_file.doc
454
456
  - test/resources/sample_xls.xls
455
457
  - test/resources/sample_xlsx.xlsx
458
+ - test/resources/sheet_streaming.xls
459
+ - test/resources/sheet_streaming.xlsx
456
460
  - test/resources/standard_mappings.yml
457
461
  - test/resources/txt_file_xls_extension.xls
458
462
  - test/resources/txt_file_xlsx_extension.xlsx