repolho 0.2.2 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cbce0fb32bf82c14c5a0b6046dab2334da176a57dac2d004f8294a1364811773
4
- data.tar.gz: '0759da9158bb03a696d5a9f23a1c453c0c1fa420f913c319bd98b40b7d646907'
3
+ metadata.gz: ae62871a294af728a74adc231bc5b0460c60289618c16d1fe57cfd3db4ea95f8
4
+ data.tar.gz: a86ebabe86f719dbbe05f85fe26d25ca8125c2b4623e0796c75fde836648a49d
5
5
  SHA512:
6
- metadata.gz: 22d3581a89caefa281f804e116ea42c0d535d7e356a66103500a690aed7c1572453250db70eb90247f2b1838c6ca988a752af8a5973ac42b2fa096d39c8d7bd0
7
- data.tar.gz: 1ca39f3fceb142d7dbc7b24902feb9fb908277e69806ef4a0da846fce9a34d512ed0200a2906dc60784bbb3809cd9bd1d98be70dd7738d91436cc2d73216b329
6
+ metadata.gz: a08581aa77e1a2e0cb68aa93a3b84d738aa747cc64bf3c2b89cf6c9d8472bf8d8f80d7169bb651fd0de781f0f71929b6837d476a350796bf09691d2e3d72ac44
7
+ data.tar.gz: 4d232be9b7bbc4c12f4fcb01a892704a5200bc0eb21a9b7517d41fe1b1c7edae9ff0eeada100c1cf4e61a1d3124b570cffa2609585e75eac121541ae5e8c9cf5
checksums.yaml.gz.sig CHANGED
Binary file
data/README.md CHANGED
@@ -165,6 +165,17 @@ You can print the **overall report total** from this block by using the second a
165
165
  </report-last-page>
