batch_factory 0.0.1
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.
- data/lib/batch_factory/hashed_worksheet.rb +19 -0
- data/lib/batch_factory/parser.rb +59 -0
- data/lib/batch_factory/version.rb +3 -0
- data/lib/batch_factory.rb +19 -0
- data/spec/spec_helper.rb +5 -0
- data/spec/unit/batch_factory/hashed_worksheet.rb +29 -0
- data/spec/unit/batch_factory/parser.rb +40 -0
- data/spec/unit/batch_factory.rb +13 -0
- metadata +105 -0
@@ -0,0 +1,19 @@
|
|
1
|
+
module BatchFactory
|
2
|
+
class HashedWorksheet
|
3
|
+
attr_accessor :keys, :rows
|
4
|
+
|
5
|
+
def initialize(keys, rows)
|
6
|
+
@keys = keys
|
7
|
+
@rows = rows
|
8
|
+
end
|
9
|
+
|
10
|
+
def method_missing(method, *args, &block)
|
11
|
+
if (Enumerable.public_instance_methods + Array.public_instance_methods).include? method.to_sym
|
12
|
+
@rows.send(method, *args, &block)
|
13
|
+
else
|
14
|
+
super
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module BatchFactory
|
2
|
+
class Parser
|
3
|
+
attr_accessor :worksheet, :heading_keys, :column_bounds, :row_hashes
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@heading_keys = []
|
7
|
+
@column_bounds = []
|
8
|
+
@row_hashes = []
|
9
|
+
end
|
10
|
+
|
11
|
+
def open file_location
|
12
|
+
workbook = Spreadsheet.open File.open(file_location)
|
13
|
+
@worksheet = workbook.worksheet(0)
|
14
|
+
end
|
15
|
+
|
16
|
+
def parse!
|
17
|
+
@hashed_worksheet = nil
|
18
|
+
|
19
|
+
parse_column_bounds
|
20
|
+
parse_heading_keys
|
21
|
+
parse_data_rows
|
22
|
+
end
|
23
|
+
|
24
|
+
def parse_heading_keys
|
25
|
+
@heading_keys = worksheet.row(0).map do |key|
|
26
|
+
key.blank? ? nil : key.strip
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def parse_column_bounds
|
31
|
+
@column_bounds = [@worksheet.dimensions[2], @worksheet.dimensions[3]-1]
|
32
|
+
end
|
33
|
+
|
34
|
+
def parse_data_rows
|
35
|
+
@row_hashes = []
|
36
|
+
|
37
|
+
worksheet.each(1) do |row|
|
38
|
+
hash = HashWithIndifferentAccess.new
|
39
|
+
|
40
|
+
for cell_index in @column_bounds[0]..@column_bounds[1]
|
41
|
+
if key = @heading_keys[cell_index] and
|
42
|
+
value = row[cell_index]
|
43
|
+
hash[key] = value.to_s.strip if value.present?
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
@row_hashes << hash
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def hashed_worksheet
|
52
|
+
@hashed_worksheet ||= HashedWorksheet.new(
|
53
|
+
@heading_keys,
|
54
|
+
@row_hashes
|
55
|
+
)
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spreadsheet'
|
2
|
+
require 'active_support/core_ext/object/blank'
|
3
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
4
|
+
|
5
|
+
require 'batch_factory/version'
|
6
|
+
require 'batch_factory/parser'
|
7
|
+
require 'batch_factory/hashed_worksheet'
|
8
|
+
|
9
|
+
module BatchFactory
|
10
|
+
class << self
|
11
|
+
def from_file file_location
|
12
|
+
parser = BatchFactory::Parser.new
|
13
|
+
parser.open file_location
|
14
|
+
parser.parse!
|
15
|
+
|
16
|
+
parser.hashed_worksheet
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe BatchFactory::HashedWorksheet do
|
4
|
+
|
5
|
+
context 'w/ instance methods' do
|
6
|
+
let(:worksheet) do
|
7
|
+
parser = BatchFactory::Parser.new
|
8
|
+
parser.open VALID_SPREADSHEET
|
9
|
+
parser.parse!
|
10
|
+
parser.hashed_worksheet
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should return an array of heading keys' do
|
14
|
+
worksheet.keys[0].should == 'name'
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should return an array of data hashes' do
|
18
|
+
worksheet.rows[0][:age].should == '50'
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should iterate over the rows' do
|
22
|
+
worksheet.size.should == 1
|
23
|
+
worksheet.each_with_index do |hash, index|
|
24
|
+
hash.should == worksheet.rows[index]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe BatchFactory::Parser do
|
4
|
+
let(:parser){ BatchFactory::Parser.new }
|
5
|
+
|
6
|
+
context 'w/ instance methods' do
|
7
|
+
it 'should open and parse a valid file' do
|
8
|
+
parser.open VALID_SPREADSHEET
|
9
|
+
parser.worksheet.should_not be_nil
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'when parsing' do
|
13
|
+
before do
|
14
|
+
parser.open VALID_SPREADSHEET
|
15
|
+
parser.parse!
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should parse column information' do
|
19
|
+
parser.column_bounds.should == [0,5]
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should parse the heading keys' do
|
23
|
+
parser.heading_keys.should == ['name', 'address', nil, 'country', 'bid', 'age']
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should parse the data rows' do
|
27
|
+
parser.row_hashes[0]['age'].should == '50'
|
28
|
+
parser.row_hashes.size.should == 1
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should return a hashed worksheet' do
|
32
|
+
worksheet = parser.hashed_worksheet
|
33
|
+
worksheet.rows.should == parser.row_hashes
|
34
|
+
worksheet.keys.should == parser.heading_keys
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe BatchFactory do
|
4
|
+
|
5
|
+
context 'w/ class methods' do
|
6
|
+
it 'should open a file and return a hashed workbook' do
|
7
|
+
worksheet = BatchFactory.from_file VALID_SPREADSHEET
|
8
|
+
worksheet.size.should == 1
|
9
|
+
worksheet[0][:age].should == '50'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: batch_factory
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Denis Ivanov
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: spreadsheet
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0.7'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0.7'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activesupport
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '2.10'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.10'
|
62
|
+
description: Assumes first row to be keys and returns hashes for consecutive rows
|
63
|
+
email:
|
64
|
+
- visible@jumph4x.net
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- lib/batch_factory.rb
|
70
|
+
- lib/batch_factory/hashed_worksheet.rb
|
71
|
+
- lib/batch_factory/parser.rb
|
72
|
+
- lib/batch_factory/version.rb
|
73
|
+
- spec/spec_helper.rb
|
74
|
+
- spec/unit/batch_factory.rb
|
75
|
+
- spec/unit/batch_factory/hashed_worksheet.rb
|
76
|
+
- spec/unit/batch_factory/parser.rb
|
77
|
+
homepage: https://github.com/jumph4x/batch-factory
|
78
|
+
licenses: []
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ! '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 1.8.24
|
98
|
+
signing_key:
|
99
|
+
specification_version: 3
|
100
|
+
summary: Meaningful spreadsheet access
|
101
|
+
test_files:
|
102
|
+
- spec/spec_helper.rb
|
103
|
+
- spec/unit/batch_factory.rb
|
104
|
+
- spec/unit/batch_factory/hashed_worksheet.rb
|
105
|
+
- spec/unit/batch_factory/parser.rb
|