batch_factory 0.0.6 → 0.1.0

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: 6f33676dbbb2880f05b0d68c8354d73842e800e8
4
- data.tar.gz: 8b73db2c704de678ffe46edcde50e4e88f655fb6
3
+ metadata.gz: 426ee0a9312cf54ccc7c7c8dba99955b2f3f7bb4
4
+ data.tar.gz: 866ca63ab2d7fb83584b4a244898a9f5c0af964e
5
5
  SHA512:
6
- metadata.gz: 3458d01dcee4626806d1cf1c002c0321376cac3d530973a96f479f9d57477c9fe3a7dc421e409154fe06e0ddc8d530622050c62433c5f719bcb9ad8e9643a629
7
- data.tar.gz: 84957e02bb3382aab41a53eee4f594551988cb87da3c6b48f241cd8585d0b59501f5dc6b5a3f711d0e36165eabd275b923fda7918405bd5632497afb3215414b
6
+ metadata.gz: bf14a17f8bea356ce0ed6e7976213b6edffe92c45a84ae2f83728a29094ed20ac754af04faf729c9e1c3f6c5fd796a320fb35425032a9654d25fc89511d2b8fd
7
+ data.tar.gz: b586db47d5c3064cdc0fd2b1db6fe5b80497836e0de9e24f58ad3b82355d8ab905635b7b6e54c2a2f39c68596108249421cf39466987ea38380fceb09c1f983f
data/lib/batch_factory.rb CHANGED
@@ -9,9 +9,9 @@ require 'batch_factory/hashed_worksheet'
9
9
 
10
10
  module BatchFactory
11
11
  class << self
12
- def from_file file_location, keys = nil
12
+ def from_file(file_location, options = {})
13
13
  parser = BatchFactory::Parser.new
14
- parser.open file_location, 0, keys
14
+ parser.open file_location, options
15
15
  parser.parse!
16
16
 
17
17
  parser.hashed_worksheet
@@ -2,18 +2,25 @@ module BatchFactory
2
2
  class HashedWorksheet
3
3
  attr_accessor :keys, :rows
4
4
 
5
+ @@proxy_methods = Enumerable.public_instance_methods + Array.public_instance_methods
6
+
5
7
  def initialize(keys, rows)
6
8
  @keys = keys
7
9
  @rows = rows
8
10
  end
9
11
 
10
12
  def method_missing(method, *args, &block)
11
- if (Enumerable.public_instance_methods + Array.public_instance_methods).include? method.to_sym
13
+ if should_proxy_method?(method.to_sym)
12
14
  @rows.send(method, *args, &block)
13
15
  else
14
16
  super
15
17
  end
16
18
  end
17
19
 
20
+ private
21
+
22
+ def should_proxy_method?(method)
23
+ @@proxy_methods.include? method
24
+ end
18
25
  end
19
26
  end
@@ -1,6 +1,7 @@
1
1
  module BatchFactory
2
2
  class Parser
3
- attr_accessor :worksheet, :user_heading_keys, :heading_keys, :column_bounds, :row_hashes
3
+ attr_accessor :worksheet, :user_heading_keys,
4
+ :heading_keys, :column_bounds, :row_hashes
4
5
 
5
6
  def initialize
6
7
  @heading_keys = []
@@ -8,9 +9,9 @@ module BatchFactory
8
9
  @row_hashes = []
9
10
  end
10
11
 
11
- def open(file_location, sheet_number = 0, user_keys = nil)
12
- @worksheet = SpreadsheetDocument.new(file_location, sheet_number)
13
- @user_heading_keys = user_keys
12
+ def open(file_location, options = {})
13
+ @worksheet = SpreadsheetDocument.new(file_location, options)
14
+ @user_heading_keys = options[:keys]
14
15
  end
15
16
 
16
17
  def parse!
@@ -1,3 +1,11 @@
1
1
  module BatchFactory
2
- VERSION = '0.0.6'
2
+ module VERSION
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ PATCH = 0
6
+ SUFFIX = nil
7
+
8
+ STRING = [MAJOR, MINOR, PATCH, SUFFIX].compact.join('.')
9
+ end
3
10
  end
11
+
@@ -1,7 +1,13 @@
1
1
  require 'roo'
2
+
2
3
  class SpreadsheetDocument
3
- def initialize(file_name, sheet_number)
4
- @spreadsheet = case file_name.split('.').last
4
+ attr_reader :filetype, :sheet_number
5
+
6
+ def initialize(file_name, options = {})
7
+ @sheet_number = options[:sheet_number] || 0
8
+ @filetype = options[:filetype] || detect_filetype(file_name)
9
+
10
+ @spreadsheet = case @filetype
5
11
  when 'xls'
6
12
  Roo::Excel.new(file_name)
7
13
  when 'xlsx'
@@ -11,7 +17,7 @@ class SpreadsheetDocument
11
17
  else
12
18
  Roo::CSV.new(file_name)
