simple_report 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.drone.yml +7 -0
- data/.ruby-version +1 -0
- data/Jenkinsfile +18 -0
- data/README.md +132 -2
- data/lib/simple_report/base.rb +4 -4
- data/lib/simple_report/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 97d5239a25275c2f4380d2e07e8d0b798c80a310
|
4
|
+
data.tar.gz: 168a60a39f728b261848f5d484dbade74e95d7ec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1a20a9eb8e08b6e83afc40fddfb612138707c05983d90672c1e4302d471303a1e6072622c54e18dd46af6ad13461eaa3aaa147b561318e616f229c51c7048fa7
|
7
|
+
data.tar.gz: a41ba2c8674e4dd8eb3b2919d41ed4c031d20f334c3ac3e2883bb944773cccd70f9fd03f0603ffdf61ee92a26b3c18deb4b685101e39d1bf1622d6e0df16d251
|
data/.drone.yml
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.4.2
|
data/Jenkinsfile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
node {
|
2
|
+
withEnv([
|
3
|
+
"PATH=$HOME/.rbenv/bin:$HOME/.rbenv/shims:$PATH",
|
4
|
+
"RBENV_SHELL=sh"
|
5
|
+
]) {
|
6
|
+
|
7
|
+
stage('Preparation') { // for display purposes
|
8
|
+
// Get some code from a GitHub repository
|
9
|
+
git 'https://github.com/lostapathy/simple_report.git'
|
10
|
+
}
|
11
|
+
stage('Build') {
|
12
|
+
sh "bundle install"
|
13
|
+
}
|
14
|
+
stage('test') {
|
15
|
+
sh "bundle exec rake test"
|
16
|
+
}
|
17
|
+
}
|
18
|
+
}
|
data/README.md
CHANGED
@@ -5,7 +5,9 @@
|
|
5
5
|
[![Test Coverage](https://codeclimate.com/github/lostapathy/simple_report/badges/coverage.svg)](https://codeclimate.com/github/lostapathy/simple_report/coverage)
|
6
6
|
|
7
7
|
|
8
|
-
simple_report is a gem for building simple reports from Enumerable collections. It was extracted from a larger application and only supports Excel exports for now, but
|
8
|
+
simple_report is a gem for building simple reports from Enumerable collections. It was extracted from a larger application and only supports Excel exports for now, but we plan to add support for HTML reports as well (see TODO below). Other formats could be added if requested.
|
9
|
+
|
10
|
+
simple_report is meant to provide a simple way to get a collection of data out of your application and out to the user in a reasonable report, and do so without any hassle. It's not intended to collect, analyze, or transform data for you.
|
9
11
|
|
10
12
|
## Installation
|
11
13
|
|
@@ -25,7 +27,135 @@ Or install it yourself as:
|
|
25
27
|
|
26
28
|
## Usage
|
27
29
|
|
28
|
-
|
30
|
+
Like a typical spreadsheet, each report may have multiple Sheets. Each sheet has multiple fields/columns. The data sources passed in can be any Enumerable collection, whether that's a simple Array or an ActiveRecord query does not matter.
|
31
|
+
|
32
|
+
Let's build our first report:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
class CustomerReport < SimpleReport::Base
|
36
|
+
def build_report
|
37
|
+
data = Customer.all
|
38
|
+
|
39
|
+
add_sheet('Customers', data) do |sheet|
|
40
|
+
sheet.add_field('Username', :username)
|
41
|
+
sheet.add_field('Full Name', width: 30) { |x| x.first_name + " " x.last_name }
|
42
|
+
sheet.add_field('Billing Rate', :billing_rate, format: :money)
|
43
|
+
sheet.add_field('SLA %', :sla, format: :percent)
|
44
|
+
sheet.add_field('Placeholder', value: 'TBD')
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
report = CustomerReport.new
|
50
|
+
File.write('report.xlsx', report.to_xlsx)
|
51
|
+
```
|
52
|
+
|
53
|
+
This is pretty straightforward, and covers the entire API. We'll go over it in detail below.
|
54
|
+
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
class CustomerReport < SimpleReport::Base
|
58
|
+
def build_report
|
59
|
+
```
|
60
|
+
Each report must subclass ```SimpleReport::Base``` and define the report in the build_report method. For this example, we're report on an an ActiveRecord collection of all ```Customer```ruby records.
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
data = Customer.all
|
64
|
+
```
|
65
|
+
Our report data will be all the ```Customer``` objects returned by ActiveRecord
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
add_sheet('Customers', data) do |sheet|
|
69
|
+
```
|
70
|
+
We want to add a Sheet to our report. It'll be labeled 'Customers', and include one row for every element in ```data```. To include multiple Sheets in your report, simple add additional add_sheet blocks.
|
71
|
+
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
sheet.add_field('Username', :username)
|
75
|
+
```
|
76
|
+
Adds a column labeled Username to the report, and populates it by invoking the :username method on each record in the data collection.
|
77
|
+
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
sheet.add_field('Full Name', width: 30) { |x| x.first_name + " " x.last_name }
|
81
|
+
```
|
82
|
+
Adds a column labeled 'Full Name' to the report, and populates it by calling the provided block on each record. In this case, it's concatenating the user's name together. We're also setting a field width of 30.
|
83
|
+
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
sheet.add_field('Billing Rate', :billing_rate, format: :money)
|
87
|
+
```
|
88
|
+
Add a column named 'Billing Rate', populate it by invoking :billing_rate on each record, and format the results as money.
|
89
|
+
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
sheet.add_field('SLA %', :sla, format: :percent)
|
93
|
+
```
|
94
|
+
Add a column named 'SLA %', populate it by invoking :sla on each record, and format the results as a percentage.
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
sheet.add_field('Placeholder', value: 'TBD')
|
98
|
+
```
|
99
|
+
Add a column 'Plaeholder' and populate it with the supplied value. This seemingly useless capability is surprisingly useful when you have to match a report full of boilerplate.
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
report = CustomerReport.new
|
103
|
+
File.write('report.xlsx', report.to_xlsx)
|
104
|
+
```
|
105
|
+
Instantiate the report, then write it to file.
|
106
|
+
|
107
|
+
## Advanced Usage
|
108
|
+
|
109
|
+
Your report object and build_report method are just plain ruby code, so you can add logic to how your reports are generated. For example:
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
class FancyReport < SimpleReport::Base
|
113
|
+
attr_accessor :include_wholesale
|
114
|
+
|
115
|
+
def initialize
|
116
|
+
@include_wholesale = true
|
117
|
+
end
|
118
|
+
|
119
|
+
def build_report
|
120
|
+
data = Product.all
|
121
|
+
add_sheet('Products', data) do |sheet|
|
122
|
+
sheet.add_field('Name', :name)
|
123
|
+
sheet.add_field('MSRP', :msrp, format: :money)
|
124
|
+
sheet.add_field('Wholesale', :wholesale, format: :money) if @include_wholesale
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
report = FancyReport.new
|
130
|
+
report.include_wholesale = false
|
131
|
+
File.write('my_report.xlsx, report.to_xlsx)
|
132
|
+
```
|
133
|
+
|
134
|
+
You can also add attributes to the report to pass data into the report, rather than compute the data in the report as our simple examples have thus far:
|
135
|
+
```ruby
|
136
|
+
class AnotherReport < SimpleReport::Base
|
137
|
+
attr_accessor :data
|
138
|
+
|
139
|
+
def build_report
|
140
|
+
add_sheet 'Something', @data do |sheet|
|
141
|
+
// define some fields
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
report = AnotherReport.new
|
147
|
+
report.data = Customer.all
|
148
|
+
File.write('another_report.xlsx', report.to_xlsx)
|
149
|
+
|
150
|
+
```
|
151
|
+
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
## TODO
|
156
|
+
|
157
|
+
* Add to_html method to render out as a simple table-based HTML report. We'll generate a table, with thead/tbody/tfoot, apply css classes based on formats, and apply basic text formatting such as number_to_currency or percent (%) characters. That will give the end-user enough flexibility to do whatever they want with the output, while still keeping the gem extremely simple
|
158
|
+
* Add some concept of a footer/summary to the reports that allows operations like sum, average, minimum, maximum. When exported to xlsx, these will be exported as formulas. Actual values should be computed for HTML reports.
|
29
159
|
|
30
160
|
## Development
|
31
161
|
|
data/lib/simple_report/base.rb
CHANGED
@@ -33,16 +33,16 @@ module SimpleReport
|
|
33
33
|
output_sheet.set_column(index, index, f.width)
|
34
34
|
output_sheet.write(0, index, f.name, @formats[:heading])
|
35
35
|
end
|
36
|
-
sheet.collection.each_with_index do |ic,
|
36
|
+
sheet.collection.each_with_index do |ic, record_num|
|
37
37
|
sheet.fields.each_with_index do |field, column|
|
38
38
|
if field.field
|
39
39
|
value = ic.send(field.field)
|
40
40
|
elsif field.value
|
41
41
|
value = field.value
|
42
42
|
elsif field.block
|
43
|
-
value = field.block.call(ic)
|
43
|
+
value = field.block.call(ic, record_num + 2)
|
44
44
|
end
|
45
|
-
output_sheet.write(
|
45
|
+
output_sheet.write(record_num + 1, column, value, find_format(field.format))
|
46
46
|
end
|
47
47
|
end
|
48
48
|
end
|
@@ -62,7 +62,7 @@ module SimpleReport
|
|
62
62
|
add_format :heading, heading
|
63
63
|
|
64
64
|
percent = @workbook.add_format
|
65
|
-
percent.set_num_format('0%')
|
65
|
+
percent.set_num_format('0.0%')
|
66
66
|
add_format :percent, percent
|
67
67
|
end
|
68
68
|
|
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.2.
|
4
|
+
version: 0.2.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-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -74,9 +74,12 @@ executables: []
|
|
74
74
|
extensions: []
|
75
75
|
extra_rdoc_files: []
|
76
76
|
files:
|
77
|
+
- ".drone.yml"
|
77
78
|
- ".gitignore"
|
79
|
+
- ".ruby-version"
|
78
80
|
- ".travis.yml"
|
79
81
|
- Gemfile
|
82
|
+
- Jenkinsfile
|
80
83
|
- LICENSE.txt
|
81
84
|
- README.md
|
82
85
|
- Rakefile
|