csvgen 1.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/csvgen.rb +30 -23
  2. 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(*args)
22
- length = args[0] || 0
23
- r = CSVRow.new(self)
24
- r.length = length
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 col(*args)
59
- val = args[0]
60
- opts = args[1] || {:quote => true}
61
- @cols.push(val)
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
- @cols[idx] = val
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
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csvgen
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- version: "1.0"
9
+ - 1
10
+ version: 1.0.1
10
11
  platform: ruby
11
12
  authors:
12
13
  - John Cromartie