csvgen 1.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/csvgen.rb +30 -23
- metadata +3 -2
data/lib/csvgen.rb
CHANGED
@@ -18,16 +18,10 @@ class CSVDoc
|
|
18
18
|
self.quote = '"'
|
19
19
|
end
|
20
20
|
|
21
|
-
def row(
|
22
|
-
|
23
|
-
r
|
24
|
-
r
|
25
|
-
begin
|
26
|
-
yield(r)
|
27
|
-
@rows.push(r)
|
28
|
-
rescue
|
29
|
-
@rows.push("Error in row: #{$!.inspect}")
|
30
|
-
end
|
21
|
+
def row(len_or_headers)
|
22
|
+
r = CSVRow.new(self, len_or_headers)
|
23
|
+
yield(r)
|
24
|
+
@rows.push(r)
|
31
25
|
self
|
32
26
|
end
|
33
27
|
|
@@ -50,15 +44,28 @@ class CSVDoc
|
|
50
44
|
end
|
51
45
|
|
52
46
|
class CSVRow
|
53
|
-
def initialize(file)
|
47
|
+
def initialize(file, len_or_headers)
|
54
48
|
@file = file
|
55
49
|
@cols = []
|
50
|
+
@headers = {}
|
51
|
+
if len_or_headers.is_a? Numeric then
|
52
|
+
self.length = len_or_headers
|
53
|
+
else
|
54
|
+
self.headers = len_or_headers
|
55
|
+
self.length = len_or_headers.length
|
56
|
+
end
|
56
57
|
end
|
57
|
-
|
58
|
-
def
|
59
|
-
|
60
|
-
|
61
|
-
|
58
|
+
|
59
|
+
def headers=(hdrs)
|
60
|
+
hdrs.each_with_index do |h, ii|
|
61
|
+
@headers[h] = ii
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def push(*args)
|
66
|
+
args.each do |x|
|
67
|
+
@cols.push(x)
|
68
|
+
end
|
62
69
|
self
|
63
70
|
end
|
64
71
|
|
@@ -67,16 +74,16 @@ class CSVRow
|
|
67
74
|
end
|
68
75
|
|
69
76
|
def []=(idx, val)
|
70
|
-
|
77
|
+
if idx.is_a? Numeric then
|
78
|
+
@cols[idx] = val
|
79
|
+
else
|
80
|
+
hidx = @headers[idx]
|
81
|
+
raise "Header field #{idx.inspect} not found" if hidx.nil?
|
82
|
+
@cols[hidx] = val
|
83
|
+
end
|
71
84
|
end
|
72
85
|
|
73
86
|
def to_s
|
74
87
|
@cols.collect { |c| @file.enquote(c) }.join(@file.sep)
|
75
88
|
end
|
76
|
-
|
77
|
-
def skip(n)
|
78
|
-
n.times do |nn|
|
79
|
-
col(nil)
|
80
|
-
end
|
81
|
-
end
|
82
89
|
end
|
metadata
CHANGED