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 CHANGED
@@ -1,3 +1,13 @@
1
+ 0.0.3 / 2012-07-06
2
+
3
+ * Bug fixes
4
+
5
+ * Show column names on table bodies in pdf format
6
+
7
+ * Enhancements
8
+
9
+ * Provide Report#cleanup to remove tmp files
10
+
1
11
  0.0.2 / 2012-07-04
2
12
 
3
13
  * Bug fixes
@@ -50,4 +50,10 @@ class Report
50
50
  def pdf
51
51
  @pdf ||= Pdf.new self
52
52
  end
53
+
54
+ def cleanup
55
+ @csv.try :cleanup
56
+ @xlsx.try :cleanup
57
+ @pdf.try :cleanup
58
+ end
53
59
  end
@@ -11,6 +11,9 @@ class Report
11
11
  def paths
12
12
  tables.map { |table| table.path }
13
13
  end
14
+ def cleanup
15
+ tables.each { |table| table.cleanup }
16
+ end
14
17
  private
15
18
  def tables
16
19
  @tables ||= report.class.tables.map do |report_table|
@@ -21,6 +21,9 @@ class Report
21
21
  end
22
22
  @path = tmp_path
23
23
  end
24
+ def cleanup
25
+ safe_delete @path if @path
26
+ end
24
27
  end
25
28
  end
26
29
  end
@@ -49,12 +49,12 @@ class Report
49
49
  else
50
50
  pdf.move_down 20
51
51
  end
52
- if t = make(table._head)
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 = make(table._body)
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 make(src)
82
+ def make_head(src)
79
83
  return unless src
80
84
  memo = []
81
85
  src.each(report) do |row|
82
- converted = row.to_a.map do |cell|
83
- case cell
84
- when TrueClass, FalseClass
85
- cell.to_s
86
- else
87
- cell
88
- end
89
- end
90
- memo << converted
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
@@ -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
@@ -1,3 +1,3 @@
1
1
  class Report
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -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
@@ -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 columns" do
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.2
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-05 00:00:00.000000000 Z
12
+ date: 2012-07-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport