ruport-util 0.12.0 → 0.13.0

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/Rakefile CHANGED
@@ -33,7 +33,7 @@ end
33
33
 
34
34
  spec = Gem::Specification.new do |spec|
35
35
  spec.name = "ruport-util"
36
- spec.version = "0.12.0"
36
+ spec.version = "0.13.0"
37
37
  spec.platform = Gem::Platform::RUBY
38
38
  spec.summary = "A set of tools and helper libs for Ruby Reports"
39
39
  spec.files = Dir.glob("{example,lib,test,bin}/**/**/*") +
@@ -48,7 +48,7 @@ spec = Gem::Specification.new do |spec|
48
48
  spec.extra_rdoc_files = %w{INSTALL}
49
49
  spec.rdoc_options << '--title' << 'ruport-util Documentation' <<
50
50
  '--main' << 'INSTALL' << '-q'
51
- spec.add_dependency('ruport', ">=1.2.3")
51
+ spec.add_dependency('ruport', ">=1.4.0")
52
52
  spec.add_dependency('mailfactory',">=1.2.3")
53
53
  spec.add_dependency('rubyzip','>=0.9.1')
54
54
  spec.author = "Gregory Brown"
@@ -1,10 +1,11 @@
1
+ require "rubygems"
1
2
  require "ruport"
2
3
  require "ruport/util"
3
4
 
4
5
  class SampleReport < Ruport::Report
5
6
  include Invoice
6
7
 
7
- def generate
8
+ def renderable_data(format)
8
9
  render_invoice do |i|
