csv-autoparser 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dda3d9b42b637575933d1b307b5dd5f5687971c6
4
+ data.tar.gz: d28893032f72edf4824d4bed8d8c212cdab7a2d6
5
+ SHA512:
6
+ metadata.gz: 93cb849bab22ab289d59a7e1821cad3b8fcdea073793d8a804fa19b71d60d07884e44d1a4696f0daadca083ff9c40d3eb862da7a60b0565cf7180cf418c4f236
7
+ data.tar.gz: 659990a336d41c984cc5468b9729d6f47cb5f78e2a018be348f65092a8a387c4455b384f0ca3fcfe7666c925fe25f4ec7f089c6b254055ea9eca14b1a6bae314
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ .*.sw[opn]
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Benjamin Delsol
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # CSV::AutoParser
2
+
3
+ CSV::AutoParser automatically parses a CSV file given a user specified header row.
4
+
5
+ ## Installation
6
+
7
+ $ gem install csv-autoparser
8
+
9
+ ## Usage
10
+
11
+ # ID header row by CSV line number.
12
+ csv = CSV::AutoParser.new("my_file.csv") {|csv_line_number, header_row| csv_line_number == 1 }
13
+ csv.rows.each {|row| puts row.full_name }
14
+
15
+ # -OR- ID header row by column header names.
16
+ csv = CSV::AutoParser.new(input_file) do |line_num, header_row|
17
+ ["name", "Job title"].all? {|cell| header_row.include?(cell) }
18
+ end
19
+ puts csv.rows.first.name # => "Jon Smith"
20
+ csv.rows.first.job_title # => "blacksmith"
21
+
22
+ ## Contributing
23
+
24
+ 1. Fork it ( http://github.com/bdiz/csv-autoparser/fork )
25
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
26
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
27
+ 4. Push to the branch (`git push origin my-new-feature`)
28
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "rake/testtask"
2
+
3
+ Rake::TestTask.new(:test) do |t|
4
+ t.libs << "test"
5
+ t.test_files = FileList['test/test_csv/*.rb']
6
+ end
7
+
8
+ task :default => :test
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'csv/autoparser/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "csv-autoparser"
8
+ spec.version = CSV::AutoParser::VERSION
9
+ spec.authors = ["Ben Delsol"]
10
+ spec.email = [] # contact me via github (username: bdiz)
11
+ spec.summary = %q{Can parse a CSV file automatically given a user specified header row.}
12
+ spec.description = %q{}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "minitest"
23
+ end
@@ -0,0 +1,49 @@
1
+ require "csv"
2
+ require "csv/autoparser/version"
3
+
4
+ class CSV
5
+ class AutoParser
6
+ class Row
7
+
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
20
+ end
21
+ end
22
+
23
+ class HeaderRowNotFound < RuntimeError; end
24
+
25
+ attr_reader :pre_header_rows, :rows
26
+
27
+ def initialize file, &is_header
28
+ map = {}
29
+ csv_line_number = 0
30
+ @rows = []
31
+ @pre_header_rows = []
32
+ CSV.foreach(file) do |row|
33
+ csv_line_number += 1
34
+ if map.empty?
35
+ if is_header.call(csv_line_number, row)
36
+ row.each_index {|index| map[row[index]] = index }
37
+ Row.define_methods(map)
38
+ else
39
+ @pre_header_rows << row
40
+ end
41
+ else
42
+ @rows << Row.new(row)
43
+ end
44
+ end
45
+ raise HeaderRowNotFound, "Could not find header row in #{file}." if map.empty?
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,5 @@
1
+ class CSV
2
+ class AutoParser
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ "full-name",job,"years of age"
2
+ name,"Job title",age
3
+ "Jon Smith",blacksmith, 55
4
+ "Jimmy Johnson",farmer, 34
5
+ "Kimmy Kimmson","pig wrangler", 29
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'csv/autoparser'
3
+
4
+ require 'minitest/spec'
5
+ require 'minitest/autorun'
6
+
7
+ def fixture_file_path file_name
8
+ File.join(File.expand_path('../fixtures', __FILE__), file_name)
9
+ end
10
+
@@ -0,0 +1,31 @@
1
+ $LOAD_PATH.unshift File.expand_path('../..', __FILE__)
2
+ require 'minitest_helper'
3
+
4
+ describe CSV::AutoParser do
5
+
6
+ def input_file
7
+ fixture_file_path('persons.csv')
8
+ end
9
+
10
+ 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"
14
+ end
15
+
16
+ it "it can parse a csv automatically via header row id" do
17
+ csv = CSV::AutoParser.new(input_file) do |line_num, header_row|
18
+ ["name", "Job title"].all? {|cell| header_row.include?(cell) }
19
+ end
20
+ csv.rows.length.must_equal 3
21
+ csv.rows.first.name.must_equal "Jon Smith"
22
+ csv.rows.first.job_title.must_equal "blacksmith"
23
+ csv.rows.last.age.to_i.must_equal 29
24
+ end
25
+
26
+ 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 }}.
28
+ must_raise(CSV::AutoParser::HeaderRowNotFound)
29
+ end
30
+
31
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csv-autoparser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ben Delsol
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: ''
42
+ email: []
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - .gitignore
48
+ - LICENSE.txt
49
+ - README.md
50
+ - Rakefile
51
+ - csv-autoparser.gemspec
52
+ - lib/csv/autoparser.rb
53
+ - lib/csv/autoparser/version.rb
54
+ - test/fixtures/persons.csv
55
+ - test/minitest_helper.rb
56
+ - test/test_csv/autoparser.rb
57
+ homepage: ''
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.0.3
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Can parse a CSV file automatically given a user specified header row.
81
+ test_files:
82
+ - test/fixtures/persons.csv
83
+ - test/minitest_helper.rb
84
+ - test/test_csv/autoparser.rb