chrisjpowers-flexible_csv 0.2 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +4 -1
- data/README.rdoc +57 -3
- data/lib/hacks.rb +6 -0
- metadata +2 -1
data/CHANGELOG.rdoc
CHANGED
@@ -6,4 +6,7 @@
|
|
6
6
|
* Minimal documentation
|
7
7
|
|
8
8
|
== Version 0.2
|
9
|
-
* Significant rewrite of Parser class, removed Row class and leveraging FasterCSV's Row class
|
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/README.rdoc
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
= FlexibleCsv
|
2
2
|
|
3
|
+
<em>Chris Powers, The Killswitch Collective (http://killswitchcollective.com/blog/authors/chris_powers)</em>
|
4
|
+
|
3
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.
|
4
6
|
|
5
7
|
== Examples
|
@@ -16,9 +18,7 @@ These CSV data snippets have the same data but are formatted much differently. U
|
|
16
18
|
csv.column :full_name, "Name", "Full Name", "Client Name"
|
17
19
|
csv.column :email, "Email", "Email Address"
|
18
20
|
end
|
19
|
-
|
20
|
-
collection1 = []
|
21
|
-
|
21
|
+
|
22
22
|
parser.parse(csv_data1).each do |row|
|
23
23
|
puts row.full_name #=> 'John Doe'
|
24
24
|
puts row.email #=> 'john@doe.com'
|
@@ -27,4 +27,58 @@ These CSV data snippets have the same data but are formatted much differently. U
|
|
27
27
|
parser.parse(csv_data2).each do |row|
|
28
28
|
puts row.full_name #=> 'John Doe'
|
29
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'
|
30
84
|
end
|
data/lib/hacks.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chrisjpowers-flexible_csv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Powers
|
@@ -35,6 +35,7 @@ files:
|
|
35
35
|
- CHANGELOG.rdoc
|
36
36
|
- LICENSE
|
37
37
|
- lib/flexible_csv.rb
|
38
|
+
- lib/hacks.rb
|
38
39
|
- test/flexible_csv.rspec
|
39
40
|
has_rdoc: true
|
40
41
|
homepage: http://killswitchcollective.com
|