chrisjpowers-flexible_csv 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,9 @@
1
+ = FlexibleCsv CHANGELOG
2
+
3
+ == Version 0.1
4
+ * First try
5
+ * Super lame tests, but they work
6
+ * Minimal documentation
7
+
8
+ == Version 0.2
9
+ * Significant rewrite of Parser class, removed Row class and leveraging FasterCSV's Row class
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2009 Chris Powers, The Killswitch Collective
2
+ http://killswitchcollective.com
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,30 @@
1
+ = FlexibleCsv
2
+
3
+ The FlexibleCsv gem uses the FasterCSV gem to parse user created CSV files that may not have standard headers. For example, you know there's an email address column somewhere in there, but it may be called "Email" or "Email Address" or "email-address", and it might be in the first column, but it might be in the third.
4
+
5
+ == Examples
6
+
7
+ These CSV data snippets have the same data but are formatted much differently. Using FlexibleCsv to map possible column names to data nodes, these differences become negligible.
8
+
9
+ require 'flexible_csv'
10
+
11
+ # Arbitrary CSV data
12
+ csv_data1 = %Q{Full Name, Email Address\nJohn Doe, john@doe.com}
13
+ csv_data2 = %Q{Email, Name\njohn@doe.com, John Doe}
14
+
15
+ parser = FlexibleCsv.new do |csv|
16
+ csv.column :full_name, "Name", "Full Name", "Client Name"
17
+ csv.column :email, "Email", "Email Address"
18
+ end
19
+
20
+ collection1 = []
21
+
22
+ parser.parse(csv_data1).each do |row|
23
+ puts row.full_name #=> 'John Doe'
24
+ puts row.email #=> 'john@doe.com'
25
+ end
26
+
27
+ parser.parse(csv_data2).each do |row|
28
+ puts row.full_name #=> 'John Doe'
29
+ puts row.email #=> 'john@doe.com'
30
+ end
@@ -0,0 +1,81 @@
1
+ class FlexibleCsv
2
+ require 'faster_csv'
3
+ require 'hacks.rb'
4
+
5
+ # a Hash where the key is the method name, and the value is the array of possible headers
6
+ attr_reader :column_hash
7
+
8
+ def initialize
9
+ @column_hash = {}
10
+ yield self
11
+ end
12
+
13
+ # Developed by Chris Powers, Killswitch Collective on 02/17/2009
14
+ #
15
+ # <b>Description:</b> Parses a CSV data source, either a String or an IO object
16
+ #
17
+ # <b>Arguments:</b> CSV formatted String or IO of CSV file
18
+ #
19
+ # <b>Returns:</b> Parser object
20
+ #
21
+ # <em>Syntax: @parser = @flexible_csv.parse(File.open('path/to/file.csv'))</em>
22
+ def parse(data)
23
+ Parser.new(data, self)
24
+ end
25
+
26
+ # Developed by Chris Powers, Killswitch Collective on 02/17/2009
27
+ #
28
+ # <b>Description:</b>
29
+ #
30
+ # <b>Arguments:</b> The hash key / method name and any number of possible header labels as strings
31
+ #
32
+ # <b>Returns:</b> nil
33
+ #
34
+ # <em>Syntax: csv.column :name, "Full Name", "Name", "Client Name"</em>
35
+ def column(id, *args)
36
+ @column_hash[id] = args.map {|item| clean_up(item) }
37
+ nil
38
+ end
39
+
40
+ # Developed by Chris Powers, Killswitch Collective on 02/17/2009
41
+ #
42
+ # <b>Description:</b> Helper method to remove whitespace, capitalization and punctuation
43
+ #
44
+ # <b>Arguments:</b> a string
45
+ #
46
+ # <b>Returns:</b> a formatted string
47
+ #
48
+ # <em>Syntax: clean_up "My Column!" #=> 'mycolumn'</em>
49
+ def clean_up(str)
50
+ str.to_s.downcase.gsub(/[^a-z0-9]/, '')
51
+ end
52
+
53
+ # Wrapper class around the FasterCSV object
54
+ class Parser
55
+
56
+ def initialize(data, csv_parent)
57
+ column_headers = []
58
+
59
+ fcsv = FasterCSV.new(data.is_a?(String) ? data : data.read, :headers => true)
60
+
61
+ # reformat headers to remove punctuation, capitalization and white space
62
+ # Change header labels to the key used in the column config
63
+ fcsv.header_convert do |field|
64
+ f = csv_parent.clean_up(field)
65
+ csv_parent.column_hash.each_pair do |k,v|
66
+ f = k.to_s and break if v.include?(f)
67
+ end
68
+ f
69
+ end
70
+
71
+ @table = fcsv.read
72
+ end
73
+
74
+ # proxy other methods to the @table object
75
+ def method_missing(method_name, *args, &block)
76
+ @table.send(method_name, *args, &block)
77
+ end
78
+
79
+ end
80
+
81
+ end
@@ -0,0 +1,36 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'activesupport'
4
+
5
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'flexible_csv.rb')
6
+
7
+ describe FlexibleCsv do
8
+
9
+ before :each do
10
+ @csv_data1 = %Q{Full Name, Email Address\nJohn Doe, john@doe.com\nJane Doe, jane@doe.com}
11
+ @csv_data2 = %Q{First Name, Last Name, Email\nJohn, Doe, john@doe.com\nJane, Doe, jane@doe.com}
12
+
13
+ @parser = FlexibleCsv.new do |csv|
14
+ csv.column :full_name, "Name", "Full Name", "Client Name"
15
+ csv.column :first_name, "First Name", "First"
16
+ csv.column :last_name, "Last Name", "Last"
17
+ csv.column :email, "Email", "Email Address"
18
+ end
19
+ end
20
+
21
+ it "should work with example 1" do
22
+ @parser.parse(@csv_data1).each do |row|
23
+ row["full_name"].should_not be_blank
24
+ row["email"].should_not be_blank
25
+ end
26
+ end
27
+
28
+ it "should work with example 2" do
29
+ @parser.parse(@csv_data2).each do |row|
30
+ row.first_name.should_not be_blank
31
+ row.last_name.should_not be_blank
32
+ row.email.should_not be_blank
33
+ end
34
+ end
35
+
36
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chrisjpowers-flexible_csv
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.2"
5
+ platform: ruby
6
+ authors:
7
+ - Chris Powers
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-17 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: fastercsv
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.3
24
+ version:
25
+ description:
26
+ email: chris@killswitchcollective.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - README.rdoc
35
+ - CHANGELOG.rdoc
36
+ - LICENSE
37
+ - lib/flexible_csv.rb
38
+ - test/flexible_csv.rspec
39
+ has_rdoc: true
40
+ homepage: http://killswitchcollective.com
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.2.0
62
+ signing_key:
63
+ specification_version: 2
64
+ summary: The FlexibleCsv gem uses the FasterCSV gem to parse user created CSV files that may not have standard headers.
65
+ test_files: []
66
+