batch_factory 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: de755e29b2ab64eb96981e9399f2c04f2445f418
4
- data.tar.gz: f823d42bca1cade8dac3685c85af46d1ce3276e5
3
+ metadata.gz: 6f33676dbbb2880f05b0d68c8354d73842e800e8
4
+ data.tar.gz: 8b73db2c704de678ffe46edcde50e4e88f655fb6
5
5
  SHA512:
6
- metadata.gz: 7d7e689e7404dfe1454b282d55555287146c6b165d38d04af3d04b1c21e26891d6a8c70b1ca3336909372502f07f1e5ca1b1baff4eb02421fccb3be83767a96a
7
- data.tar.gz: f9db6af5626c42b1d40fc09101210e2fc938035fde02757f0c526fce25d52ed076042b095f40d8a4950a4a7cece1260147091bef6294984d342f554a4482e6ba
6
+ metadata.gz: 3458d01dcee4626806d1cf1c002c0321376cac3d530973a96f479f9d57477c9fe3a7dc421e409154fe06e0ddc8d530622050c62433c5f719bcb9ad8e9643a629
7
+ data.tar.gz: 84957e02bb3382aab41a53eee4f594551988cb87da3c6b48f241cd8585d0b59501f5dc6b5a3f711d0e36165eabd275b923fda7918405bd5632497afb3215414b
@@ -9,11 +9,11 @@ require 'batch_factory/hashed_worksheet'
9
9
 
10
10
  module BatchFactory
11
11
  class << self
12
- def from_file file_location
12
+ def from_file file_location, keys = nil
13
13
  parser = BatchFactory::Parser.new
14
- parser.open file_location
14
+ parser.open file_location, 0, keys
15
15
  parser.parse!
16
-
16
+
17
17
  parser.hashed_worksheet
18
18
  end
19
19
  end
@@ -1,12 +1,12 @@
1
1
  module BatchFactory
2
2
  class HashedWorksheet
3
3
  attr_accessor :keys, :rows
4
-
4
+
5
5
  def initialize(keys, rows)
6
6
  @keys = keys
7
7
  @rows = rows
8
8
  end
9
-
9
+
10
10
  def method_missing(method, *args, &block)
11
11
  if (Enumerable.public_instance_methods + Array.public_instance_methods).include? method.to_sym
12
12
  @rows.send(method, *args, &block)
@@ -14,6 +14,6 @@ module BatchFactory
14
14
  super
15
15
  end
16
16
  end
17
-
17
+
18
18
  end
19
19
  end
@@ -1,61 +1,64 @@
1
1
  module BatchFactory
2
2
  class Parser
3
- attr_accessor :worksheet, :heading_keys, :column_bounds, :row_hashes
4
-
3
+ attr_accessor :worksheet, :user_heading_keys, :heading_keys, :column_bounds, :row_hashes
4
+
5
5
  def initialize
6
6
  @heading_keys = []
7
7
  @column_bounds = []
8
8
  @row_hashes = []
9
9
  end
10
-
11
- def open(file_location, sheet_number = 0)
10
+
11
+ def open(file_location, sheet_number = 0, user_keys = nil)
12
12
  @worksheet = SpreadsheetDocument.new(file_location, sheet_number)
13
+ @user_heading_keys = user_keys
13
14
  end
14
-
15
+
15
16
  def parse!
16
17
  @hashed_worksheet = nil
17
-
18
+
18
19
  parse_column_bounds
19
20
  parse_heading_keys
20
21
  parse_data_rows
21
22
  end
22
-
23
+
23
24
  def parse_heading_keys
24
- @heading_keys = worksheet.row(worksheet.first_row).map do |key|
25
+ @heading_keys = user_heading_keys || worksheet.row(worksheet.first_row).map do |key|
25
26
  key.blank? ? nil : key.strip
26
27
  end
27
28
  end
28
-
29
+
29
30
  def parse_column_bounds
30
31
  @column_bounds = (@worksheet.first_column-1)..(@worksheet.last_column-1)
31
32
  end
32
-
33
+
33
34
  def parse_data_rows
34
35
  @row_hashes = []
35
-
36
- rows_range = (worksheet.first_row+1..worksheet.last_row)
36
+
37
+ first_row_offset = user_heading_keys ? 0 : 1
38
+
39
+ rows_range = ((worksheet.first_row+first_row_offset)..worksheet.last_row)
37
40
  rows_range.each do |idx|
38
41
  row = worksheet.row(idx)
39
42
  hash = HashWithIndifferentAccess.new
40
-
43
+
41
44
  for cell_index in @column_bounds
42
45
  if key = @heading_keys[cell_index] and
43
46
  value = row[cell_index]
44
-
47
+
45
48
  hash[key] = value if value.present?
