csvgen 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.markdown +15 -0
  2. data/lib/csvgen.rb +82 -0
  3. metadata +67 -0
data/README.markdown ADDED
@@ -0,0 +1,15 @@
1
+ # AnyAP Ruby Export Tool
2
+
3
+ This tool lets us write customer data exports in Ruby instead of XSLT.
4
+
5
+ Most exports are going to use the `nokogiri` gem.
6
+
7
+ Compare:
8
+
9
+ <xsl:text disable-output-escaping="yes">,&quot;</xsl:text>
10
+ <xsl:value-of disable-output-escaping="yes" select="AP_Vendor_no"/>
11
+ <xsl:text disable-output-escaping="yes">&quot;,</xsl:text>
12
+
13
+ vs.
14
+
15
+ row[3] = invoice.xpath("AP_Vendor_no").text
data/lib/csvgen.rb ADDED
@@ -0,0 +1,82 @@
1
+ # classes to support CSV exports form AnyAP
2
+
3
+ class Date
4
+ def mdy
5
+ strftime("%m/%d/%y")
6
+ end
7
+ end
8
+
9
+ class CSVDoc
10
+ attr_accessor :newline
11
+ attr_accessor :sep
12
+ attr_accessor :quote
13
+
14
+ def initialize()
15
+ @rows = []
16
+ self.newline = "\n"
17
+ self.sep = ","
18
+ self.quote = '"'
19
+ end
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
31
+ self
32
+ end
33
+
34
+ def to_s
35
+ @rows.collect { |r| r.to_s }.join(@newline)
36
+ end
37
+
38
+ def needs_quote(val)
39
+ val.include?(quote) || val.include?(sep)
40
+ end
41
+
42
+ def enquote(val)
43
+ val_s = val.to_s
44
+ if needs_quote(val_s) then
45
+ quote + val_s.gsub(quote, quote * 2) + quote
46
+ else
47
+ val_s
48
+ end
49
+ end
50
+ end
51
+
52
+ class CSVRow
53
+ def initialize(file)
54
+ @file = file
55
+ @cols = []
56
+ end
57
+
58
+ def col(*args)
59
+ val = args[0]
60
+ opts = args[1] || {:quote => true}
61
+ @cols.push(val)
62
+ self
63
+ end
64
+
65
+ def length=(n)
66
+ @cols = (0..(n - 1)).map { |idx| @cols[idx] || nil }
67
+ end
68
+
69
+ def []=(idx, val)
70
+ @cols[idx] = val
71
+ end
72
+
73
+ def to_s
74
+ @cols.collect { |c| @file.enquote(c) }.join(@file.sep)
75
+ end
76
+
77
+ def skip(n)
78
+ n.times do |nn|
79
+ col(nil)
80
+ end
81
+ end
82
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csvgen
3
+ version: !ruby/object:Gem::Version
4
+ hash: 15
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ version: "1.0"
10
+ platform: ruby
11
+ authors:
12
+ - John Cromartie
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-07-09 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: a simple lib oriented towards CSV generation only (no parsing)
22
+ email: jcromartie@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - README.markdown
31
+ - lib/csvgen.rb
32
+ has_rdoc: true
33
+ homepage:
34
+ licenses: []
35
+
36
+ post_install_message:
37
+ rdoc_options: []
38
+
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ hash: 3
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.3.7
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: simple CSV generation
66
+ test_files: []
67
+