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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e241b0d81fce8a3b55de4dbf8eccfe3e34a72a95020fd6098bb501a2434257fe
4
- data.tar.gz: ebb7296a6025cd82acb5461863f7599f52cfb8494184adc29fd6888004584e23
3
+ metadata.gz: fd3898009be42319daf8afaa54b0a064b8952d107a8cef3ad0d3d5692c2d17ad
4
+ data.tar.gz: dabe5fac8684e094140bf276d14cddfef3c3ba03693b66049ea5d2a68de30fd5
5
5
  SHA512:
6
- metadata.gz: 773100dbaed0b7d43c0b8ca8139fcbd7c925ef18515ef39d89e8131f3738bfc4edb8070188a64c40cea5ea03532d4b07f1dcfe57ac66890b88954735b1b22a51
7
- data.tar.gz: 97150a6758cd926f08efbe920d5be2013cbf5e92d77f4768947086ceb0e333bb0fabe187bf624127a2417f3ed2b99f4cf233dc84029361e5e87dc857e12525ee
6
+ metadata.gz: d89cf51d6df90425a07e33e666f3a6e63434e0ec7085b1701d10777e371cef6149e43d16b408f809360211ea934f1e514037747dcdede233938f752146e11661
7
+ data.tar.gz: 3f9ec0ebbeaa07dc650c05f6a7ed488d289febc9712c5ac96547a4cc6875a63093b254c1f58ed4bbf723263eb43b6f459e75f3a923b8f7803b2c3921e6bcb510
checksums.yaml.gz.sig CHANGED
@@ -1,3 +1,4 @@
1
- m��cN��N]ķ���Y\N�N/NE�]�d�%�qz�I�}�2�*��ֻ�f����� 6{�� F��H��6-F�
2
- ��{��#7]�̩i��ѭ�][�dp�Tz�0��,{����ғk�y����C赅���U��9.���7�`�Q����DZ���p��e��y��r_�U$K�s�F7 Y�A�6GL?��u}e�1��"�4м)�k;BQ�
3
- v>Q��).ꨟ\U3��^75)og��KC'��Q�|=��s�o�ߜV|�nُ�MC����$�x}ZQ�2P�̀M}��|椂�c�X[w�L�q��|�Z=}'N�|�&yn�V�,
1
+ S�Jtg��+Ը�,R�� �|;ez4`jp���}����D7��H!
2
+ ���$�ۚ5�*�P�p-O�߼�ֺ4-���9���#�5���H�Cp�ܰ;����o�-�8W ;�׊wLY,)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
 
@@ -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
- @stats[attr.to_s]
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
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Repolho
4
- VERSION = "0.1.6"
4
+ VERSION = "0.1.8"
5
5
  end
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
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: repolho
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eustáquio Rangel
metadata.gz.sig CHANGED
Binary file