csv-autoparser 0.0.1 → 0.1.0

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: dda3d9b42b637575933d1b307b5dd5f5687971c6
4
- data.tar.gz: d28893032f72edf4824d4bed8d8c212cdab7a2d6
3
+ metadata.gz: b806b5d4df47de7532a01384fa357b998e53653a
4
+ data.tar.gz: 1ad970d72389cb2a061127d69c584ed8be1e0244
5
5
  SHA512:
6
- metadata.gz: 93cb849bab22ab289d59a7e1821cad3b8fcdea073793d8a804fa19b71d60d07884e44d1a4696f0daadca083ff9c40d3eb862da7a60b0565cf7180cf418c4f236
7
- data.tar.gz: 659990a336d41c984cc5468b9729d6f47cb5f78e2a018be348f65092a8a387c4455b384f0ca3fcfe7666c925fe25f4ec7f089c6b254055ea9eca14b1a6bae314
6
+ metadata.gz: 9263abf623d4ffb74ad65ba9243f9fa36e9924ba7787f7c9619bbbf866a3843764ee2f0a1476af6331c16172eed75f948340562c8750ca37b47260b803594b5a
7
+ data.tar.gz: 07d207dc0bd482d688d721a714bb4caa8527925f521099f47dfe6bb0cf485645811c6d598a572e24eb86f222868909e0246fcf2040d42d9e5f6f63708f3afedc
data/README.md CHANGED
@@ -8,6 +8,8 @@ CSV::AutoParser automatically parses a CSV file given a user specified header ro
8
8
 
9
9
  ## Usage
10
10
 
11
+ require 'csv/autoparser'
12
+
11
13
  # ID header row by CSV line number.
12
14
  csv = CSV::AutoParser.new("my_file.csv") {|csv_line_number, header_row| csv_line_number == 1 }
13
15
  csv.rows.each {|row| puts row.full_name }
@@ -3,20 +3,13 @@ require "csv/autoparser/version"
3
3
 
4
4
  class CSV
5
5
  class AutoParser
6
- class Row
7
6
 
8
- def self.define_methods map
9
- map.each_pair do |column_name, column_offset|
10
- define_method(column_to_method_name(column_name)) { @row[column_offset] }
11
- end
12
- end
13
-
14
- def self.column_to_method_name name
15
- name.downcase.strip.gsub(/\s+/, '_').gsub(/-+/, '_').gsub(/[^\w]/, '')
16
- end
17
-
18
- def initialize row
19
- @row = row
7
+ class Row < Array
8
+ attr_reader :csv_file, :csv_line
9
+ def self.create original_row, file, line
10
+ row = Row.new(original_row)
11
+ row.instance_eval { @csv_file = file; @csv_line = line }
12
+ return row
20
13
  end
21
14
  end
22
15
 
@@ -34,16 +27,22 @@ class CSV
34
27
  if map.empty?
35
28
  if is_header.call(csv_line_number, row)
36
29
  row.each_index {|index| map[row[index]] = index }
37
- Row.define_methods(map)
38
30
  else
39
- @pre_header_rows << row
31
+ @pre_header_rows << Row::create(row, file, csv_line_number)
40
32
  end
41
33
  else
42
- @rows << Row.new(row)
34
+ @rows << Row::create(row, file, csv_line_number)
35
+ map.each_pair do |column_name, column_offset|
36
+ @rows.last.define_singleton_method(column_to_method_name(column_name)) { self[column_offset] }
37
+ end
43
38
  end
44
39
  end
45
40
  raise HeaderRowNotFound, "Could not find header row in #{file}." if map.empty?
46
41
  end
47
42
 
43
+ def column_to_method_name name
44
+ name.downcase.strip.gsub(/\s+/, '_').gsub(/-+/, '_').gsub(/[^\w]/, '')
45
+ end
46
+
48
47
  end
49
48
  end
@@ -1,5 +1,5 @@
1
1
  class CSV
2
2
  class AutoParser
3
- VERSION = "0.0.1"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -1,4 +1,5 @@
1
1
  "full-name",job,"years of age"
2
+ bob,"window washer",13
2
3
  name,"Job title",age
3
4
  "Jon Smith",blacksmith, 55
4
5
  "Jimmy Johnson",farmer, 34
@@ -0,0 +1,3 @@
1
+ "Job title",age,name
2
+ frog,102,"Kermy Frog"
3
+ "beauty queen",19,"mrs piggy"
@@ -3,18 +3,14 @@ require 'minitest_helper'
3
3
 
