flexible_csv 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +12 -0
- data/LICENSE +21 -0
- data/README.rdoc +84 -0
- data/lib/flexible_csv.rb +81 -0
- data/lib/hacks.rb +6 -0
- data/test/flexible_csv.rspec +36 -0
- metadata +69 -0
data/CHANGELOG.rdoc
ADDED
@@ -0,0 +1,12 @@
|
|
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
|
10
|
+
|
11
|
+
== Version 0.2.1
|
12
|
+
* Added documentation about using Adapter classes
|
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,84 @@
|
|
1
|
+
= FlexibleCsv
|
2
|
+
|
3
|
+
<em>Chris Powers, The Killswitch Collective (http://killswitchcollective.com/blog/authors/chris_powers)</em>
|
4
|
+
|
5
|
+
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.
|
6
|
+
|
7
|
+
== Examples
|
8
|
+
|
9
|
+
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.
|
10
|
+
|
11
|
+
require 'flexible_csv'
|
12
|
+
|
13
|
+
# Arbitrary CSV data
|
14
|
+
csv_data1 = %Q{Full Name, Email Address\nJohn Doe, john@doe.com}
|
15
|
+
csv_data2 = %Q{Email, Name\njohn@doe.com, John Doe}
|
16
|
+
|
17
|
+
parser = FlexibleCsv.new do |csv|
|
18
|
+
csv.column :full_name, "Name", "Full Name", "Client Name"
|
19
|
+
csv.column :email, "Email", "Email Address"
|
20
|
+
end
|
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
|
31
|
+
|
32
|
+
== Using Adapters
|
33
|
+
|
34
|
+
What if simply mapping header names to values is not enough? What if you need more translation logic? Instead of baking in complex rule functionality into FlexibleCsv, I am strongly suggesting users use their own adapter classes to wrap up this logic.
|
35
|
+
|
36
|
+
For example, let's say that some of your CSV files have a column for each contact's full name, but other have a separate column for first and last name:
|
37
|
+
|
38
|
+
require 'flexible_csv'
|
39
|
+
|
40
|
+
# Arbitrary CSV data
|
41
|
+
csv_data1 = %Q{Full Name\nJohn Doe}
|
42
|
+
csv_data2 = %Q{First Name, Last Name\nJohn,Doe}
|
43
|
+
|
44
|
+
parser = FlexibleCsv.new do |csv|
|
45
|
+
csv.column :full_name, "Name", "Full Name", "Client Name"
|
46
|
+
csv.column :first_name, "First Name", "First"
|
47
|
+
csv.column :last_name, "Last Name", "Last", "Surname"
|
48
|
+
end
|
49
|
+
|
50
|
+
class CsvAdapter
|
51
|
+
def initialize(row)
|
52
|
+
@row = row
|
53
|
+
end
|
54
|
+
|
55
|
+
def full_name
|
56
|
+
row.full_name || "#{row.first_name} #{row.last_name}"
|
57
|
+
end
|
58
|
+
|
59
|
+
def last_name
|
60
|
+
row.last_name || row.full_name.split(' ').last
|
61
|
+
end
|
62
|
+
|
63
|
+
def first_name
|
64
|
+
row.first_name || row.full_name.split(' ').first
|
65
|
+
end
|
66
|
+
|
67
|
+
def method_missing(method_name, *args)
|
68
|
+
row.send(method_name, *args)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
parser.parse(csv_data1).each do |row|
|
73
|
+
ad_row = CsvAdapter.new(row)
|
74
|
+
puts ad_row.full_name #=> 'John Doe'
|
75
|
+
puts ad_row.first_name #=> 'John'
|
76
|
+
puts ad_row.last_name #=> 'Doe'
|
77
|
+
end
|
78
|
+
|
79
|
+
parser.parse(csv_data2).each do |row|
|
80
|
+
ad_row = CsvAdapter.new(row)
|
81
|
+
puts ad_row.full_name #=> 'John Doe'
|
82
|
+
puts ad_row.first_name #=> 'John'
|
83
|
+
puts ad_row.last_name #=> 'Doe'
|
84
|
+
end
|
data/lib/flexible_csv.rb
ADDED
@@ -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
|
data/lib/hacks.rb
ADDED
@@ -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,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: flexible_csv
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
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 -06: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
|
+
- lib/hacks.rb
|
39
|
+
- test/flexible_csv.rspec
|
40
|
+
has_rdoc: true
|
41
|
+
homepage: http://killswitchcollective.com
|
42
|
+
licenses: []
|
43
|
+
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
61
|
+
requirements: []
|
62
|
+
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.3.5
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: The FlexibleCsv gem uses the FasterCSV gem to parse user created CSV files that may not have standard headers.
|
68
|
+
test_files: []
|
69
|
+
|