ruport-util 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/INSTALL CHANGED
@@ -3,14 +3,18 @@
3
3
  ruport-util provides a home for marooned former parts of Ruby Reports,
4
4
  and newer fringe ideas.
5
5
 
6
- Support as of 2007.05.01:
6
+ Support as of 2007.06.07:
7
7
 
8
8
  * Basic Graphing (util/graph)
9
9
  * Report runner (util/report)
10
10
  * Report Mailing (util/mailer)
11
11
  * Report Manager (util/report_manager)
12
12
  * Invoices (util/invoice)
13
+ * PDF form helpers (util/pdf/form)
14
+ * ODS formatter for tables (util/ods)
15
+ * Benchmarking Tool (util/bench)
13
16
  * Ruport centric code generation via the rope command.
17
+ * csv to OpenDocument conversion via csv2ods
14
18
 
15
19
  See example/ for more information
16
20
 
@@ -27,14 +31,11 @@ It's on RubyForge:
27
31
  gem install ruport-util -y
28
32
 
29
33
  == Using
30
-
31
- To load all of the tools at once:
32
-
34
+
35
+ require "ruport"
33
36
  require "ruport/util"
34
37
 
35
- To load just one tool:
36
-
37
- require "ruport/util/graph"
38
+ # then just use whatever tools you want.
38
39
 
39
40
  == Help?
40
41
 
data/Rakefile CHANGED
@@ -1,24 +1,39 @@
1
- require "rake/rdoctask"
2
- require "rake/testtask"
3
- require "rake/gempackagetask"
4
-
5
1
  begin
6
2
  require "rubygems"
7
3
  rescue LoadError
8
4
  nil
9
5
  end
10
6
 
11
- task :default => [:test]
7
+ require 'rake'
8
+ require "rake/rdoctask"
9
+ require "rake/gempackagetask"
10
+
11
+ require 'spec/rake/spectask'
12
+
13
+ task :default => [:wraptest]
14
+
15
+ desc "Run all tests"
16
+ Spec::Rake::SpecTask.new('test') do |t|
17
+ t.spec_files = FileList['test/test_*.rb']
18
+ end
19
+
20
+ desc "Generate specdocs for examples for inclusion in RDoc"
21
+ Spec::Rake::SpecTask.new('specdoc') do |t|
22
+ t.spec_files = FileList['test/test_*.rb']
23
+ t.spec_opts = ["--format", "rdoc"]
24
+ t.out = 'EXAMPLES.rd'
25
+ end
12
26
 
13
- Rake::TestTask.new do |test|
14
- test.libs << "test"
15
- test.test_files = Dir[ "test/test_*.rb" ]
16
- test.verbose = true
27
+ desc "Generate HTML report for failing examples"
28
+ Spec::Rake::SpecTask.new('failing_examples_with_html') do |t|
29
+ t.spec_files = FileList['test/test_*.rb']
30
+ t.spec_opts = ["--format", "html:failing_examples.html", "--diff"]
31
+ t.fail_on_error = false
17
32
  end
18
33
 
19
34
  spec = Gem::Specification.new do |spec|
20
35
  spec.name = "ruport-util"
21
- spec.version = "0.6.0"
36
+ spec.version = "0.7.0"
22
37
  spec.platform = Gem::Platform::RUBY
23
38
  spec.summary = "A set of tools and helper libs for Ruby Reports"
24
39
  spec.files = Dir.glob("{example,lib,test,bin}/**/**/*") +
@@ -28,7 +43,7 @@ spec = Gem::Specification.new do |spec|
28
43
 
29
44
  spec.test_files = Dir[ "test/test_*.rb" ]
30
45
  spec.bindir = "bin"
31
- spec.executables = FileList["rope"]
46
+ spec.executables = FileList["rope", "csv2ods"]
32
47
  spec.has_rdoc = true
33
48
  spec.extra_rdoc_files = %w{INSTALL}
34
49
  spec.rdoc_options << '--title' << 'ruport-util Documentation' <<
@@ -67,3 +82,43 @@ begin
67
82
  rescue LoadError
68
83
  nil
69
84
  end
