csvparser 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,10 @@
1
+ Copyright (c) 2006, Christopher Maujean and project contributors
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
+
6
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+ * Neither the name of the NGSLib nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9
+
10
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,118 @@
1
+ # $Id: csvparser.rb 44 2006-08-13 23:18:33Z cmaujean $
2
+ # See LICENSE for copyright information
3
+ require 'csv'
4
+ require 'stringio'
5
+
6
+ #
7
+ # CSVLine is used internally to represent the lines as rows. Do not use this class externally.
8
+ #
9
+
10
+ class CSVLine
11
+ def initialize(row)
12
+ @row = row.dup
13
+ end
14
+
15
+ def [](pos)
16
+ @row[pos]
17
+ end
18
+ end
19
+
20
+ #
21
+ # An enumerating, streaming CSV parser that has ORM-like features.
22
+ #
23
+ # ex:
24
+ # # this data:
25
+ #
26
+ # Id,Thing,Description
27
+ # 1,Toast,Happy morning vibes
28
+ # 2,Cigarette,Emergency response mechanism
29
+ # 3,Coffee,The new water
30
+ #
31
+ # # takes a string or already opened IO object
32
+ # csv = CSVParser.new(io)
33
+ #
34
+ # array = csv.to_a # read all the records into an array
35
+ # row = csv.find { |row| row.thing = "Coffee" } # or find a specific one
36
+ #
37
+ # # it takes the first line of the CSV file and creates methods from it.
38
+ # csv.each do |row|
39
+ # p row.id
40
+ # p row.title
41
+ # p row.description
42
+ # end
43
+ #
44
+ #
45
+
46
+ class CSVParser
47
+ include Enumerable
48
+
49
+ #
50
+ # Arguments:
51
+ # io: IO or String object for parsing. Uses readline and StringIO internally to represent a stream.
52
+ # fs: Field Separator. This is the separator that separates fields on each line.
53
+ # rs: Record Separator. These are the line endings for the file.
54
+ #
55
+
56
+ def initialize(io=$stdin, fs=nil, rs=nil)
57
+ @io = io.dup
58
+
59
+ if !@io.respond_to? :readline
60
+ @io = StringIO.open(@io)
61
+ end
62
+
63
+ #
64
+ # this weirdness corrects a small problem with @fs/@rs not being initialized.
65
+ #
66
+ @fs = fs ? fs.dup : nil
67
+ @rs = rs ? rs.dup : nil
68
+ @line_pos = 0
69
+ title = line_to_a
70
+ @methods = []
71
+
72
+
73
+ pos = 0
74
+
75
+ title.each do |method_name|
76
+ @methods.push [ method_name.downcase.gsub(/[^a-z0-9]+/, "_").to_sym, pos ]
77
+ pos += 1
78
+ end
79
+ end
80
+
81
+ #
82
+ # each is used to provide Enumerable with context, however you can use it yourself, just like Array#each.
83
+ #
84
+
85
+ def each
86
+ loop do
87
+ row = line_to_a
88
+ break unless row
89
+
90
+ rowobj = CSVLine.new row
91
+ inject_row rowobj
92
+ yield rowobj
93
+ end
94
+ end
95
+
96
+ private
97
+
98
+ def inject_row(rowobj)
99
+ @methods.each do |method_name|
100
+ rowobj.class.send(:define_method, method_name[0]) do
101
+ self[method_name[1]]
102
+ end
103
+ end
104
+ end
105
+
106
+ def line_to_a
107
+ line = []
108
+ begin
109
+ line = CSV.parse_line(@io.readline, @fs, @rs)
110
+ rescue EOFError => e
111
+ line = nil
112
+ end
113
+
114
+ line
115
+ end
116
+ end
117
+
118
+ # vi:sw=2 ts=2
@@ -0,0 +1,37 @@
1
+ require 'test/unit'
2
+ require 'csvparser'
3
+
4
+ class TestCSVParser < Test::Unit::TestCase
5
+ def setup
6
+ @csv_string = <<EOF
7
+ Id,Thing,Description
8
+ 1,Toast,Happy morning vibes
9
+ 2,Cigarette,Emergency response mechanism
10
+ 3,Coffee,The new water
11
+ EOF
12
+ @csv_file = File.open('csvparser/test/test.csv')
13
+ end
14
+
15
+ def test_accessors
16
+ csv = CSVParser.new(@csv_string)
17
+ array = csv.to_a
18
+ assert_equal(array[0].thing, "Toast")
19
+ assert_equal(array[1].description, "Emergency response mechanism")
20
+ assert_equal(array[2].id, "3")
21
+ end
22
+
23
+ def test_enumerable
24
+ csv = CSVParser.new(@csv_string)
25
+ assert_equal(csv.find { |row| row.thing == "Coffee" }.description, "The new water")
26
+ end
27
+
28
+ def test_file
29
+ csv = CSVParser.new(@csv_file)
30
+ array = csv.to_a
31
+ assert_equal(array[0].id, "1")
32
+ assert_equal(array[0].thing, "Toast")
33
+ assert_equal(array[0].description, "Happy morning vibes")
34
+ end
35
+ end
36
+
37
+ # vi:sw=2 ts=2
@@ -0,0 +1,4 @@
1
+ Id,Thing,Description
2
+ 1,Toast,Happy morning vibes
3
+ 2,Cigarette,Emergency response mechanism
4
+ 3,Coffee,The new water
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: csvparser
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2006-08-13 00:00:00 -07:00
8
+ summary: An enumerating, streaming CSV parser that has ORM-like features.
9
+ require_paths:
10
+ - lib
11
+ email: erik@hollensbe.com
12
+ homepage: http://rubyforge.org/projects/ngslib
13
+ rubyforge_project:
14
+ description:
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Erik Hollensbe
31
+ files:
32
+ - lib/csvparser.rb
33
+ - lib/LICENSE
34
+ - test/tc_csvparser.rb
35
+ - test/test.csv
36
+ test_files: []
37
+
38
+ rdoc_options: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ executables: []
43
+
44
+ extensions: []
45
+
46
+ requirements: []
47
+
48
+ dependencies: []
49
+