simple_report 0.1.0 → 0.1.1
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.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/simple_report/base.rb +41 -41
- data/lib/simple_report/sheet.rb +9 -1
- data/lib/simple_report/version.rb +1 -1
- data/lib/simple_report.rb +1 -0
- data/simple_report.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '059e4443b7a7da77afb6aaf74a997fd3a701b309'
|
4
|
+
data.tar.gz: 6c0bcf4155da5e41b12f6394f31bcbbc4317956a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 441965e971b584be1c63a6e00779ee6032f7a6f4fb61a00d64f32f1a5f546230ba4506f60506ccaa9facc5389d396b096d80ce618dc064d0b802044eae83c5bd
|
7
|
+
data.tar.gz: 4163d38ba55e7f7a18f262ccee4ce4234f712b35d59373971a277e78de3f89371a4faf25921607ec669a9bb75f4dfb96a0bc84ff24f3a11bfb6ee93d4e35c31e
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# simple_report
|
2
2
|
|
3
3
|
[](https://travis-ci.org/lostapathy/simple_report)
|
4
4
|
[](https://codeclimate.com/github/lostapathy/simple_report)
|
data/lib/simple_report/base.rb
CHANGED
@@ -1,28 +1,17 @@
|
|
1
1
|
module SimpleReport
|
2
2
|
class Base
|
3
|
+
def initialize
|
4
|
+
@sheets = []
|
5
|
+
end
|
6
|
+
|
3
7
|
def add_format(name, format)
|
4
8
|
@formats ||= {}
|
5
9
|
@formats[name] = format
|
6
10
|
end
|
7
11
|
|
8
|
-
def initialize
|
9
|
-
@file = Tempfile.new('simple_report')
|
10
|
-
@workbook = WriteXLSX.new(@file.path)
|
11
|
-
money = @workbook.add_format
|
12
|
-
money.set_num_format('$0.00')
|
13
|
-
add_format :money, money
|
14
|
-
|
15
|
-
heading = @workbook.add_format
|
16
|
-
heading.set_bold
|
17
|
-
add_format :heading, heading
|
18
|
-
|
19
|
-
percent = @workbook.add_format
|
20
|
-
percent.set_num_format('0%')
|
21
|
-
add_format :percent, percent
|
22
|
-
end
|
23
|
-
|
24
12
|
def report_xls(*params)
|
25
13
|
report(*params)
|
14
|
+
generate_report
|
26
15
|
@workbook.close
|
27
16
|
File.read @file.path
|
28
17
|
end
|
@@ -30,41 +19,52 @@ module SimpleReport
|
|
30
19
|
private
|
31
20
|
|
32
21
|
def add_sheet(name, data)
|
33
|
-
sheet = Sheet.new(name)
|
34
|
-
|
35
|
-
sheet = @workbook.add_worksheet(name)
|
36
|
-
yield
|
37
|
-
@sheets ||= []
|
22
|
+
sheet = Sheet.new(name, data)
|
23
|
+
yield sheet
|
38
24
|
@sheets << sheet
|
25
|
+
end
|
39
26
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
27
|
+
def generate_report
|
28
|
+
@file = Tempfile.new('simple_report')
|
29
|
+
@workbook = WriteXLSX.new(@file.path)
|
30
|
+
add_formats
|
31
|
+
@sheets.each do |sheet|
|
32
|
+
output_sheet = @workbook.add_worksheet(sheet.name)
|
33
|
+
sheet.fields.each_with_index do |f, index|
|
34
|
+
output_sheet.set_column(index, index, f.width)
|
35
|
+
output_sheet.write(0, index, f.name, @formats[:heading])
|
36
|
+
end
|
37
|
+
sheet.collection.each_with_index do |ic, row|
|
38
|
+
sheet.fields.each_with_index do |field, column|
|
39
|
+
if field.field
|
40
|
+
value = ic.send(field.field)
|
41
|
+
elsif field.value
|
42
|
+
value = field.value
|
43
|
+
elsif field.block
|
44
|
+
value = field.block.call(ic)
|
45
|
+
end
|
46
|
+
output_sheet.write(row + 1, column, value, find_format(field.format))
|
53
47
|
end
|
54
|
-
sheet.write(row + 1, column, value, find_format(field.format))
|
55
48
|
end
|
56
49
|
end
|
57
50
|
end
|
58
51
|
|
59
|
-
def
|
60
|
-
|
61
|
-
@fields << rf
|
52
|
+
def find_format(format)
|
53
|
+
@formats[format]
|
62
54
|
end
|
63
55
|
|
64
|
-
|
56
|
+
def add_formats
|
57
|
+
money = @workbook.add_format
|
58
|
+
money.set_num_format('$0.00')
|
59
|
+
add_format :money, money
|
65
60
|
|
66
|
-
|
67
|
-
|
61
|
+
heading = @workbook.add_format
|
62
|
+
heading.set_bold
|
63
|
+
add_format :heading, heading
|
64
|
+
|
65
|
+
percent = @workbook.add_format
|
66
|
+
percent.set_num_format('0%')
|
67
|
+
add_format :percent, percent
|
68
68
|
end
|
69
69
|
end
|
70
70
|
end
|
data/lib/simple_report/sheet.rb
CHANGED
@@ -1,7 +1,15 @@
|
|
1
1
|
module SimpleReport
|
2
2
|
class Sheet
|
3
|
-
|
3
|
+
attr_reader :name, :collection, :fields
|
4
|
+
|
5
|
+
def initialize(name, collection)
|
4
6
|
@name = name
|
7
|
+
@collection = collection
|
8
|
+
@fields = []
|
9
|
+
end
|
10
|
+
|
11
|
+
def add_field(name, field = nil, width: nil, format: nil, value: nil, &block)
|
12
|
+
@fields << Field.new(name, field, width: width, format: format, value: value, &block)
|
5
13
|
end
|
6
14
|
end
|
7
15
|
end
|
data/lib/simple_report.rb
CHANGED
data/simple_report.gemspec
CHANGED
@@ -15,7 +15,7 @@ Gem::Specification.new do |spec|
|
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
17
17
|
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
-
f.match(%r{^(test|spec|features)/})
|
18
|
+
f.match(%r{^(test|spec|features)/}) || f.match(%r{gem$})
|
19
19
|
end
|
20
20
|
spec.bindir = "exe"
|
21
21
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_report
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe Francis
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-10-
|
11
|
+
date: 2017-10-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|