46
49
  end
47
50
  end
48
-
51
+
49
52
  @row_hashes << hash unless hash.empty?
50
53
  end
51
54
  end
52
-
55
+
53
56
  def hashed_worksheet
54
57
  @hashed_worksheet ||= HashedWorksheet.new(
55
58
  @heading_keys,
56
59
  @row_hashes
57
60
  )
58
61
  end
59
-
62
+
60
63
  end
61
64
  end
@@ -1,3 +1,3 @@
1
1
  module BatchFactory
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
@@ -9,11 +9,11 @@ class SpreadsheetDocument
9
9
  when 'ods'
10
10
  Roo::Openoffice.new(file_name)
11
11
  else
12
- Roo::Csv.new(file_name)
12
+ Roo::CSV.new(file_name)
13
13
  end
14
14
  @current_sheet = @spreadsheet.sheets[sheet_number]
15
15
  end
16
-
16
+
17
17
  def method_missing(method, *args, &block)
18
18
  if @spreadsheet.respond_to? method.to_sym
19
19
  args << @current_sheet
@@ -5,8 +5,9 @@ describe BatchFactory do
5
5
  context 'w/ class methods' do
6
6
  it 'should open a file and return a hashed workbook' do
7
7
  worksheet = BatchFactory.from_file VALID_SPREADSHEET
8
+ puts worksheet.inspect
8
9
  worksheet.size.should == 1
9
- worksheet[0][:age].should == '50'
10
+ worksheet[0][:age].should == 50
10
11
  end
11
12
  end
12
13
 
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe BatchFactory::HashedWorksheet do
4
-
4
+
5
5
  context 'w/ instance methods' do
6
6
  let(:worksheet) do
7
7
  parser = BatchFactory::Parser.new
@@ -9,15 +9,15 @@ describe BatchFactory::HashedWorksheet do
9
9
  parser.parse!
10
10
  parser.hashed_worksheet
11
11
  end
12
-
12
+
13
13
  it 'should return an array of heading keys' do
14
14
  worksheet.keys[0].should == 'name'
15
15
  end
16
-
16
+
17
17
  it 'should return an array of data hashes' do
18
18
  worksheet.rows[0][:age].should == 50
19
19
  end
20
-
20
+
21
21
  it 'should iterate over the rows' do
22
22
  worksheet.size.should == 1
23
23
  worksheet.each_with_index do |hash, index|
@@ -25,5 +25,5 @@ describe BatchFactory::HashedWorksheet do
25
25
  end
26
26
  end
27
27
  end
28
-
28
+
29
29
  end
@@ -8,26 +8,26 @@ describe BatchFactory::Parser do
8
8
  parser.open VALID_SPREADSHEET
9
9
  parser.worksheet.should_not be_nil
10
10
  end
11
-
11
+
12
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 == (1..6)
19
+ parser.column_bounds.should == (0..5)
20
20
  end
21
-
21
+
22
22
  it 'should parse the heading keys' do
23
23
  parser.heading_keys.should == ['name', 'address', nil, 'country', 'bid', 'age']
24
24
  end
25
-
25
+
26
26
  it 'should parse the data rows' do
27
27
  parser.row_hashes[0]['age'].should == 50
28
28
  parser.row_hashes.size.should == 1
29
29
  end
30
-
30
+
31
31
  it 'should return a hashed worksheet' do
32
32
  worksheet = parser.hashed_worksheet
33
33
  worksheet.rows.should == parser.row_hashes
@@ -40,20 +40,20 @@ describe BatchFactory::Parser do
40
40
  parser.open VALID_SPREADSHEET, 1
41
41
  parser.parse!
42
42
  end
43
-
43
+
44
44
  it 'should parse column information' do
45
- parser.column_bounds.should == (3..8)
45
+ parser.column_bounds.should == (2..7)
46
46
  end
47
-
47
+
48
48
  it 'should parse the heading keys' do
49
49
  parser.heading_keys.should == ['name', 'address', nil, 'country', 'bid', 'age']
50
50
  end
51
-
51
+
52
52
  it 'should parse the data rows' do
53
53
  parser.row_hashes[0]['age'].should == 50
54
54
  parser.row_hashes.size.should == 1
55
55
  end
56
-
56
+
57
57
  it 'should return a hashed worksheet' do
58
58
  worksheet = parser.hashed_worksheet
59
59
  worksheet.rows.should == parser.row_hashes
@@ -61,7 +61,5 @@ describe BatchFactory::Parser do
61
61
  end
62
62
  end
63
63
 
64
-
65
64
  end
66
-
67
65
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: batch_factory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Ivanov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-16 00:00:00.000000000 Z
11
+ date: 2014-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: roo