report 0.0.2 → 0.0.3
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/CHANGELOG +10 -0
- data/lib/report.rb +6 -0
- data/lib/report/csv.rb +3 -0
- data/lib/report/csv/table.rb +3 -0
- data/lib/report/pdf.rb +29 -12
- data/lib/report/utils.rb +10 -1
- data/lib/report/version.rb +1 -1
- data/lib/report/xlsx.rb +8 -0
- data/spec/report_spec.rb +18 -1
- metadata +2 -2
data/CHANGELOG
CHANGED
data/lib/report.rb
CHANGED
data/lib/report/csv.rb
CHANGED
data/lib/report/csv/table.rb
CHANGED
data/lib/report/pdf.rb
CHANGED
@@ -49,12 +49,12 @@ class Report
|
|
49
49
|
else
|
50
50
|
pdf.move_down 20
|
51
51
|
end
|
52
|
-
if t =
|
52
|
+
if t = make_head(table._head)
|
53
53
|
pdf.table t, head
|
54
54
|
pdf.move_down 20
|
55
55
|
end
|
56
56
|
pdf.text table.name, :style => :bold
|
57
|
-
if t =
|
57
|
+
if t = make_body(table._body)
|
58
58
|
pdf.move_down 10
|
59
59
|
pdf.table t, body
|
60
60
|
end
|
@@ -73,25 +73,42 @@ class Report
|
|
73
73
|
@path = tmp_path
|
74
74
|
end
|
75
75
|
|
76
|
+
def cleanup
|
77
|
+
safe_delete @path if @path
|
78
|
+
end
|
79
|
+
|
76
80
|
private
|
77
81
|
|
78
|
-
def
|
82
|
+
def make_head(src)
|
79
83
|
return unless src
|
80
84
|
memo = []
|
81
85
|
src.each(report) do |row|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
86
|
+
memo << convert_row(row)
|
87
|
+
end
|
88
|
+
memo if memo.length > 0
|
89
|
+
end
|
90
|
+
|
91
|
+
def make_body(src)
|
92
|
+
return unless src
|
93
|
+
memo = []
|
94
|
+
memo << src.columns.map(&:name)
|
95
|
+
src.each(report) do |row|
|
96
|
+
memo << convert_row(row)
|
91
97
|
end
|
92
98
|
memo if memo.length > 0
|
93
99
|
end
|
94
100
|
|
101
|
+
def convert_row(row)
|
102
|
+
row.to_a.map do |cell|
|
103
|
+
case cell
|
104
|
+
when TrueClass, FalseClass
|
105
|
+
cell.to_s
|
106
|
+
else
|
107
|
+
cell
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
95
112
|
def font_name
|
96
113
|
'MainFont'
|
97
114
|
end
|
data/lib/report/utils.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
require 'tmpdir'
|
2
|
+
require 'fileutils'
|
2
3
|
|
3
4
|
class Report
|
5
|
+
# stolen from https://github.com/seamusabshere/unix_utils
|
4
6
|
module Utils
|
5
|
-
# stolen from https://github.com/seamusabshere/unix_utils
|
6
7
|
def tmp_path(options = {})
|
7
8
|
ancestor = [ self.class.name, options[:hint] ].compact.join('_')
|
8
9
|
extname = options.fetch(:extname, '.tmp')
|
@@ -11,5 +12,13 @@ class Report
|
|
11
12
|
time = Time.now.strftime('%H%M%S%L')
|
12
13
|
File.join Dir.tmpdir, [time, '_', basename[0..(234-extname.length)], extname].join
|
13
14
|
end
|
15
|
+
|
16
|
+
def safe_delete(path)
|
17
|
+
path = File.expand_path path
|
18
|
+
unless File.dirname(path).start_with?(Dir.tmpdir)
|
19
|
+
raise "Refusing to rm -rf #{path} because it's not in #{Dir.tmpdir}"
|
20
|
+
end
|
21
|
+
FileUtils.rm_rf path
|
22
|
+
end
|
14
23
|
end
|
15
24
|
end
|
data/lib/report/version.rb
CHANGED
data/lib/report/xlsx.rb
CHANGED
@@ -3,10 +3,13 @@ require 'fileutils'
|
|
3
3
|
class Report
|
4
4
|
class Xlsx
|
5
5
|
include Utils
|
6
|
+
|
6
7
|
attr_reader :report
|
8
|
+
|
7
9
|
def initialize(report)
|
8
10
|
@report = report
|
9
11
|
end
|
12
|
+
|
10
13
|
def path
|
11
14
|
return @path if defined?(@path)
|
12
15
|
require 'xlsx_writer'
|
@@ -38,6 +41,11 @@ class Report
|
|
38
41
|
workbook.cleanup
|
39
42
|
@path = tmp_path
|
40
43
|
end
|
44
|
+
|
45
|
+
def cleanup
|
46
|
+
safe_delete @path if @path
|
47
|
+
end
|
48
|
+
|
41
49
|
private
|
42
50
|
def calculate_autofilter(table, cursor)
|
43
51
|
[ 'A', cursor, ':', XlsxWriter::Cell.excel_column_letter(table._body.columns.length-1), cursor ].join
|
data/spec/report_spec.rb
CHANGED
@@ -209,6 +209,21 @@ class C1 < Report
|
|
209
209
|
end
|
210
210
|
|
211
211
|
describe Report do
|
212
|
+
it "cleans up if asked" do
|
213
|
+
r = A1.new
|
214
|
+
mess = []
|
215
|
+
mess += r.csv.paths
|
216
|
+
mess << r.xlsx.path
|
217
|
+
mess << r.pdf.path
|
218
|
+
mess.each do |path|
|
219
|
+
File.exist?(path).should == true
|
220
|
+
end
|
221
|
+
r.cleanup
|
222
|
+
mess.each do |path|
|
223
|
+
File.exist?(path).should == false
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
212
227
|
describe '#csv' do
|
213
228
|
it "writes each table to a separate file" do
|
214
229
|
hello = ::CSV.read A1.new.csv.paths.first
|
@@ -383,10 +398,12 @@ describe Report do
|
|
383
398
|
stdout_utf8 = child.out.force_encoding('UTF-8')
|
384
399
|
stdout_utf8.should include('World')
|
385
400
|
end
|
386
|
-
it "constructs a body out of rows and
|
401
|
+
it "constructs a body out of head rows and body rows and column names" do
|
387
402
|
how_to_say_hello = A2.new.pdf.path
|
388
403
|
child = POSIX::Spawn::Child.new('pdftotext', how_to_say_hello, '-')
|
389
404
|
stdout_utf8 = child.out.force_encoding('UTF-8')
|
405
|
+
stdout_utf8.should include('Language')
|
406
|
+
stdout_utf8.should include('Content')
|
390
407
|
stdout_utf8.should include('English')
|
391
408
|
stdout_utf8.should include('Hello')
|
392
409
|
stdout_utf8.should include('Russian')
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: report
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|