166
166
  ```
167
167
 
168
+
169
+ ### Limiting statistics
170
+
171
+ By default every numeric column is tracked. To keep metadata (and the work)
172
+ focused on the measures that matter, set one of these attributes on `<report>`:
173
+
174
+ - `stats="PE_TOTA,PE_LPRO"` — whitelist: only these columns
175
+ - `ignore-stats="ID_PEDI,ID_FILI,PE_NUME"` — blacklist: all numeric columns except these
176
+
177
+ If both are present, `stats` wins. Columns are matched by name (case-sensitive).
178
+
168
179
  ### Statistics context
169
180
 
170
181
  Every container keeps its own statistics. By default `sum`, `avg`, `count`, `min` and `max` operate on the current container.
@@ -42,7 +42,7 @@ module Repolho
42
42
 
43
43
  @cur_row = data if data
44
44
  update_stats if data && type == :row
45
- check_started
45
+ check_started(type)
46
46
 
47
47
  @containers.each do |container|
48
48
  container.call(type, data)
@@ -205,8 +205,11 @@ module Repolho
205
205
  force_header_reprint_on_children
206
206
  end
207
207
 
208
- def check_started
209
- return unless @status != STARTED
208
+ def check_started(type = :row)
209
+ return if @status == STARTED
210
+ # On the final :finish, do not re-open a container that was already closed
211
+ # (e.g. groups finished early so their footer appears before last-page content).
212
+ return if type == :finish && @status == FINISHED
210
213
 
211
214
  @page << start
212
215
  @status = STARTED
data/lib/repolho/main.rb CHANGED
@@ -138,6 +138,11 @@ module Repolho
138
138
  groups: collect_groups_with_data
139
139
  }
140
140
 
141
+ # Close any groups that are still open so their footers appear
142
+ # *before* the last-page content. The subsequent :finish will
143
+ # skip them (status == FINISHED) and only emit page/report footers.
144
+ finish_open_groups(@page)
145
+
141
146
  inject_last_page_content
142
147
 
143
148
  @report.call(:finish, nil)
@@ -185,8 +190,23 @@ module Repolho
185
190
  @page.mark_as_last!
186
191
  rendered = render_erb(@last_page_template)
187
192
  @page.inject_as_rows(rendered)
188
- # Page footer is rendered later by the normal :finish path,
189
- # after any remaining group footers.
193
+ # Page footer is rendered later by the normal :finish path.
194
+ # Group footers were already emitted by finish_open_groups.
195
+ end
196
+
197
+ # Depth-first: finish nested groups before their parents so footers
198
+ # appear in the correct nesting order. Only closes containers that
199
+ # have actually started (have data).
200
+ def finish_open_groups(container)
201
+ container.containers.each do |child|
202
+ finish_open_groups(child)
203
+
204
+ next unless child.status == Container::STARTED
205
+
206
+ @page << child.finish
207
+ child.reset_stats
208
+ child.status = Container::FINISHED
209
+ end
190
210
  end
191
211
 
192
212
  def inject_pagination_labels
@@ -257,6 +277,36 @@ module Repolho
257
277
  end
258
278
  end
259
279
 
280
+ # Restricts which numeric columns receive statistics.
281
+ # - stats="A,B" whitelist: only these columns
282
+ # - ignore-stats="A,B" blacklist: all numeric columns except these
283
+ # Whitelist wins if both are present. When neither is set, every
284
+ # numeric column is tracked (previous behaviour).
285
+ def apply_stats_filter(report_node)
286
+ allow = split_attr(report_node["stats"])
287
+ ignore = split_attr(report_node["ignore-stats"])
288
+
289
+ return if allow.empty? && ignore.empty?
290
+
291
+ @columns = if allow.any?
292
+ @columns & allow
293
+ else
294
+ @columns - ignore
295
+ end
296
+
297
+ # Page and report were created in initialize with the full set.
298
+ # Groups are built afterwards and pick up the filtered columns.
299
+ filtered = make_stats
300
+ @page.instance_variable_set(:@stats, filtered)
301
+ @report.instance_variable_set(:@stats, make_stats)
302
+ end
303
+
304
+ def split_attr(value)
305
+ return [] if value.nil? || value.strip.empty?
306
+
307
+ value.split(",").map(&:strip).reject(&:empty?)
308
+ end
309
+
260
310
  def parse
261
311
  content = File.read(@path)
262
312
  extract_title(content)
@@ -292,6 +342,8 @@ module Repolho
292
342
  @page.min_row_for_break = report_node["min-row-for-break"].to_i
293
343
  end
294
344
 
345
+ apply_stats_filter(report_node)
346
+
295
347
  if @pagination
296
348
  @pagination_labels[:first] = report_node["pagination-first"] if report_node["pagination-first"]
297
349
  @pagination_labels[:prev] = report_node["pagination-prev"] if report_node["pagination-prev"]
data/lib/repolho/page.rb CHANGED
@@ -110,10 +110,44 @@ module Repolho
110
110
 
111
111
  def finalize
112
112
  close_page
113
+ remove_empty_trailing_pages
113
114
  end
114
115
 
115
116
  private
116
117
 
118
+ # Drops trailing pages that contain no <tr>. This happens when content
119
+ # lands exactly on the page limit: check_page_break closes the page, then
120
+ # a subsequent empty append (e.g. an empty report footer) opens a new
121
+ # page that receives nothing. Post-processing is the reliable fix because
122
+ # the limit is only known after the rows have been emitted.
123
+ def remove_empty_trailing_pages
124
+ loop do
125
+ break unless remove_one_empty_trailing_page
126
+
127
+ @page_number -= 1
128
+ end
129
+ end
130
+
131
+ def remove_one_empty_trailing_page
132
+ open_idx = @content.rindex { |c| c.is_a?(String) && c.include?('class="repolho-page"') }
133
+ return false unless open_idx
134
+
135
+ # Keep at least one page.
136
+ earlier = @content[0...open_idx]
137
+ return false unless earlier.any? { |c| c.to_s.include?('class="repolho-page"') }
138
+
139
+ rest = @content[(open_idx + 1)..]
140
+ close_rel = rest.index { |c| c.is_a?(String) && c.strip == "</tbody>" }
141
+ return false unless close_rel
142
+
143
+ between = rest[0...close_rel]
144
+ return false unless between.all? { |c| c.to_s.strip.empty? }
145
+
146
+ close_idx = open_idx + 1 + close_rel
147
+ @content.slice!(open_idx..close_idx)
148
+ true
149
+ end
150
+
117
151
  def check_page_break
118
152
  return unless @row_number >= @valid_rows
119
153
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Repolho
4
- VERSION = "0.2.2"
4
+ VERSION = "0.2.3"
5
5
  end
data/test/main_test.rb CHANGED
@@ -605,6 +605,130 @@ module Repolho
605
605
  template&.unlink
606
606
  end
607
607
 
608
+
609
+ def test_last_page_content_appears_after_group_footer
610
+ template_html = <<~HTML
611
+ <!DOCTYPE html>
612
+ <html><body>
613
+ <table>
614
+ <report>
615
+ <report-page rows="20">
616
+ <report-group id="state" attr="state">
617
+ <report-header>
618
+ <tr><td>Group: <%= attr(:state) %></td></tr>
619
+ </report-header>
620
+ <report-row>
621
+ <tr><td><%= attr(:name) %></td><td><%= attr(:value) %></td></tr>
622
+ </report-row>
623
+ <report-footer>
624
+ <tr><td>State total</td><td><%= sum(:value) %></td></tr>
625
+ </report-footer>
626
+ </report-group>
627
+ <report-last-page>
628
+ <tr><td>GRAND TOTAL</td><td><%= sum(:value, :report) %></td></tr>
629
+ </report-last-page>
630
+ </report-page>
631
+ </report>
632
+ </table>
633
+ </body></html>
634
+ HTML
635
+ template = write_temp_template(template_html)
636
+ main = Main.new(template.path, @data)
637
+ main.run
638
+ html = main.content.join
639
+
640
+ # Last group is RJ (value 15). Its footer must appear before the grand total.
641
+ state_total_pos = html.index("State total")
642
+ grand_pos = html.index("GRAND TOTAL")
643
+
644
+ assert state_total_pos, "group footer missing"
645
+ assert grand_pos, "last-page content missing"
646
+ assert state_total_pos < grand_pos,
647
+ "report-last-page must appear after the last group footer"
648
+
649
+ # The last group's total (15.0) should be present, and the grand total (45.0) after it.
650
+ assert_includes html, "15.0"
651
+ assert_includes html, "45.0"
652
+ ensure
653
+ template&.unlink
654
+ end
655
+
656
+
657
+ def test_stats_whitelist_limits_tracked_columns
658
+ template_html = <<~HTML
659
+ <!DOCTYPE html>
660
+ <html><body>
661
+ <table>
662
+ <report stats="value">
663
+ <report-page rows="10">
664
+ <report-row>
665
+ <tr><td><%= attr(:id) %></td><td><%= sum(:value) %></td></tr>
666
+ </report-row>
667
+ </report-page>
668
+ </report>
669
+ </table>
670
+ </body></html>
671
+ HTML
672
+ template = write_temp_template(template_html)
673
+ main = Main.new(template.path, @data)
674
+ main.run
675
+ meta = main.metadata
676
+
677
+ assert_includes meta[:report][:stats].keys, "value"
678
+ refute_includes meta[:report][:stats].keys, "id"
679
+ assert_equal 45, meta[:report][:stats]["value"][:sum]
680
+ ensure
681
+ template&.unlink
682
+ end
683
+
684
+ def test_ignore_stats_excludes_listed_columns
685
+ template_html = <<~HTML
686
+ <!DOCTYPE html>
687
+ <html><body>
688
+ <table>
689
+ <report ignore-stats="id">
690
+ <report-page rows="10">
691
+ <report-row>
692
+ <tr><td><%= attr(:id) %></td><td><%= attr(:value) %></td></tr>
693
+ </report-row>
694
+ </report-page>
695
+ </report>
696
+ </table>
697
+ </body></html>
698
+ HTML
699
+ template = write_temp_template(template_html)
700
+ main = Main.new(template.path, @data)
701
+ main.run
702
+ meta = main.metadata
703
+
704
+ assert_includes meta[:report][:stats].keys, "value"
705
+ refute_includes meta[:report][:stats].keys, "id"
706
+ ensure
707
+ template&.unlink
708
+ end
709
+
710
+ def test_stats_whitelist_takes_precedence_over_ignore
711
+ template_html = <<~HTML
712
+ <!DOCTYPE html>
713
+ <html><body>
714
+ <table>
715
+ <report stats="value" ignore-stats="value">
716
+ <report-page rows="10">
717
+ <report-row><tr><td><%= attr(:value) %></td></tr></report-row>
718
+ </report-page>
719
+ </report>
720
+ </table>
721
+ </body></html>
722
+ HTML
723
+ template = write_temp_template(template_html)
724
+ main = Main.new(template.path, @data)
725
+ main.run
726
+
727
+ assert_includes main.metadata[:report][:stats].keys, "value"
728
+ ensure
729
+ template&.unlink
730
+ end
731
+
608
732
  def test_metadata_no_title
609
733
  content = SIMPLE_TEMPLATE.gsub("<title>Test</title>", "")
610
734
  template = write_temp_template(content)
data/test/page_test.rb CHANGED
@@ -72,6 +72,22 @@ module Repolho
72
72
  assert_equal 0, @page.row_number
73
73
  end
74
74
 
75
+
76
+ def test_finalize_removes_empty_trailing_page
77
+ @page << "<tr><td>real</td></tr>"
78
+ @page.send(:close_page)
79
+
80
+ # Simulate the empty page opened by a blank append after an exact fill.
81
+ @page.send(:open_page)
82
+ @page << ""
83
+ @page.finalize
84
+
85
+ joined = @page.content.join
86
+ assert_includes joined, 'data-page="1"'
87
+ refute_includes joined, 'data-page="2"'
88
+ assert_equal 2, @page.page_number # one past the last real page
89
+ end
90
+
75
91
  def test_content_is_array_of_strings
76
92
  @page.append_raw("before")
77
93
  @page << "<tr><td>row</td></tr>"
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,11 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: repolho
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eustáquio Rangel
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain:
11
10
  - |
@@ -36,7 +35,7 @@ cert_chain:
36
35
  L0XcRn7yJijWxXFMBrnwp/4PHjPT0l6Qci+zxGly0D7LCTHa5WSyWujq4BZw55Yp
37
36
  G13dwkTphaMB
38
37
  -----END CERTIFICATE-----
39
- date: 2026-11-09 00:00:00.000000000 Z
38
+ date: 1980-01-02 00:00:00.000000000 Z
40
39
  dependencies:
41
40
  - !ruby/object:Gem::Dependency
42
41
  name: nokogiri
@@ -114,7 +113,6 @@ licenses:
114
113
  metadata:
115
114
  homepage_uri: https://github.com/taq/repolho
116
115
  source_code_uri: https://github.com/taq/repolho
117
- post_install_message:
118
116
  rdoc_options: []
119
117
  require_paths:
120
118
  - lib
@@ -129,8 +127,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
129
127
  - !ruby/object:Gem::Version
130
128
  version: '0'
131
129
  requirements: []
132
- rubygems_version: 3.5.3
133
- signing_key:
130
+ rubygems_version: 3.6.9
134
131
  specification_version: 4
135
132
  summary: Report generator for Ruby and Rails from .rep.erb templates
136
133
  test_files: []
metadata.gz.sig CHANGED
Binary file