85
+
86
+ ## RSpec Wrapper
87
+
88
+ require 'test/helper/layout'
89
+
90
+ class String
91
+ def /(obj)
92
+ File.join(self, obj.to_s)
93
+ end
94
+ end
95
+
96
+ SPEC_BASE = File.expand_path('test')
97
+
98
+ # ignore files with these paths
99
+ ignores = [ './helper/*', './helper.rb' ]
100
+
101
+ files = Dir[SPEC_BASE/'**'/'*.rb']
102
+ ignores.each do |ignore|
103
+ ignore_files = Dir[SPEC_BASE/ignore]
104
+ ignore_files.each do |ignore_file|
105
+ files.delete File.expand_path(ignore_file)
106
+ end
107
+ end
108
+
109
+ files.sort!
110
+
111
+ spec_layout = Hash.new{|h,k| h[k] = []}
112
+
113
+ files.each do |file|
114
+ name = file.gsub(/^#{SPEC_BASE}/, '.')
115
+ dir_name = File.dirname(name)[2..-1] || './'
116
+ task_name = 'wrap' + ([:test] + dir_name.split('/')).join(':')
117
+ spec_layout[task_name] << file
118
+ end
119
+
120
+ desc "Test all"
121
+ task "wraptest" => [] do
122
+ wrap = SpecWrap.new(*spec_layout.values.flatten)
123
+ wrap.run
124
+ end
data/bin/csv2ods ADDED
@@ -0,0 +1,53 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'ostruct'
5
+ require 'fileutils'
6
+
7
+ begin
8
+ require 'rubygems'
9
+ rescue LoadError
10
+ end
11
+
12
+ require 'ruport'
13
+ require 'ruport/util'
14
+
15
+ options = OpenStruct.new(:output => 'output.ods')
16
+
17
+ opts = OptionParser.new do |opt|
18
+ ruby_version = "ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
19
+ ruport_util_version = "Ruport Util Version #{Ruport::Util::VERSION}"
20
+ ruport_version = "Ruport Version #{Ruport::VERSION}, on #{ruby_version}"
21
+ version = "#{ruport_util_version} - #{ruport_version}"
22
+
23
+ opt.banner = "Usage: csv2ods [ OPTIONS ] input.csv [ output.ods ]"
24
+ opt.define_head(version)
25
+
26
+ opt.separator ''
27
+ opt.separator 'Common options:'
28
+
29
+ opt.on_tail('-h', '--help', 'Show this message') do
30
+ puts opts
31
+ exit
32
+ end
33
+
34
+ opt.on_tail('-v', '--version', 'Show version') do
35
+ puts version
36
+ exit
37
+ end
38
+ end
39
+
40
+ opts.parse!(ARGV)
41
+
42
+ input = ARGV.shift
43
+ options.input = input if input
44
+ output = ARGV.shift
45
+ options.output = output if output
46
+
47
+ unless options.input and options.output
48
+ puts opts
49
+ exit
50
+ end
51
+
52
+ ods = Table(options.input).to_ods(:tempfile => true)
53
+ FileUtils.cp(ods.path, options.output)
Binary file
@@ -4,13 +4,17 @@ require "ruport/util"
4
4
 
5
5
  class GraphReport < Ruport::Report
6
6
 
7
+ renders_as_graph
8
+
7
9
  def generate
8
10
  graph = Ruport::Graph(:column_names => %w[a b c d e])
9
11
  graph.add_line [1,2,3,4,5], :name => "foo"
10
12
  graph.add_line [11,22,70,2,19], :name => "bar"
11
- graph.to_svg
13
+ return graph
12
14
  end
13
15
 
14
16
  end
15
17
 
16
- GraphReport.run { |r| r.write("foo.svg") }
18
+ GraphReport.generate do |r|
19
+ r.save_as("foo.svg")
20
+ end
@@ -24,4 +24,5 @@ class SampleReport < Ruport::Report
24
24
  end
25
25
  end
26
26
 
27
- SampleReport.run { |r| r.write "out.pdf" }
27
+ a = SampleReport.new
28
+ File.open("invoice.pdf","wb") { |f| f << a.generate }
data/example/mailer.rb CHANGED
@@ -2,6 +2,9 @@ require "ruport"
2
2
  require "ruport/util"
3
3
  require "fileutils"
4
4
  class MyReport < Ruport::Report
5
+
6
+ renders_as_table
7
+
5
8
  def prepare
6
9
  add_mailer :default,
7
10
  :host => "mail.adelphia.net",
@@ -9,17 +12,14 @@ class MyReport < Ruport::Report
9
12
  end
10
13
 
11
14
  def generate
12
- Table(%w[a b c]) { |t| t << [1,2,3] << [4,5,6] }.to_pdf
15
+ Table(%w[a b c]) { |t| t << [1,2,3] << [4,5,6] }
13
16
  end
14
17
 
15
- def cleanup
16
- FileUtils.rm("foo.pdf")
17
- end
18
18
  end
19
19
 
20
- MyReport.run do |res|
21
- res.write "foo.pdf";
22
- res.send_to("gregory.t.brown@gmail.com") do |mail|
20
+ MyReport.generate do |report|
21
+ report.save_as "foo.pdf"
22
+ report.send_to("gregory.t.brown@gmail.com") do |mail|
23
23
  mail.subject = "Sample report"
24
24
  mail.attach "foo.pdf"
25
25
  mail.text = <<-EOS
@@ -5,7 +5,7 @@ class MyReport < Ruport::Report
5
5
 
6
6
  acts_as_managed_report
7
7
 
8
- def generate
8
+ def content
9
9
  "Hello Mike"
10
10
  end
11
11
 
@@ -15,7 +15,7 @@ class YourReport < Ruport::Report
15
15
 
16
16
  acts_as_managed_report
17
17
 
18
- def generate
18
+ def content
19
19
  "Hello Joe"
20
20
  end
21
21
 
@@ -25,12 +25,12 @@ class HisReport < Ruport::Report
25
25
 
26
26
  acts_as_managed_report
27
27
 
28
- def generate
28
+ def content
29
29
  "Hello Robert"
30
30
  end
31
31
 
32
32
  end
33
33
 
34
34
  %w[MyReport YourReport HisReport].each do |report|
35
- puts Ruport::ReportManager[report].run
36
- end
35
+ puts Ruport::ReportManager[report].generate { |r| r.content }
36
+ end
data/example/ods.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'ruport'
2
+ require "ruport/util"
3
+
4
+ csv = %{
5
+ first col,second col,third col
6
+ first row,cell 1,cell 2
7
+ second row,cell 3,cell 4
8
+ }.strip
9
+
10
+
11
+ f = Table(:string => csv).to_ods(:tempfile => true)
12
+ FileUtils.cp(f.path,"out.ods")
@@ -175,15 +175,12 @@ REP = <<EOR
175
175
  require "lib/init"
176
176
  class #{class_name} < Ruport::Report
177
177
 
178
- def generate
178
+ def renderable_data
179
179
 
180
180
  end
181
181
 
182
182
  end
183
183
 
184
- if __FILE__ == $0
185
- puts #{class_name}.run
186
- end
187
184
  EOR
188
185
 
189
186
  TEST = <<EOR
@@ -296,13 +293,7 @@ README = <<END_README
296
293
 
297
294
  Rope provides you with a number of simple utilities that script away
298
295
  much of your boilerplate code, and also provide useful tools for
299
- development, such as automatic test running and a way to check your
300
- SQL queries for validity. Additionally, you'll get logging for free
301
- and you can share a common configuration file between applications
302
- in the same project.
303
-
304
- Though each tool on it's own isn't complicated, having them all
305
- working together can be a major win.
296
+ development
306
297
 
307
298
  # The Basics
308
299
 
@@ -14,6 +14,14 @@ end
14
14
 
15
15
  module Ruport
16
16
 
17
+ module Renderer::Hooks
18
+ module ClassMethods
19
+ def renders_as_graph
20
+ renders_with Ruport::Renderer::Graph
21
+ end
22
+ end
23
+ end
24
+
17
25
  def Graph(*args)
18
26
  Graph.new(*args)
19
27
  end
@@ -0,0 +1,80 @@
1
+ require 'cgi'
2
+ require 'tmpdir'
3
+
4
+ require 'zip/zip'
5
+
6
+ module Ruport
7
+
8
+ class Formatter::ODS < Formatter
9
+ BLANK_ODS = File.join(Ruport::Util::BASEDIR, 'example', 'data', 'blank.ods')
10
+
11
+ renders :ods, :for => [ Renderer::Row, Renderer::Table]
12
+
13
+ def prepare_table
14
+ output << %{<?xml version="1.0" encoding="UTF-8"?>
15
+ <office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" office:version="1.0">
16
+ <office:scripts/>
17
+ <office:font-face-decls/>
18
+ <office:automatic-styles/>
19
+ <office:body>
20
+ <office:spreadsheet>
21
+ <table:table table:name="Ruport" table:print="false">
22
+ <office:forms form:automatic-focus="false" form:apply-design-mode="false"/>
23
+ }
24
+ end
25
+
26
+ def build_table_header
27
+ if @options.show_table_headers
28
+ table_row{ build_cells(data.column_names) }
29
+ end
30
+ end
31
+
32
+ def build_table_body
33
+ render_data_by_row
34
+ end
35
+
36
+ def build_row
37
+ table_row{ build_cells(data.to_a) }
38
+ end
39
+
40
+ def table_row
41
+ output << %{ <table:table-row>\n}
42
+ yield
43
+ output << %{ </table:table-row>\n}
44
+ end
45
+
46
+ def build_cells(values)
47
+ values.each do |value|
48
+ value = CGI.escapeHTML(value.to_s)
49
+ output << %{ <table:table-cell>\n}
50
+ output << %{ <text:p>#{value}</text:p>\n}
51
+ output << %{ </table:table-cell>\n}
52
+ end
53
+ end
54
+
55
+ def finalize_table
56
+ output << %{ </table:table>
57
+ </office:spreadsheet>
58
+ </office:body>
59
+ </office:document-content>}
60
+
61
+ @tempfile = Tempfile.new('output.ods')
62
+
63
+ File.open(BLANK_ODS){|bo| @tempfile.print(bo.read(1024)) until bo.eof? }
64
+ @tempfile.close
65
+
66
+ zip = Zip::ZipFile.open(@tempfile.path)
67
+ zip.get_output_stream('content.xml') do |cxml|
68
+ cxml.write(output)
69
+ end
70
+ zip.close
71
+
72
+ options.io =
73
+ if options.tempfile
74
+ @tempfile
75
+ else
76
+ File.read(@tempfile.path)
77
+ end
78
+ end
79
+ end
80
+ end