13
19
  end
14
- @current_sheet = @spreadsheet.sheets[sheet_number]
20
+ @current_sheet = @spreadsheet.sheets[@sheet_number]
15
21
  end
16
22
 
17
23
  def method_missing(method, *args, &block)
@@ -22,4 +28,11 @@ class SpreadsheetDocument
22
28
  super
23
29
  end
24
30
  end
31
+
32
+ private
33
+
34
+ def detect_filetype(file_name)
35
+ File.extname(file_name)[1..-1]
36
+ end
25
37
  end
38
+
@@ -1,7 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe BatchFactory do
4
-
5
4
  context 'w/ class methods' do
6
5
  it 'should open a file and return a hashed workbook' do
7
6
  worksheet = BatchFactory.from_file VALID_SPREADSHEET
@@ -10,5 +9,5 @@ describe BatchFactory do
10
9
  worksheet[0][:age].should == 50
11
10
  end
12
11
  end
13
-
14
12
  end
13
+
@@ -1,7 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe BatchFactory::HashedWorksheet do
4
-
5
4
  context 'w/ instance methods' do
6
5
  let(:worksheet) do
7
6
  parser = BatchFactory::Parser.new
@@ -27,3 +26,4 @@ describe BatchFactory::HashedWorksheet do
27
26
  end
28
27
 
29
28
  end
29
+
@@ -37,7 +37,7 @@ describe BatchFactory::Parser do
37
37
 
38
38
  context 'when parsing second worksheet' do
39
39
  before do
40
- parser.open VALID_SPREADSHEET, 1
40
+ parser.open VALID_SPREADSHEET, sheet_number: 1
41
41
  parser.parse!
42
42
  end
43
43
 
@@ -60,6 +60,6 @@ describe BatchFactory::Parser do
60
60
  worksheet.keys.should == parser.heading_keys
61
61
  end
62
62
  end
63
-
64
63
  end
65
64
  end
65
+
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe SpreadsheetDocument do
4
+ context 'w/ filetype option' do
5
+ subject { described_class.new(VALID_SPREADSHEET) }
6
+
7
+ its(:filetype) { should eq('xls') }
8
+ end
9
+
10
+ context 'with filetype option' do
11
+ subject { described_class.new(VALID_SPREADSHEET, filetype: 'xls') }
12
+
13
+ its(:filetype) { should eq('xls') }
14
+ end
15
+ end
16
+
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,16 @@
1
1
  require 'rspec'
2
+ require 'rspec/its'
3
+
2
4
  require 'batch_factory'
3
5
 
4
- ASSETS_DIR = File.expand_path File.join(File.dirname(__FILE__), 'files')
6
+ ASSETS_DIR = File.expand_path File.join(File.dirname(__FILE__), 'support', 'files')
5
7
  VALID_SPREADSHEET = File.join(ASSETS_DIR, 'valid-spreadsheet.xls')
8
+
9
+ RSpec.configure do |config|
10
+ config.mock_with :rspec
11
+ config.order = 'random'
12
+ config.filter_run focus: true
13
+ config.run_all_when_everything_filtered = true
14
+ config.fail_fast = false
15
+ end
16
+
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.6
4
+ version: 0.1.0
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-17 00:00:00.000000000 Z
11
+ date: 2015-04-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: roo
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '2.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-its
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description: Assumes first row to be keys and returns hashes for consecutive rows
56
70
  email:
57
71
  - visible@jumph4x.net
@@ -64,10 +78,11 @@ files:
64
78
  - lib/batch_factory/parser.rb
65
79
  - lib/batch_factory/version.rb
66
80
  - lib/spreadsheet_document.rb
81
+ - spec/batch_factory/batch_factory_spec.rb
82
+ - spec/batch_factory/hashed_worksheet_spec.rb
83
+ - spec/batch_factory/parser_spec.rb
84
+ - spec/batch_factory/spreadsheet_document_spec.rb
67
85
  - spec/spec_helper.rb
68
- - spec/unit/batch_factory.rb
69
- - spec/unit/batch_factory/hashed_worksheet.rb
70
- - spec/unit/batch_factory/parser.rb
71
86
  homepage: https://github.com/jumph4x/batch-factory
72
87
  licenses: []
73
88
  metadata: {}
@@ -92,7 +107,8 @@ signing_key:
92
107
  specification_version: 4
93
108
  summary: Meaningful spreadsheet access
94
109
  test_files:
110
+ - spec/batch_factory/batch_factory_spec.rb
111
+ - spec/batch_factory/hashed_worksheet_spec.rb
112
+ - spec/batch_factory/parser_spec.rb
113
+ - spec/batch_factory/spreadsheet_document_spec.rb
95
114
  - spec/spec_helper.rb
96
- - spec/unit/batch_factory/hashed_worksheet.rb
97
- - spec/unit/batch_factory/parser.rb
98
- - spec/unit/batch_factory.rb