repolho 0.1.6 → 0.1.8
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
- checksums.yaml.gz.sig +4 -3
- data/README.md +55 -4
- data/lib/repolho/container.rb +31 -14
- data/lib/repolho/main.rb +20 -1
- data/lib/repolho/page.rb +22 -0
- data/lib/repolho/version.rb +1 -1
- data/test/main_test.rb +64 -0
- data.tar.gz.sig +0 -0
- metadata +1 -1
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fd3898009be42319daf8afaa54b0a064b8952d107a8cef3ad0d3d5692c2d17ad
|
|
4
|
+
data.tar.gz: dabe5fac8684e094140bf276d14cddfef3c3ba03693b66049ea5d2a68de30fd5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d89cf51d6df90425a07e33e666f3a6e63434e0ec7085b1701d10777e371cef6149e43d16b408f809360211ea934f1e514037747dcdede233938f752146e11661
|
|
7
|
+
data.tar.gz: 3f9ec0ebbeaa07dc650c05f6a7ed488d289febc9712c5ac96547a4cc6875a63093b254c1f58ed4bbf723263eb43b6f459e75f3a923b8f7803b2c3921e6bcb510
|
checksums.yaml.gz.sig
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
S�Jtg��+Ը�,R�� �|;ez4`jp���}����D7��H!
|
|
2
|
+
���$�ۚ5�*�P�p-O��ֺ4-���9���#�5���H�Cp�ܰ;����o�-�8W ;�wLY,)g���
|
|
3
|
+
��,S����3��q�鞟g�(� �bq���7��,@|��v��>��M!���(i:����VՈ��t
|
|
4
|
+
R@���<��;σZ��Ϧ Yp��}�Zv12ؽV�5.�\0�iH����&q(��<���Zڹ�r�(܌���7t�uQ��B��@��w���� "Y����x����ކ�"��2�l������Oۅ����d�gk��)V�l�qK���B:�G�w>D���)��t����tm��be�fL
|
data/README.md
CHANGED
|
@@ -64,6 +64,56 @@ sections). Everything inside the custom elements is processed as ERB.
|
|
|
64
64
|
| `<report-header>` | Header of the current container | — |
|
|
65
65
|
| `<report-footer>` | Footer of the current container | — |
|
|
66
66
|
| `<report-row>` | Template for each data row (inside the innermost group) | — |
|
|
67
|
+
| `<report-last-page>` | Optional block rendered only at the end of the report, as additional rows on the last page. Counts toward the page row limit and can force a page break. | — |
|
|
68
|
+
|
|
69
|
+
### Last page content (`<report-last-page>`)
|
|
70
|
+
|
|
71
|
+
Optional element that is rendered **only after all data rows** have been processed.
|
|
72
|
+
Its content is injected as normal rows into the page flow, so:
|
|
73
|
+
|
|
74
|
+
- It counts toward the `rows` limit of `<report-page>`.
|
|
75
|
+
- If it does not fit on the current page, a new page is opened automatically.
|
|
76
|
+
- The page footer of the final page is rendered **after** this block.
|
|
77
|
+
|
|
78
|
+
Because the block is only ever emitted at the end, you do not need to check `last_page?` inside it.
|
|
79
|
+
|
|
80
|
+
You can print the **overall report total** from this block by using the second argument of the statistics methods (see below):
|
|
81
|
+
|
|
82
|
+
```html
|
|
83
|
+
<report-last-page>
|
|
84
|
+
<tr>
|
|
85
|
+
<td colspan="4">Grand total</td>
|
|
86
|
+
<td><%= format("%.2f", sum(:value, :report)) %></td>
|
|
87
|
+
</tr>
|
|
88
|
+
<tr>
|
|
89
|
+
<td colspan="5">Final notes and signature</td>
|
|
90
|
+
</tr>
|
|
91
|
+
</report-last-page>
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Statistics context
|
|
95
|
+
|
|
96
|
+
Every container keeps its own statistics. By default `sum`, `avg`, `count`, `min` and `max` operate on the current container.
|
|
97
|
+
|
|
98
|
+
All statistics methods accept an optional second argument that selects another context:
|
|
99
|
+
|
|
100
|
+
| Context | Meaning |
|
|
101
|
+
|---------|---------|
|
|
102
|
+
| `nil` (default) | Current container |
|
|
103
|
+
| `:page` | The current page |
|
|
104
|
+
| `:report` | The whole report (grand total) |
|
|
105
|
+
| a `Container` object | That specific container |
|
|
106
|
+
|
|
107
|
+
Examples:
|
|
108
|
+
|
|
109
|
+
```erb
|
|
110
|
+
<%= sum(:value) %> <!-- current group or page -->
|
|
111
|
+
<%= sum(:value, :page) %> <!-- current page total -->
|
|
112
|
+
<%= sum(:value, :report) %> <!-- overall report total -->
|
|
113
|
+
<%= report.sum(:value) %> <!-- same as above, via the report object -->
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
The `report` object is available in every ERB context (headers, footers, rows and last-page content).
|
|
67
117
|
|
|
68
118
|
### Allowed HTML inside page content
|
|
69
119
|
|
|
@@ -224,11 +274,12 @@ To preview the structure, rename the file to `.html` and open it in a browser
|
|
|
224
274
|
| Method | Description |
|
|
225
275
|
|--------|-------------|
|
|
226
276
|
| `attr(:field)` | Value of the field on the current row |
|
|
227
|
-
| `sum(:field)` | Sum within the current container |
|
|
228
|
-
| `avg(:field)` | Average |
|
|
229
|
-
| `min(:field)` / `max(:field)` | Minimum / maximum |
|
|
230
|
-
| `count(:field)` | Number of values |
|
|
277
|
+
| `sum(:field)` / `sum(:field, context)` | Sum within the current container, or in the given context (`:page`, `:report`) |
|
|
278
|
+
| `avg(:field)` / `avg(:field, context)` | Average |
|
|
279
|
+
| `min(:field)` / `max(:field)` (and with context) | Minimum / maximum |
|
|
280
|
+
| `count(:field)` / `count(:field, context)` | Number of values |
|
|
231
281
|
| `number` | Current page or group number |
|
|
282
|
+
| `report` | The root `Report` container (useful to call `report.sum(:field)`) |
|
|
232
283
|
|
|
233
284
|
## Rails helpers
|
|
234
285
|
|
data/lib/repolho/container.rb
CHANGED
|
@@ -6,8 +6,8 @@ require_relative "stats"
|
|
|
6
6
|
|
|
7
7
|
module Repolho
|
|
8
8
|
class Container
|
|
9
|
-
attr_accessor :header, :footer, :containers, :row_template, :attr, :page, :status, :reprint_header_on_page_break
|
|
10
|
-
attr_reader :id, :number
|
|
9
|
+
attr_accessor :header, :footer, :containers, :row_template, :attr, :page, :report, :status, :reprint_header_on_page_break
|
|
10
|
+
attr_reader :id, :number, :stats
|
|
11
11
|
|
|
12
12
|
UNSTARTED = 0
|
|
13
13
|
STARTED = 1
|
|
@@ -27,6 +27,7 @@ module Repolho
|
|
|
27
27
|
@number = 1
|
|
28
28
|
@attr = nil
|
|
29
29
|
@page = nil
|
|
30
|
+
@report = nil
|
|
30
31
|
|
|
31
32
|
create_locals
|
|
32
33
|
end
|
|
@@ -70,24 +71,24 @@ module Repolho
|
|
|
70
71
|
|
|
71
72
|
def row_number = @page.row_number
|
|
72
73
|
|
|
73
|
-
def sum(attr)
|
|
74
|
-
stat(attr)&.sum || 0.0
|
|
74
|
+
def sum(attr, context = nil)
|
|
75
|
+
stat(attr, context)&.sum || 0.0
|
|
75
76
|
end
|
|
76
77
|
|
|
77
|
-
def avg(attr)
|
|
78
|
-
stat(attr)&.avg || 0.0
|
|
78
|
+
def avg(attr, context = nil)
|
|
79
|
+
stat(attr, context)&.avg || 0.0
|
|
79
80
|
end
|
|
80
81
|
|
|
81
|
-
def count(attr)
|
|
82
|
-
stat(attr)&.count || 0
|
|
82
|
+
def count(attr, context = nil)
|
|
83
|
+
stat(attr, context)&.count || 0
|
|
83
84
|
end
|
|
84
85
|
|
|
85
|
-
def min(attr)
|
|
86
|
-
stat(attr)&.min
|
|
86
|
+
def min(attr, context = nil)
|
|
87
|
+
stat(attr, context)&.min
|
|
87
88
|
end
|
|
88
89
|
|
|
89
|
-
def max(attr)
|
|
90
|
-
stat(attr)&.max
|
|
90
|
+
def max(attr, context = nil)
|
|
91
|
+
stat(attr, context)&.max
|
|
91
92
|
end
|
|
92
93
|
|
|
93
94
|
def attr(attr)
|
|
@@ -127,8 +128,24 @@ module Repolho
|
|
|
127
128
|
end
|
|
128
129
|
end
|
|
129
130
|
|
|
130
|
-
def stat(attr)
|
|
131
|
-
|
|
131
|
+
def stat(attr, context = nil)
|
|
132
|
+
target_stats = resolve_stats(context)
|
|
133
|
+
target_stats&.[](attr.to_s)
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def resolve_stats(context)
|
|
137
|
+
return @stats if context.nil?
|
|
138
|
+
|
|
139
|
+
case context
|
|
140
|
+
when :page
|
|
141
|
+
@page&.stats
|
|
142
|
+
when :report
|
|
143
|
+
@report&.stats
|
|
144
|
+
when Container
|
|
145
|
+
context.stats
|
|
146
|
+
else
|
|
147
|
+
@stats
|
|
148
|
+
end
|
|
132
149
|
end
|
|
133
150
|
|
|
134
151
|
def check_attribute_break(data)
|
data/lib/repolho/main.rb
CHANGED
|
@@ -79,6 +79,7 @@ module Repolho
|
|
|
79
79
|
@pagination = false
|
|
80
80
|
@orientation = nil
|
|
81
81
|
@paper = nil
|
|
82
|
+
@last_page_template = nil
|
|
82
83
|
@pagination_labels = {
|
|
83
84
|
first: "First",
|
|
84
85
|
prev: "<",
|
|
@@ -88,6 +89,8 @@ module Repolho
|
|
|
88
89
|
|
|
89
90
|
@report.containers << @page
|
|
90
91
|
@report.page = @page
|
|
92
|
+
@report.report = @report
|
|
93
|
+
@page.report = @report
|
|
91
94
|
end
|
|
92
95
|
|
|
93
96
|
def content
|
|
@@ -104,6 +107,8 @@ module Repolho
|
|
|
104
107
|
@report.call(:row, row)
|
|
105
108
|
end
|
|
106
109
|
|
|
110
|
+
inject_last_page_content
|
|
111
|
+
|
|
107
112
|
@report.call(:finish, nil)
|
|
108
113
|
@page.finalize
|
|
109
114
|
|
|
@@ -140,6 +145,16 @@ module Repolho
|
|
|
140
145
|
|
|
141
146
|
private
|
|
142
147
|
|
|
148
|
+
def inject_last_page_content
|
|
149
|
+
return if @last_page_template.nil? || @last_page_template.strip.empty?
|
|
150
|
+
|
|
151
|
+
@page.mark_as_last!
|
|
152
|
+
rendered = render_erb(@last_page_template)
|
|
153
|
+
@page.inject_as_rows(rendered)
|
|
154
|
+
# Page footer is rendered later by the normal :finish path,
|
|
155
|
+
# after any remaining group footers.
|
|
156
|
+
end
|
|
157
|
+
|
|
143
158
|
def inject_pagination_labels
|
|
144
159
|
@page.instance_variable_set(:@pagination_first, @pagination_labels[:first])
|
|
145
160
|
@page.instance_variable_set(:@pagination_prev, @pagination_labels[:prev])
|
|
@@ -247,6 +262,9 @@ module Repolho
|
|
|
247
262
|
|
|
248
263
|
build_page(report)
|
|
249
264
|
build_groups(report)
|
|
265
|
+
|
|
266
|
+
last_page = report.search("report-last-page").first
|
|
267
|
+
@last_page_template = last_page.inner_html if last_page
|
|
250
268
|
end
|
|
251
269
|
|
|
252
270
|
def build_page(report)
|
|
@@ -283,6 +301,7 @@ module Repolho
|
|
|
283
301
|
container.attr = element["attr"]
|
|
284
302
|
container.reprint_header_on_page_break = element["reprint-header-on-page-break"] == "true"
|
|
285
303
|
container.page = @page
|
|
304
|
+
container.report = @report
|
|
286
305
|
container
|
|
287
306
|
end
|
|
288
307
|
|
|
@@ -295,7 +314,7 @@ module Repolho
|
|
|
295
314
|
def render_erb(content)
|
|
296
315
|
return "" if content.nil? || content.empty?
|
|
297
316
|
|
|
298
|
-
ERB.new(content).result(@page.instance_eval { binding })
|
|
317
|
+
ERB.new(CGI.unescapeHTML(content)).result(@page.instance_eval { binding })
|
|
299
318
|
end
|
|
300
319
|
end
|
|
301
320
|
end
|
data/lib/repolho/page.rb
CHANGED
|
@@ -14,6 +14,7 @@ module Repolho
|
|
|
14
14
|
@row_number = 1
|
|
15
15
|
@page_number = 1
|
|
16
16
|
@page_open = false
|
|
17
|
+
@last_page = false
|
|
17
18
|
end
|
|
18
19
|
|
|
19
20
|
def rows=(number)
|
|
@@ -21,6 +22,27 @@ module Repolho
|
|
|
21
22
|
@valid_rows = @rows
|
|
22
23
|
end
|
|
23
24
|
|
|
25
|
+
def last_page?
|
|
26
|
+
@last_page
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def mark_as_last!
|
|
30
|
+
@last_page = true
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Injects pre-rendered HTML (expected to contain <tr>s) as additional rows.
|
|
34
|
+
# Each <tr> is appended individually so normal page-break logic applies.
|
|
35
|
+
def inject_as_rows(html)
|
|
36
|
+
return if html.nil? || html.strip.empty?
|
|
37
|
+
|
|
38
|
+
Nokogiri::HTML(html).search("tr").each do |tr|
|
|
39
|
+
check_page_break
|
|
40
|
+
# After a break, status is FINISHED; reopen so later :finish can still emit the footer
|
|
41
|
+
@status = STARTED if @status == FINISHED
|
|
42
|
+
self << tr.to_html
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
24
46
|
def <<(content)
|
|
25
47
|
return unless content
|
|
26
48
|
|
data/lib/repolho/version.rb
CHANGED
data/test/main_test.rb
CHANGED
|
@@ -370,6 +370,70 @@ module Repolho
|
|
|
370
370
|
refute_includes columns, "state"
|
|
371
371
|
end
|
|
372
372
|
|
|
373
|
+
def test_last_page_content_is_injected_and_counts_as_rows
|
|
374
|
+
template_html = <<~HTML
|
|
375
|
+
<!DOCTYPE html>
|
|
376
|
+
<html><body>
|
|
377
|
+
<table>
|
|
378
|
+
<report>
|
|
379
|
+
<report-page rows="3">
|
|
380
|
+
<report-header><tr><th>H</th></tr></report-header>
|
|
381
|
+
<report-row><tr><td><%= attr(:id) %></td></tr></report-row>
|
|
382
|
+
<report-footer><tr><td>F</td></tr></report-footer>
|
|
383
|
+
</report-page>
|
|
384
|
+
<report-last-page>
|
|
385
|
+
<tr><td>LAST BLOCK LINE 1</td></tr>
|
|
386
|
+
<tr><td>LAST BLOCK LINE 2</td></tr>
|
|
387
|
+
</report-last-page>
|
|
388
|
+
</report>
|
|
389
|
+
</table>
|
|
390
|
+
</body></html>
|
|
391
|
+
HTML
|
|
392
|
+
template = write_temp_template(template_html)
|
|
393
|
+
main = Main.new(template.path, @data) # 3 rows
|
|
394
|
+
main.run
|
|
395
|
+
html = main.content.join
|
|
396
|
+
|
|
397
|
+
assert_includes html, "LAST BLOCK LINE 1"
|
|
398
|
+
assert_includes html, "LAST BLOCK LINE 2"
|
|
399
|
+
# Should have caused a page break because 3 data + 2 last = more than rows=3
|
|
400
|
+
assert_operator html.scan(/repolho-page/).size, :>=, 2
|
|
401
|
+
ensure
|
|
402
|
+
template&.unlink
|
|
403
|
+
end
|
|
404
|
+
|
|
405
|
+
def test_last_page_can_access_report_grand_total
|
|
406
|
+
template_html = <<~HTML
|
|
407
|
+
<!DOCTYPE html>
|
|
408
|
+
<html><body>
|
|
409
|
+
<table>
|
|
410
|
+
<report>
|
|
411
|
+
<report-page rows="10">
|
|
412
|
+
<report-row>
|
|
413
|
+
<tr><td><%= attr(:id) %></td><td><%= attr(:value) %></td></tr>
|
|
414
|
+
</report-row>
|
|
415
|
+
</report-page>
|
|
416
|
+
<report-last-page>
|
|
417
|
+
<tr><td>Grand total from last page:</td><td><%= sum(:value, :report) %></td></tr>
|
|
418
|
+
<tr><td>Also via report object:</td><td><%= report.sum(:value) %></td></tr>
|
|
419
|
+
</report-last-page>
|
|
420
|
+
</report>
|
|
421
|
+
</table>
|
|
422
|
+
</body></html>
|
|
423
|
+
HTML
|
|
424
|
+
template = write_temp_template(template_html)
|
|
425
|
+
main = Main.new(template.path, @data)
|
|
426
|
+
main.run
|
|
427
|
+
html = main.content.join
|
|
428
|
+
|
|
429
|
+
# 10 + 20 + 15 = 45
|
|
430
|
+
assert_includes html, "Grand total from last page:"
|
|
431
|
+
assert_includes html, "45.0"
|
|
432
|
+
assert_includes html, "Also via report object:"
|
|
433
|
+
ensure
|
|
434
|
+
template&.unlink
|
|
435
|
+
end
|
|
436
|
+
|
|
373
437
|
def test_page_number_available_in_after_erb
|
|
374
438
|
after_erb = <<~HTML
|
|
375
439
|
<!DOCTYPE html>
|
data.tar.gz.sig
CHANGED
|
Binary file
|
metadata
CHANGED
metadata.gz.sig
CHANGED
|
Binary file
|