batch_factory 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,9 +8,8 @@ module BatchFactory
8
8
  @row_hashes = []
9
9
  end
10
10
 
11
- def open file_location
12
- workbook = Spreadsheet.open File.open(file_location)
13
- @worksheet = workbook.worksheet(0)
11
+ def open(file_location, sheet_number = 0)
12
+ @worksheet = SpreadsheetDocument.new(file_location, sheet_number)
14
13
  end
15
14
 
16
15
  def parse!
@@ -22,26 +21,28 @@ module BatchFactory
22
21
  end
23
22
 
24
23
  def parse_heading_keys
25
- @heading_keys = worksheet.row(0).map do |key|
24
+ @heading_keys = worksheet.row(worksheet.first_row).map do |key|
26
25
  key.blank? ? nil : key.strip
27
26
  end
28
27
  end
29
28
 
30
29
  def parse_column_bounds
31
- @column_bounds = [@worksheet.dimensions[2], @worksheet.dimensions[3]-1]
30
+ @column_bounds = @worksheet.first_column..@worksheet.last_column
32
31
  end
33
32
 
34
33
  def parse_data_rows
35
34
  @row_hashes = []
36
35
 
37
- worksheet.each(1) do |row|
36
+ rows_range = (worksheet.first_row+1..worksheet.last_row)
37
+ rows_range.each do |idx|
38
+ row = worksheet.row(idx)
38
39
  hash = HashWithIndifferentAccess.new
39
40
 
40
- for cell_index in @column_bounds[0]..@column_bounds[1]
41
+ for cell_index in @column_bounds
41
42
  if key = @heading_keys[cell_index] and
42
43
  value = row[cell_index]
43
44
 
44
- hash[key] = sanitize_value value if value.present?
45
+ hash[key] = value if value.present?
45
46
  end
46
47
  end
47
48
 
@@ -49,14 +50,6 @@ module BatchFactory
49
50
  end
50
51
  end
51
52
 
52
- def sanitize_value value
53
- if value.is_a? Spreadsheet::Formula
54
- value.value
55
- else
56
- value.to_s.strip
57
- end
58
- end
59
-
60
53
  def hashed_worksheet
61
54
  @hashed_worksheet ||= HashedWorksheet.new(
62
55
  @heading_keys,
@@ -1,3 +1,3 @@
1
1
  module BatchFactory
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
data/lib/batch_factory.rb CHANGED
@@ -2,6 +2,7 @@ require 'spreadsheet'
2
2
  require 'active_support/core_ext/object/blank'
3
3
  require 'active_support/core_ext/hash/indifferent_access'
4
4
 
5
+ require 'spreadsheet_document'
5
6
  require 'batch_factory/version'
6
7
  require 'batch_factory/parser'
7
8
  require 'batch_factory/hashed_worksheet'
@@ -0,0 +1,25 @@
1
+ require 'roo'
2
+ class SpreadsheetDocument
3
+ def initialize(file_name, sheet_number)
4
+ @spreadsheet = case file_name.split('.').last
5
+ when 'xls'
6
+ Excel.new(file_name)
7
+ when 'xlsx'
8
+ Excelx.new(file_name)
9
+ when 'ods'
10
+ Openoffice.new(file_name)
11
+ else
12
+ Csv.new(file_name)
13
+ end
14
+ @current_sheet = @spreadsheet.sheets[sheet_number]
15
+ end
16
+
17
+ def method_missing(method, *args, &block)
18
+ if @spreadsheet.respond_to? method.to_sym
19
+ args << @current_sheet
20
+ @spreadsheet.send(method, *args, &block)
21
+ else
22
+ super
23
+ end
24
+ end
25
+ end
@@ -15,7 +15,7 @@ describe BatchFactory::HashedWorksheet do
15
15
  end
16
16
 
17
17
  it 'should return an array of data hashes' do
18
- worksheet.rows[0][:age].should == '50'
18
+ worksheet.rows[0][:age].should == 50
19
19
  end
20
20
 
21
21
  it 'should iterate over the rows' do
@@ -9,14 +9,14 @@ describe BatchFactory::Parser do
9
9
  parser.worksheet.should_not be_nil
10
10
  end
11
11
 
12
- context 'when parsing' do
12
+ context 'when parsing default worksheet' do
13
13
  before do
14
14
  parser.open VALID_SPREADSHEET
15
15
  parser.parse!
16
16
  end
17
17
 
18
18
  it 'should parse column information' do
19
- parser.column_bounds.should == [0,5]
19
+ parser.column_bounds.should == (1..6)
20
20
  end
21
21
 
22
22
  it 'should parse the heading keys' do
@@ -24,7 +24,7 @@ describe BatchFactory::Parser do
24
24
  end
25
25
 
26
26
  it 'should parse the data rows' do
27
- parser.row_hashes[0]['age'].should == '50'
27
+ parser.row_hashes[0]['age'].should == 50
28
28
  parser.row_hashes.size.should == 1
29
29
  end
30
30
 
@@ -34,6 +34,33 @@ describe BatchFactory::Parser do
34
34
  worksheet.keys.should == parser.heading_keys
35
35
  end
36
36
  end
37
+
38
+ context 'when parsing second worksheet' do
39
+ before do
40
+ parser.open VALID_SPREADSHEET, 1
41
+ parser.parse!
42
+ end
43
+
44
+ it 'should parse column information' do
45
+ parser.column_bounds.should == (3..8)
46
+ end
47
+
48
+ it 'should parse the heading keys' do
49
+ parser.heading_keys.should == ['name', 'address', nil, 'country', 'bid', 'age']
50
+ end
51
+
52
+ it 'should parse the data rows' do
53
+ parser.row_hashes[0]['age'].should == 50
54
+ parser.row_hashes.size.should == 1
55
+ end
56
+
57
+ it 'should return a hashed worksheet' do
58
+ worksheet = parser.hashed_worksheet
59
+ worksheet.rows.should == parser.row_hashes
60
+ worksheet.keys.should == parser.heading_keys
61
+ end
62
+ end
63
+
37
64
 
38
65
  end
39
66
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: batch_factory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,16 +9,16 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-11 00:00:00.000000000 Z
12
+ date: 2013-03-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: spreadsheet
15
+ name: roo
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: '0.7'
21
+ version: 1.10.1
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: '0.7'
29
+ version: 1.10.1
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: activesupport
32
32
  requirement: !ruby/object:Gem::Requirement
@@ -67,9 +67,10 @@ extensions: []
67
67
  extra_rdoc_files: []
68
68
  files:
69
69
  - lib/batch_factory.rb
70
+ - lib/spreadsheet_document.rb
70
71
  - lib/batch_factory/hashed_worksheet.rb
71
- - lib/batch_factory/parser.rb
72
72
  - lib/batch_factory/version.rb
73
+ - lib/batch_factory/parser.rb
73
74
  - spec/spec_helper.rb
74
75
  - spec/unit/batch_factory.rb
75
76
  - spec/unit/batch_factory/hashed_worksheet.rb