9
10
  i.data = Table("Item Number","Description","Price") { |t|
10
11
  t << %w[101 Erlang\ The\ Movie $1000.00]
@@ -25,4 +26,4 @@ class SampleReport < Ruport::Report
25
26
  end
26
27
 
27
28
  a = SampleReport.new
28
- File.open("invoice.pdf","wb") { |f| f << a.generate }
29
+ a.save_as("invoice.pdf")
data/lib/ruport/util.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Ruport
2
2
  module Util
3
- VERSION = "0.12.0"
3
+ VERSION = "0.13.0"
4
4
 
5
5
  file = __FILE__
6
6
  file = File.readlink(file) if File.symlink?(file)
@@ -21,3 +21,5 @@ require "ruport/util/pdf/form"
21
21
  require "ruport/util/ods"
22
22
  require "ruport/util/query"
23
23
  require "ruport/util/xls"
24
+ require "ruport/util/ods_table"
25
+ require "ruport/util/xls_table"
@@ -22,7 +22,7 @@ module Ruport
22
22
  end
23
23
 
24
24
  def build_customer_header
25
- move_cursor -5
25
+ move_cursor(-5)
26
26
  rounded_text_box(options.customer_info) { |o|
27
27
  o.radius = 3
28
28
  o.width = options.header_width || 200
@@ -99,7 +99,7 @@ module Ruport
99
99
 
100
100
  horizontal_line left_boundary + 20, right_boundary - 25
101
101
 
102
- move_cursor -10
102
+ move_cursor(-10)
103
103
 
104
104
  add_text(options.comments,:justification => :center,
105
105
  :left => 0, :right => 0 )
@@ -0,0 +1,136 @@
1
+ require 'ruport'
2
+
3
+ # === Overview
4
+ #
5
+ # This class extends the core class Ruport::Data::Table and adds support for loading Openoffice
6
+ # spreadsheet files using roo. The idea is to get data from speadsheets that may contain
7
+ # already calculated values entered by non-programmers.
8
+ #
9
+ # Once your data is in a Table object, it can be manipulated
10
+ # to suit your needs, then used to build a report.
11
+ #
12
+ # Copyright (C) 2007, Wes Hays
13
+ # All Rights Reserved.
14
+ #
15
+ class Ruport::Data::Table
16
+
17
+ # === Overview
18
+ #
19
+ # This module provides facilities for creating tables from Openoffice spreadsheet file (ods).
20
+ #
21
+ module FromODS
22
+ # Loads a ods file directly into a Table using the roo library.
23
+ #
24
+ # Example:
25
+ #
26
+ # # Load data from Openoffice ods file with defaults
27
+ # table = Table.load_ods('myspreadsheet.ods')
28
+ #
29
+ # # do not assume the data has column names - default is false.
30
+ # table = Table.load_ods('myspreadsheet.ods',{:has_column_names => false})
31
+ #
32
+ # # Select sheet - default is the first sheet.
33
+ # table = Table.load_ods('myspreadsheet.ods', {:select_sheet => 1})
34
+ #
35
+ # # Start row - default is the first row. Use this to override where
36
+ # the first row should start.
37
+ # table = Table.load_ods('myspreadsheet.ods', {:start_row => 1})
38
+ #
39
+ def load_ods(ods_file, options={})
40
+ get_table_from_ods_file(ods_file, options)
41
+ end
42
+
43
+ # Creates a Table from an Openoffice object (from roo library).
44
+ #
45
+ # Example:
46
+ #
47
+ # # parse openoffice object with defaults.
48
+ # table = Table.parse_ods(openoffice_object)
49
+ #
50
+ # # do not assume the data has column names.
51
+ # table = Table.parse_ods(openoffice_object,{:has_column_names => false})
52
+ #
53
+ # # Select sheet - default is the first sheet.
54
+ # table = Table.parse_ods(openoffice_object, {:select_sheet => 1})
55
+ #
56
+ # # Start row - default is the first row. Use this to override where
57
+ # the first row should start.
58
+ # table = Table.parse_ods('myspreadsheet.ods', {:start_row => 1})
59
+ #
60
+ def parse_ods(ods_object, options={})
61
+ get_table_from_ods(ods_object, options)
62
+ end
63
+
64
+ private
65
+
66
+ def get_table_from_ods_file(ods_file, options) #:nodoc:
67
+ require 'roo'
68
+ oo = Openoffice.new(ods_file)
69
+ get_table_from_ods(oo, options)
70
+ end
71
+
72
+ def get_table_from_ods(oo, options) #:nodoc:
73
+ options = {:has_column_names => true,
74
+ :select_sheet => oo.sheets.first,
75
+ :start_row => 0}.merge(options)
76
+ oo.default_sheet = options[:select_sheet]
77
+
78
+ options[:start_row] = options[:start_row].to_i + 1 unless options[:start_row].nil?
79
+ start_row = options[:start_row]
80
+
81
+ raise 'start_row must be greater than or equal to zero' if options[:start_row].to_i < 0
82
+
83
+ last_row_index_zero = oo.last_row - 1
84
+ raise "start_row must be less than or equal to #{last_row_index_zero}" if !oo.last_row.nil? and
85
+ (options[:start_row].to_i > oo.last_row)
86
+
87
+ table = self.new(options) do |feeder|
88
+
89
+ if options[:has_column_names] == true
90
+ feeder.data.column_names = oo.row(start_row)
91
+ start_row = start_row + 1
92
+ end
93
+
94
+ unless oo.last_row.nil?
95
+ start_row.upto(oo.last_row) do |row|
96
+ tempArr = []
97
+ 1.upto(oo.last_column) do |col|
98
+ tempArr << oo.cell(row,col)
99
+ end
100
+ feeder << tempArr
101
+ end
102
+ end
103
+
104
+ end
105
+
106
+ return table
107
+ end
108
+
109
+ end
110
+
111
+ extend FromODS
112
+
113
+ end
114
+
115
+
116
+ module Kernel
117
+
118
+ alias :RuportTableMethod :Table
119
+
120
+ # Updates the Ruport interface for creating Data::Tables with
121
+ # the ability to pass in a ODS file or Roo Openoffice object.
122
+ #
123
+ # t = Table("myspreadsheet.ods")
124
+ # t = Table("myspreadsheet.ods", :has_column_names => true)
125
+ def Table(*args,&block)
126
+ table=
127
+ case(args[0])
128
+ when /\.ods/
129
+ Ruport::Data::Table.load_ods(*args)
130
+ else
131
+ RuportTableMethod(*args,&block)
132
+ end
133
+
134
+ return table
135
+ end
136
+ end
@@ -66,13 +66,24 @@ module Ruport
66
66
  # # explicitly use a file, even if it doesn't end in .sql
67
67
  # Ruport::Query.new(:file => "foo")
68
68
  #
69
+ # # query with parameter substitution
70
+ # Ruport::Query.new("select * from fo where bar=?", :params => [1234])
71
+ # Ruport::Query.new(:file => "foo", :params => [1234])
72
+ #
73
+ # # query with parameter substitution (ActiveRecord style)
74
+ # Ruport::Query.new(["select * from fo where bar=?", 1234])
75
+ #
69
76
  def initialize(sql, options={})
70
77
  if sql.kind_of?(Hash)
71
78
  options = { :source => :default }.merge(sql)
72
79
  sql = options[:file] || options[:string]
73
80
  else
81
+ if sql.kind_of?(Array)
82
+ options[:params] = sql[1..-1]
83
+ sql = sql.first
84
+ end
74
85
  options = { :source => :default, :string => sql }.merge(options)
75
- options[:file] = sql if sql =~ /.sql$/
86
+ options[:file] = sql if sql =~ /\.sql$/
76
87
  end
77
88
  origin = options[:file] ? :file : :string
78
89
 
@@ -0,0 +1,136 @@
1
+ require 'ruport'
2
+
3
+ # === Overview
4
+ #
5
+ # This class extends the core class Ruport::Data::Table and adds support for loading Excel
6
+ # spreadsheet files using roo. The idea is to get data from speadsheets that may contain
7
+ # already calculated values entered by non-programmers.
8
+ #
9
+ # Once your data is in a Table object, it can be manipulated
10
+ # to suit your needs, then used to build a report.
11
+ #
12
+ # Copyright (C) 2008, Wes Hays
13
+ # All Rights Reserved.
14
+ #
15
+ class Ruport::Data::Table
16
+
17
+ # === Overview
18
+ #
19
+ # This module provides facilities for creating tables from Excel spreadsheet file (xls).
20
+ #
21
+ module FromXLS
22
+ # Loads a xls file directly into a Table using the roo library.
23
+ #
24
+ # Example:
25
+ #
26
+ # # Load data from an Excel xls file with defaults
27
+ # table = Table.load_xls('myspreadsheet.xls')
28
+ #
29
+ # # do not assume the data has column names - default is false.
30
+ # table = Table.load_xls('myspreadsheet.xls',{:has_column_names => false})
31
+ #
32
+ # # Select sheet - default is the first sheet.
33
+ # table = Table.load_xls('myspreadsheet.xls', {:select_sheet => 1})
34
+ #
35
+ # # Start row - default is the first row. Use this to override where
36
+ # the first row should start.
37
+ # table = Table.load_xls('myspreadsheet.xls', {:start_row => 1})
38
+ #
39
+ def load_xls(xls_file, options={})
40
+ get_table_from_xls_file(xls_file, options)
41
+ end
42
+
43
+ # Creates a Table from an Excel object (from roo library).
44
+ #
45
+ # Example:
46
+ #
47
+ # # parse excel object with defaults.
48
+ # table = Table.parse_xls(excel_object)
49
+ #
50
+ # # do not assume the data has column names.
51
+ # table = Table.parse_xls(excel_object,{:has_column_names => false})
52
+ #
53
+ # # Select sheet - default is the first sheet.
54
+ # table = Table.parse_xls(excel_object, {:select_sheet => 1})
55
+ #
56
+ # # Start row - default is the first row. Use this to override where
57
+ # the first row should start.
58
+ # table = Table.parse_xls('myspreadsheet.xls', {:start_row => 1})
59
+ #
60
+ def parse_xls(xls_object, options={})
61
+ get_table_from_xls(xls_object, options)
62
+ end
63
+
64
+ private
65
+
66
+ def get_table_from_xls_file(xls_file, options) #:nodoc:
67
+ require 'roo'
68
+ oo = Excel.new(xls_file)
69
+ get_table_from_xls(oo, options)
70
+ end
71
+
72
+ def get_table_from_xls(oo, options) #:nodoc:
73
+ options = {:has_column_names => true,
74
+ :select_sheet => oo.sheets.first,
75
+ :start_row => 0}.merge(options)
76
+ oo.default_sheet = options[:select_sheet]
77
+
78
+ options[:start_row] = options[:start_row].to_i + 1 unless options[:start_row].nil?
79
+ start_row = options[:start_row]
80
+
81
+ raise 'start_row must be greater than or equal to zero' if options[:start_row].to_i < 0
82
+
83
+ last_row_index_zero = oo.last_row - 1
84
+ raise "start_row must be less than or equal to #{last_row_index_zero}" if !oo.last_row.nil? and
85
+ (options[:start_row].to_i > oo.last_row)
86
+
87
+ table = self.new(options) do |feeder|
88
+
89
+ if options[:has_column_names] == true
90
+ feeder.data.column_names = oo.row(start_row)
91
+ start_row = start_row + 1
92
+ end
93
+
94
+ unless oo.last_row.nil?
95
+ start_row.upto(oo.last_row) do |row|
96
+ tempArr = []
97
+ 1.upto(oo.last_column) do |col|
98
+ tempArr << oo.cell(row,col)
99
+ end
100
+ feeder << tempArr
101
+ end
102
+ end
103
+
104
+ end
105
+
106
+ return table
107
+ end
108
+
109
+ end
110
+
111
+ extend FromXLS
112
+
113
+ end
114
+
115
+
116
+ module Kernel
117
+
118
+ alias :RuportTableMethod2 :Table
119
+
120
+ # Updates the Ruport interface for creating Data::Tables with
121
+ # the ability to pass in a XLS file or Roo Excel object.
122
+ #
123
+ # t = Table("myspreadsheet.xls")
124
+ # t = Table("myspreadsheet.xls", :has_column_names => true)
125
+ def Table(*args,&block)
126
+ table=
127
+ case(args[0])
128
+ when /\.xls/
129
+ Ruport::Data::Table.load_xls(*args)
130
+ else
131
+ RuportTableMethod2(*args,&block)
132
+ end
133
+
134
+ return table
135
+ end
136
+ end
Binary file
Binary file
@@ -0,0 +1,219 @@
1
+ # Copyright (C) 2007, Wes Hays
2
+ # All Rights Reserved.
3
+
4
+ require 'test/helper'
5
+ testcase_requires 'roo'
6
+
7
+ describe 'Ruport::Data::TableFromODS' do
8
+ before(:each) do
9
+ @ods_file = 'test/samples/people.ods'
10
+ @csv_file = 'test/samples/data.csv'
11
+
12
+ @ods_file_column_names = %w(Name Age DOB)
13
+ @rows = [ ['Andy', 27.0, Date.parse('01/20/1980')],
14
+ ['Bob', 26.0, Date.parse('02/11/1981')],
15
+ ['Charlie', 20.0, Date.parse('03/14/1987')],
16
+ ['David', 73.0, Date.parse('04/26/1997')] ]
17
+
18
+ @ods_file_column_names2 = %w(Name Age Pet_Type)
19
+ @rows2 = [ ['Tigger', 3.0, 'Cat'],
20
+ ['Chai', 4.0, 'Dog'],
21
+ ['Rusky', 6.0, 'Dog'],
22
+ ['Sam', 13.0, 'Dog'] ]
23
+ end
24
+
25
+ # ==== File check ====
26
+ # Raise error if file is not found
27
+ it "should raise if ods file is not found" do
28
+ lambda do
29
+ Ruport::Data::Table.load_ods('people.ods')
30
+ end.should raise_error
31
+ end
32
+
33
+ # Raise error if file is not found
34
+ it "shouldn't raise if ods file exists" do
35
+ lambda do
36
+ Ruport::Data::Table.load_ods(@ods_file)
37
+ end.should_not raise_error
38
+ end
39
+
40
+
41
+ # ==== Constructor check ====
42
+ it "shouldn't be nil if a ods file is passed" do
43
+ table = Table(@ods_file)
44
+ table.should_not be_nil
45
+ end
46
+
47
+ it "shouldn't be nil if a Openoffice object is passed" do
48
+ oo = Openoffice.new(@ods_file)
49
+ oo.default_sheet = oo.sheets.first
50
+ table = Table(oo) # This will be passed to the base Ruport::Data::Table class.
51
+ table.should_not be_nil
52
+ end
53
+
54
+ it "shouldn't be nil if a Ruport::Data::Table parameter is passed" do
55
+ table = Table(@csv_file) # Pass cs file
56
+ table.should_not be_nil
57
+ end
58
+
59
+
60
+ # ==== Constructor check with options params ====
61
+ it "shouldn't be nil if a ods file is passed with options params" do
62
+ table = Table(@ods_file, {:has_column_names => false})
63
+ table.should_not be_nil
64
+ end
65
+
66
+ it "shouldn't be nil if a Openoffice object is passed with options params using parse_ods method" do
67
+ oo = Openoffice.new(@ods_file)
68
+ oo.default_sheet = oo.sheets.first
69
+ table = Ruport::Data::Table.parse_ods(oo, {:has_column_names => false})
70
+ table.should_not be_nil
71
+ end
72
+
73
+ it "shouldn't be nil if a Ruport::Data::Table parameter is passed with options params" do
74
+ table = Table(@csv_file, {:has_column_names => false}) # Pass cs file
75
+ table.should_not be_nil
76
+ end
77
+
78
+ it "should raise if start_row is less than zero" do
79
+ lambda do
80
+ Table(@ods_file, {:start_row => -2})
81
+ end.should raise_error
82
+ end
83
+
84
+ it "should raise if start_row is greater than the number of rows (starting at 0) in the spreadsheet" do
85
+ lambda do
86
+ Table(@ods_file, {:start_row => 20})
87
+ end.should raise_error
88
+ end
89
+
90
+
91
+ # ==== Table load check ====
92
+
93
+ # Output:
94
+ # +-----------------------------+
95
+ # | Name | Age | DOB |
96
+ # | Andy | 27.0 | 1980-01-20 |
97
+ # | Bob | 26.0 | 1981-02-11 |
98
+ # | Charlie | 20.0 | 1987-03-14 |
99
+ # | David | 73.0 | 1997-04-26 |
100
+ # +-----------------------------+
101
+ it "table should be valid without column names loaded from ods file starting at the row 1 (index 0) - column names will be data" do
102
+ # Load data from ods file but do not load column headers.
103
+ table = Table(@ods_file, {:has_column_names => false, :start_row => 0})
104
+ table.should_not be_nil
105
+ table.column_names.should == []
106
+
107
+ # Add headers to the first position
108
+ @rows.insert(0, @ods_file_column_names)
109
+
110
+ table.each { |r| r.to_a.should == @rows.shift
111
+ r.attributes.should == [0, 1, 2] }
112
+ end
113
+
114
+ # Output:
115
+ # +-----------------------------+
116
+ # | Bob | 26.0 | 1981-02-11 |
117
+ # | Charlie | 20.0 | 1987-03-14 |
118
+ # | David | 73.0 | 1997-04-26 |
119
+ # +-----------------------------+
120
+ it "table should be valid without column names loaded from ods file starting at row 3 (index 2)" do
121
+ # Load data from ods file but do not load column headers.
122
+ # Will start at Row 3 (index 2): ['Bob', 26.0, Date.parse('02/11/1981')]
123
+ table = Table(@ods_file, {:has_column_names => false, :start_row => 2})
124
+ table.should_not be_nil
125
+ table.column_names.should == []
126
+
127
+ # The header row has not been included yet so don't worry about that one
128
+ # just delete the first row in @rows.
129
+ @rows.delete_at(0) # delete ['Andy', 27.0, Date.parse('01/20/1980')]
130
+
131
+ table.each { |r| r.to_a.should == @rows.shift
132
+ r.attributes.should == [0, 1, 2] }
133
+ end
134
+
135
+ # Output:
136
+ # +-----------------------------+
137
+ # | Name | Age | DOB |
138
+ # | Andy | 27.0 | 1980-01-20 |
139
+ # | Bob | 26.0 | 1981-02-11 |
140
+ # | Charlie | 20.0 | 1987-03-14 |
141
+ # | David | 73.0 | 1997-04-26 |
142
+ # +-----------------------------+
143
+ it "table should be valid without column names loaded from ods file" do
144
+ # Load data from ods file but do not load column headers.
145
+ table = Table(@ods_file, {:has_column_names => false})
146
+ table.should_not be_nil
147
+ table.column_names.should == []
148
+
149
+ # Add headers to the first position
150
+ @rows.insert(0, @ods_file_column_names)
151
+
152
+ table.each { |r| r.to_a.should == @rows.shift
153
+ r.attributes.should == [0, 1, 2] }
154
+ end
155
+
156
+ # Output:
157
+ # +-----------------------------+
158
+ # | Name | Age | DOB |
159
+ # +-----------------------------+
160
+ # | Andy | 27.0 | 1980-01-20 |
161
+ # | Bob | 26.0 | 1981-02-11 |
162
+ # | Charlie | 20.0 | 1987-03-14 |
163
+ # | David | 73.0 | 1997-04-26 |
164
+ # +-----------------------------+
165
+ it "table should be valid with column names loaded from ods file" do
166
+ # Load data from ods file but do not load column headers.
167
+ table = Table(@ods_file)
168
+ table.should_not be_nil
169
+ table.column_names.should == @ods_file_column_names
170
+
171
+ table.each { |r| r.to_a.should == @rows.shift
172
+ r.attributes.should == @ods_file_column_names }
173
+ end
174
+
175
+ # Output:
176
+ # +--------------------------+
177
+ # | Name | Age | Pet_Type |
178
+ # +--------------------------+
179
+ # | Tigger | 3.0 | Cat |
180
+ # | Chai | 4.0 | Dog |
181
+ # | Rusky | 6.0 | Dog |
182
+ # | Sam | 13.0 | Dog |
183
+ # +--------------------------+
184
+ it "table should be valid with column names loaded from ods file using Sheet2" do
185
+ # Load data from ods file but do not load column headers.
186
+ table = Table(@ods_file, {:select_sheet => 'Sheet2'})
187
+ table.should_not be_nil
188
+ table.column_names.should == @ods_file_column_names2
189
+
190
+ table.each { |r| r.to_a.should == @rows2.shift
191
+ r.attributes.should == @ods_file_column_names2 }
192
+ end
193
+
194
+ # Output:
195
+ # +--------------------------+
196
+ # | Name | Age | Pet_Type |
197
+ # +--------------------------+
198
+ # | Tigger | 3.0 | Cat |
199
+ # | Chai | 4.0 | Dog |
200
+ # | Rusky | 6.0 | Dog |
201
+ # | Sam | 13.0 | Dog |
202
+ # +--------------------------+
203
+ it "should be valid if an Openoffice object is passed using parse_ods method" do
204
+ oo = Openoffice.new(@ods_file)
205
+ oo.default_sheet = oo.sheets.first
206
+ table = Ruport::Data::Table.parse_ods(oo)
207
+ table.should_not be_nil
208
+
209
+ table.column_names.should == @ods_file_column_names
210
+
211
+ table.each { |r| r.to_a.should == @rows.shift
212
+ r.attributes.should == @ods_file_column_names }
213
+ end
214
+
215
+ end
216
+
217
+
218
+
219
+
data/test/test_query.rb CHANGED
@@ -29,6 +29,7 @@ describe "A Query" do
29
29
  :plain => Ruport::Query.new(@sql[0]),
30
30
  :sourced => Ruport::Query.new(@sql[0], :source => :alternative),
31
31
  :paramed => Ruport::Query.new(@sql[0], :params => [ 42 ]),
32
+ :paramed_ar => Ruport::Query.new([@sql[0], 69, 777]),
32
33
  :raw => Ruport::Query.new(@sql[0], :row_type => :raw),
33
34
  :resultless => Ruport::Query.new(@sql[1]),
34
35
  :multi => Ruport::Query.new(@sql[2]),
@@ -52,13 +53,20 @@ describe "A Query" do
52
53
  query.execute.should == nil
53
54
  end
54
55
 
55
- it "should allow excute to accept parameters" do
56
+ it "should allow execute to accept parameters" do
56
57
  query = @query[:paramed]
57
58
  setup_mock_dbi(1, :params => [ 42 ])
58
59
 
59
60
  query.execute.should == nil
60
61
  end
61
62
 
63
+ it "should allow execute to accept parameters (ActiveRecord style)" do
64
+ query = @query[:paramed_ar]
65
+ setup_mock_dbi(1, :params => [ 69, 777 ])
66
+
67
+ query.execute.should == nil
68
+ end
69
+
62
70
  it "should return nil for empty results" do
63
71
  query = @query[:resultless]
64
72
  setup_mock_dbi(1, :resultless => true, :sql => @sql[1])
@@ -0,0 +1,236 @@
1
+ # Copyright (C) 2008, Wes Hays
2
+ # All Rights Reserved.
3
+
4
+ require 'test/helper'
5
+ testcase_requires 'roo'
6
+
7
+ describe 'Ruport::Data::TableFromXLS' do
8
+ before(:each) do
9
+ @xls_file = 'test/samples/people.xls'
10
+ @csv_file = 'test/samples/data.csv'
11
+
12
+ @xls_file_column_names = %w(Name Age DOB)
13
+ # This test will pass once Spreadsheet and Roo support
14
+ # formulas in an excel file.
15
+ # @rows = [ ['Andy', 27.0, Date.parse('01/20/1980')],
16
+ # ['Bob', 26.0, Date.parse('02/11/1981')],
17
+ # ['Charlie', 20.0, Date.parse('03/14/1987')],
18
+ # ['David', 73.0, Date.parse('04/26/1997')] ]
19
+
20
+ # Delete this once Roo supports formulas in an excel file.
21
+ @rows = [ ['Andy', 27.0, Date.parse('01/20/1980')],
22
+ ['Bob', 26.0, Date.parse('02/11/1981')],
23
+ ['Charlie', 20.0, Date.parse('03/14/1987')],
24
+ ['David', nil, Date.parse('04/26/1997')] ]
25
+
26
+
27
+ @xls_file_column_names2 = %w(Name Age Pet_Type)
28
+ # This test will pass once Spreadsheet and Roo support
29
+ # formulas in an excel file.
30
+ # @rows2 = [ ['Tigger', 3.0, 'Cat'],
31
+ # ['Chai', 4.0, 'Dog'],
32
+ # ['Rusky', 6.0, 'Dog'],
33
+ # ['Sam', 13.0, 'Dog'] ]
34
+
35
+ # Delete this once Roo supports formulas in an excel file.
36
+ @xls_file_column_names2 = %w(Name Age Pet_Type)
37
+ @rows2 = [ ['Tigger', 3.0, 'Cat'],
38
+ ['Chai', 4.0, 'Dog'],
39
+ ['Rusky', 6.0, 'Dog'],
40
+ ['Sam', nil, 'Dog'] ]
41
+ end
42
+
43
+ # ==== File check ====
44
+ # Raise error if file is not found
45
+ it "should raise if xls file is not found" do
46
+ lambda do
47
+ Ruport::Data::Table.load_xls('people.xls')
48
+ end.should raise_error
49
+ end
50
+
51
+ # Raise error if file is not found
52
+ it "shouldn't raise if xls file exists" do
53
+ lambda do
54
+ Ruport::Data::Table.load_xls(@xls_file)
55
+ end.should_not raise_error
56
+ end
57
+
58
+
59
+ # ==== Constructor check ====
60
+ it "shouldn't be nil if a xls file is passed" do
61
+ table = Table(@xls_file)
62
+ table.should_not be_nil
63
+ end
64
+
65
+ it "shouldn't be nil if a Excel object is passed" do
66
+ oo = Excel.new(@xls_file)
67
+ oo.default_sheet = oo.sheets.first
68
+ table = Table(oo) # This will be passed to the base Ruport::Data::Table class.
69
+ table.should_not be_nil
70
+ end
71
+
72
+ it "shouldn't be nil if a Ruport::Data::Table parameter is passed" do
73
+ table = Table(@csv_file) # Pass cs file
74
+ table.should_not be_nil
75
+ end
76
+
77
+
78
+ # ==== Constructor check with options params ====
79
+ it "shouldn't be nil if a xls file is passed with options params" do
80
+ table = Table(@xls_file, {:has_column_names => false})
81
+ table.should_not be_nil
82
+ end
83
+
84
+ it "shouldn't be nil if a Excel object is passed with options params using parse_xls method" do
85
+ oo = Excel.new(@xls_file)
86
+ oo.default_sheet = oo.sheets.first
87
+ table = Ruport::Data::Table.parse_xls(oo, {:has_column_names => false})
88
+ table.should_not be_nil
89
+ end
90
+
91
+ it "shouldn't be nil if a Ruport::Data::Table parameter is passed with options params" do
92
+ table = Table(@csv_file, {:has_column_names => false}) # Pass cs file
93
+ table.should_not be_nil
94
+ end
95
+
96
+ it "should raise if start_row is less than zero" do
97
+ lambda do
98
+ Table(@xls_file, {:start_row => -2})
99
+ end.should raise_error
100
+ end
101
+
102
+ it "should raise if start_row is greater than the number of rows (starting at 0) in the spreadsheet" do
103
+ lambda do
104
+ Table(@xls_file, {:start_row => 20})
105
+ end.should raise_error
106
+ end
107
+
108
+
109
+ # ==== Table load check ====
110
+
111
+ # Output:
112
+ # +-----------------------------+
113
+ # | Name | Age | DOB |
114
+ # | Andy | 27.0 | 1980-01-20 |
115
+ # | Bob | 26.0 | 1981-02-11 |
116
+ # | Charlie | 20.0 | 1987-03-14 |
117
+ # | David | 73.0 | 1997-04-26 |
118
+ # +-----------------------------+
119
+ it "table should be valid without column names loaded from xls file starting at the row 1 (index 0) - column names will be data" do
120
+ # Load data from xls file but do not load column headers.
121
+ table = Table(@xls_file, {:has_column_names => false, :start_row => 0})
122
+ table.should_not be_nil
123
+ table.column_names.should == []
124
+
125
+ # Add headers to the first position
126
+ @rows.insert(0, @xls_file_column_names)
127
+
128
+ table.each { |r| r.to_a.should == @rows.shift
129
+ r.attributes.should == [0, 1, 2] }
130
+ end
131
+
132
+ # Output:
133
+ # +-----------------------------+
134
+ # | Bob | 26.0 | 1981-02-11 |
135
+ # | Charlie | 20.0 | 1987-03-14 |
136
+ # | David | 73.0 | 1997-04-26 |
137
+ # +-----------------------------+
138
+ it "table should be valid without column names loaded from xls file starting at row 3 (index 2)" do
139
+ # Load data from xls file but do not load column headers.
140
+ # Will start at Row 3 (index 2): ['Bob', 26.0, Date.parse('02/11/1981')]
141
+ table = Table(@xls_file, {:has_column_names => false, :start_row => 2})
142
+ table.should_not be_nil
143
+ table.column_names.should == []
144
+
145
+ # The header row has not been included yet so don't worry about that one
146
+ # just delete the first row in @rows.
147
+ @rows.delete_at(0) # delete ['Andy', 27.0, Date.parse('01/20/1980')]
148
+
149
+ table.each { |r| r.to_a.should == @rows.shift
150
+ r.attributes.should == [0, 1, 2] }
151
+ end
152
+
153
+ # Output:
154
+ # +-----------------------------+
155
+ # | Name | Age | DOB |
156
+ # | Andy | 27.0 | 1980-01-20 |
157
+ # | Bob | 26.0 | 1981-02-11 |
158
+ # | Charlie | 20.0 | 1987-03-14 |
159
+ # | David | 73.0 | 1997-04-26 |
160
+ # +-----------------------------+
161
+ it "table should be valid without column names loaded from xls file" do
162
+ # Load data from xls file but do not load column headers.
163
+ table = Table(@xls_file, {:has_column_names => false})
164
+ table.should_not be_nil
165
+ table.column_names.should == []
166
+
167
+ # Add headers to the first position
168
+ @rows.insert(0, @xls_file_column_names)
169
+
170
+ table.each { |r| r.to_a.should == @rows.shift
171
+ r.attributes.should == [0, 1, 2] }
172
+ end
173
+
174
+ # Output:
175
+ # +-----------------------------+
176
+ # | Name | Age | DOB |
177
+ # +-----------------------------+
178
+ # | Andy | 27.0 | 1980-01-20 |
179
+ # | Bob | 26.0 | 1981-02-11 |
180
+ # | Charlie | 20.0 | 1987-03-14 |
181
+ # | David | 73.0 | 1997-04-26 |
182
+ # +-----------------------------+
183
+ it "table should be valid with column names loaded from xls file" do
184
+ # Load data from xls file but do not load column headers.
185
+ table = Table(@xls_file)
186
+ table.should_not be_nil
187
+ table.column_names.should == @xls_file_column_names
188
+
189
+ table.each { |r| r.to_a.should == @rows.shift
190
+ r.attributes.should == @xls_file_column_names }
191
+ end
192
+
193
+ # Output:
194
+ # +--------------------------+
195
+ # | Name | Age | Pet_Type |
196
+ # +--------------------------+
197
+ # | Tigger | 3.0 | Cat |
198
+ # | Chai | 4.0 | Dog |
199
+ # | Rusky | 6.0 | Dog |
200
+ # | Sam | 13.0 | Dog |
201
+ # +--------------------------+
202
+ it "table should be valid with column names loaded from xls file using Sheet2" do
203
+ # Load data from xls file but do not load column headers.
204
+ table = Table(@xls_file, {:select_sheet => 'Sheet2'})
205
+ table.should_not be_nil
206
+ table.column_names.should == @xls_file_column_names2
207
+
208
+ table.each { |r| r.to_a.should == @rows2.shift
209
+ r.attributes.should == @xls_file_column_names2 }
210
+ end
211
+
212
+ # Output:
213
+ # +--------------------------+
214
+ # | Name | Age | Pet_Type |
215
+ # +--------------------------+
216
+ # | Tigger | 3.0 | Cat |
217
+ # | Chai | 4.0 | Dog |
218
+ # | Rusky | 6.0 | Dog |
219
+ # | Sam | 13.0 | Dog |
220
+ # +--------------------------+
221
+ it "should be valid if an Excel object is passed using parse_xls method" do
222
+ oo = Excel.new(@xls_file)
223
+ oo.default_sheet = oo.sheets.first
224
+ table = Ruport::Data::Table.parse_xls(oo)
225
+ table.should_not be_nil
226
+
227
+ table.column_names.should == @xls_file_column_names
228
+
229
+ table.each { |r| r.to_a.should == @rows.shift
230
+ r.attributes.should == @xls_file_column_names }
231
+ end
232
+
233
+ end
234
+
235
+
236
+
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.2
2
+ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: ruport-util
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.12.0
7
- date: 2007-12-25 00:00:00 -05:00
6
+ version: 0.13.0
7
+ date: 2008-02-08 00:00:00 -05:00
8
8
  summary: A set of tools and helper libs for Ruby Reports
9
9
  require_paths:
10
10
  - lib
@@ -30,71 +30,79 @@ authors:
30
30
  - Gregory Brown
31
31
  files:
32
32
  - example/data
33
- - example/data/amline_settings.xml
34
- - example/data/blank.xlsx
35
- - example/data/amline_graph.xml
36
- - example/data/blank.ods
37
- - example/invoice_report.rb
38
33
  - example/draw_graph.rb
39
34
  - example/form.rb
35
+ - example/graph_report.rb
40
36
  - example/gruff_report.rb
41
- - example/ods.rb
42
- - example/managed_report.rb
37
+ - example/invoice_report.rb
43
38
  - example/mailer.rb
44
- - example/graph_report.rb
39
+ - example/managed_report.rb
40
+ - example/ods.rb
41
+ - example/data/amline_graph.xml
42
+ - example/data/amline_settings.xml
43
+ - example/data/blank.ods
44
+ - example/data/blank.xlsx
45
+ - lib/open_flash_chart.rb
45
46
  - lib/ruport
46
47
  - lib/ruport/util
47
- - lib/ruport/util/graph
48
- - lib/ruport/util/graph/scruffy.rb
49
- - lib/ruport/util/graph/amline.rb
50
- - lib/ruport/util/graph/o_f_c.rb
51
- - lib/ruport/util/graph/gruff.rb
52
- - lib/ruport/util/pdf
53
- - lib/ruport/util/pdf/form.rb
54
- - lib/ruport/util/generator.rb
48
+ - lib/ruport/util.rb
55
49
  - lib/ruport/util/bench.rb
50
+ - lib/ruport/util/generator.rb
51
+ - lib/ruport/util/graph
56
52
  - lib/ruport/util/graph.rb
53
+ - lib/ruport/util/invoice.rb
54
+ - lib/ruport/util/mailer.rb
57
55
  - lib/ruport/util/ods.rb
56
+ - lib/ruport/util/ods_table.rb
57
+ - lib/ruport/util/pdf
58
58
  - lib/ruport/util/query.rb
59
+ - lib/ruport/util/report.rb
59
60
  - lib/ruport/util/report_manager.rb
60
61
  - lib/ruport/util/xls.rb
61
- - lib/ruport/util/mailer.rb
62
- - lib/ruport/util/report.rb
63
- - lib/ruport/util/invoice.rb
64
- - lib/ruport/util.rb
65
- - lib/open_flash_chart.rb
62
+ - lib/ruport/util/xls_table.rb
63
+ - lib/ruport/util/graph/amline.rb
64
+ - lib/ruport/util/graph/gruff.rb
65
+ - lib/ruport/util/graph/o_f_c.rb
66
+ - lib/ruport/util/graph/scruffy.rb
67
+ - lib/ruport/util/pdf/form.rb
66
68
  - test/helper
67
- - test/helper/layout.rb
68
- - test/helper/wrap.rb
69
+ - test/helper.rb
69
70
  - test/samples
70
- - test/samples/data.csv
71
- - test/samples/foo.rtxt
72
- - test/test_report_manager.rb
71
+ - test/test_format_ods.rb
73
72
  - test/test_format_xls.rb
74
- - test/helper.rb
75
- - test/test_query.rb
76
- - test/test_hpricot_traverser.rb
77
- - test/test_graph_renderer.rb
78
73
  - test/test_graph_ofc.rb
74
+ - test/test_graph_renderer.rb
75
+ - test/test_hpricot_traverser.rb
76
+ - test/test_invoice.rb
79
77
  - test/test_mailer.rb
78
+ - test/test_ods_table.rb
79
+ - test/test_query.rb
80
80
  - test/test_report.rb
81
- - test/test_invoice.rb
82
- - test/test_format_ods.rb
81
+ - test/test_report_manager.rb
82
+ - test/test_xls_table.rb
83
+ - test/helper/layout.rb
84
+ - test/helper/wrap.rb
85
+ - test/samples/data.csv
86
+ - test/samples/foo.rtxt
87
+ - test/samples/people.ods
88
+ - test/samples/people.xls
83
89
  - bin/csv2ods
84
90
  - bin/rope
85
91
  - Rakefile
86
92
  - INSTALL
87
93
  test_files:
88
- - test/test_report_manager.rb
94
+ - test/test_format_ods.rb
89
95
  - test/test_format_xls.rb
90
- - test/test_query.rb
91
- - test/test_hpricot_traverser.rb
92
- - test/test_graph_renderer.rb
93
96
  - test/test_graph_ofc.rb
97
+ - test/test_graph_renderer.rb
98
+ - test/test_hpricot_traverser.rb
99
+ - test/test_invoice.rb
94
100
  - test/test_mailer.rb
101
+ - test/test_ods_table.rb
102
+ - test/test_query.rb
95
103
  - test/test_report.rb
96
- - test/test_invoice.rb
97
- - test/test_format_ods.rb
104
+ - test/test_report_manager.rb
105
+ - test/test_xls_table.rb
98
106
  rdoc_options:
99
107
  - --title
100
108
  - ruport-util Documentation
@@ -118,7 +126,7 @@ dependencies:
118
126
  requirements:
119
127
  - - ">="
120
128
  - !ruby/object:Gem::Version
121
- version: 1.2.3
129
+ version: 1.4.0
122
130
  version:
123
131
  - !ruby/object:Gem::Dependency
124
132
  name: mailfactory