4
4
  describe CSV::AutoParser do
5
5
 
6
- def input_file
7
- fixture_file_path('persons.csv')
8
- end
9
-
10
6
  it "it can parse a csv automatically via csv line number id" do
11
- csv = CSV::AutoParser.new(input_file) {|csv_line_number, header_row| csv_line_number == 1 }
12
- csv.rows.length.must_equal 4
13
- csv.rows.first.full_name.must_equal "name"
7
+ csv = CSV::AutoParser.new(fixture_file_path('persons.csv')) {|csv_line_number, header_row| csv_line_number == 1 }
8
+ csv.rows.length.must_equal 5
9
+ csv.rows.first.full_name.must_equal "bob"
14
10
  end
15
11
 
16
12
  it "it can parse a csv automatically via header row id" do
17
- csv = CSV::AutoParser.new(input_file) do |line_num, header_row|
13
+ csv = CSV::AutoParser.new(fixture_file_path('persons.csv')) do |line_num, header_row|
18
14
  ["name", "Job title"].all? {|cell| header_row.include?(cell) }
19
15
  end
20
16
  csv.rows.length.must_equal 3
@@ -23,9 +19,40 @@ describe CSV::AutoParser do
23
19
  csv.rows.last.age.to_i.must_equal 29
24
20
  end
25
21
 
22
+ it "it will give you the rows found before the header row" do
23
+ csv = CSV::AutoParser.new(fixture_file_path('persons.csv')) do |line_num, header_row|
24
+ ["name", "Job title"].all? {|cell| header_row.include?(cell) }
25
+ end
26
+ csv.pre_header_rows.first.last.must_equal "years of age"
27
+ csv.pre_header_rows.last.first.must_equal "bob"
28
+ File.basename(csv.pre_header_rows.last.csv_file).must_equal "persons.csv"
29
+ csv.pre_header_rows.last.csv_line.must_equal 2
30
+ end
31
+
26
32
  it "will raise an exception if it can't find the header row" do
27
- lambda { CSV::AutoParser.new(input_file) {|csv_line_number, header_row| csv_line_number == 0 }}.
33
+ lambda { CSV::AutoParser.new(fixture_file_path('persons.csv')) {|csv_line_number, header_row| csv_line_number == 0 }}.
28
34
  must_raise(CSV::AutoParser::HeaderRowNotFound)
29
35
  end
30
36
 
37
+ it "will not confuse column information with another csv which is parsed simultaneously" do
38
+ csv = CSV::AutoParser.new(fixture_file_path('persons.csv')) do |line_num, header_row|
39
+ ["name", "Job title"].all? {|cell| header_row.include?(cell) }
40
+ end
41
+ csv2 = CSV::AutoParser.new(fixture_file_path('persons2.csv')) do |line_num, header_row|
42
+ ["name", "Job title"].all? {|cell| header_row.include?(cell) }
43
+ end
44
+ csv.rows.length.must_equal 3
45
+ csv.rows.first.name.must_equal "Jon Smith"
46
+ csv.rows.first.job_title.must_equal "blacksmith"
47
+ csv.rows.last.age.to_i.must_equal 29
48
+ csv.rows[1].job_title.must_equal "farmer"
49
+ csv.rows[1].csv_line.must_equal 5
50
+
51
+ csv2.rows.length.must_equal 2
52
+ csv2.rows.first.name.must_equal "Kermy Frog"
53
+ csv2.rows.first.job_title.must_equal "frog"
54
+ csv2.rows.last.age.to_i.must_equal 19
55
+ File.basename(csv2.rows.first.csv_file).must_equal "persons2.csv"
56
+ end
57
+
31
58
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csv-autoparser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Delsol
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-24 00:00:00.000000000 Z
11
+ date: 2014-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -52,6 +52,7 @@ files:
52
52
  - lib/csv/autoparser.rb
53
53
  - lib/csv/autoparser/version.rb
54
54
  - test/fixtures/persons.csv
55
+ - test/fixtures/persons2.csv
55
56
  - test/minitest_helper.rb
56
57
  - test/test_csv/autoparser.rb
57
58
  homepage: ''
@@ -80,5 +81,6 @@ specification_version: 4
80
81
  summary: Can parse a CSV file automatically given a user specified header row.
81
82
  test_files:
82
83
  - test/fixtures/persons.csv
84
+ - test/fixtures/persons2.csv
83
85
  - test/minitest_helper.rb
84
86
  - test/test_csv/autoparser.rb