csvgen 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +11 -12
- data/lib/csvgen.rb +17 -1
- metadata +3 -3
data/README.markdown
CHANGED
@@ -1,15 +1,14 @@
|
|
1
|
-
#
|
1
|
+
# CSV generation library
|
2
2
|
|
3
|
-
This tool lets us write customer data exports in Ruby instead of
|
3
|
+
This tool lets us write customer data exports in Ruby instead of
|
4
|
+
XSLT. It's publicly hosted just for fun.
|
4
5
|
|
5
|
-
|
6
|
+
Example usage:
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
row[3] = invoice.xpath("AP_Vendor_no").text
|
8
|
+
require 'csvgen'
|
9
|
+
doc = CSVDoc.new
|
10
|
+
doc.quote = "'"
|
11
|
+
|
12
|
+
doc.row { |r| r[0] = "hey"; r[3] = "ho" }
|
13
|
+
doc.row { |r| r[1] = "let's"; r[2] = "go" }
|
14
|
+
puts doc.to_s
|
data/lib/csvgen.rb
CHANGED
@@ -1,11 +1,14 @@
|
|
1
|
-
# classes to support CSV exports form AnyAP
|
2
1
|
|
2
|
+
# Extensions to Date for our export purposes
|
3
3
|
class Date
|
4
|
+
# Print date as Month/Day/Year style
|
4
5
|
def mdy
|
5
6
|
strftime("%m/%d/%y")
|
6
7
|
end
|
7
8
|
end
|
8
9
|
|
10
|
+
# CSV document class. Extend this class to add custom behavior, such
|
11
|
+
# as special quoting rules.
|
9
12
|
class CSVDoc
|
10
13
|
attr_accessor :newline
|
11
14
|
attr_accessor :sep
|
@@ -18,6 +21,9 @@ class CSVDoc
|
|
18
21
|
self.quote = '"'
|
19
22
|
end
|
20
23
|
|
24
|
+
# Append a row to the document, using the given fixed length, or
|
25
|
+
# array of headers. Expects a block, which will be passed the new
|
26
|
+
# (empty) row object.
|
21
27
|
def row(len_or_headers)
|
22
28
|
r = CSVRow.new(self, len_or_headers)
|
23
29
|
yield(r)
|
@@ -29,10 +35,14 @@ class CSVDoc
|
|
29
35
|
@rows.collect { |r| r.to_s }.join(@newline)
|
30
36
|
end
|
31
37
|
|
38
|
+
# Override this method if you need to implement special quoting
|
39
|
+
# rules. By default it only quotes data which contains the quote
|
40
|
+
# character itself.
|
32
41
|
def needs_quote(val)
|
33
42
|
val.include?(quote) || val.include?(sep)
|
34
43
|
end
|
35
44
|
|
45
|
+
# Return quoted CSV value string for val.
|
36
46
|
def enquote(val)
|
37
47
|
val_s = val.to_s
|
38
48
|
if needs_quote(val_s) then
|
@@ -43,6 +53,7 @@ class CSVDoc
|
|
43
53
|
end
|
44
54
|
end
|
45
55
|
|
56
|
+
# Represents a single CSV row.
|
46
57
|
class CSVRow
|
47
58
|
def initialize(file, len_or_headers)
|
48
59
|
@file = file
|
@@ -56,12 +67,14 @@ class CSVRow
|
|
56
67
|
end
|
57
68
|
end
|
58
69
|
|
70
|
+
# Set the header array
|
59
71
|
def headers=(hdrs)
|
60
72
|
hdrs.each_with_index do |h, ii|
|
61
73
|
@headers[h] = ii
|
62
74
|
end
|
63
75
|
end
|
64
76
|
|
77
|
+
# Append columns
|
65
78
|
def push(*args)
|
66
79
|
args.each do |x|
|
67
80
|
@cols.push(x)
|
@@ -69,10 +82,13 @@ class CSVRow
|
|
69
82
|
self
|
70
83
|
end
|
71
84
|
|
85
|
+
# Set the fixed column length, for when you don't have headers
|
72
86
|
def length=(n)
|
73
87
|
@cols = (0..(n - 1)).map { |idx| @cols[idx] || nil }
|
74
88
|
end
|
75
89
|
|
90
|
+
# Set value by numeric index or by header (should be a value from
|
91
|
+
# header array)
|
76
92
|
def []=(idx, val)
|
77
93
|
if idx.is_a? Numeric then
|
78
94
|
@cols[idx] = val
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csvgen
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 2
|
10
|
+
version: 1.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- John Cromartie
|