repolho 0.2.1 → 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: 8f4aac6b18333e8a6d790ad2e130f681419393b0771b98b35368cc34bb11ec7f
4
- data.tar.gz: 327ddea1d9ecf3bcae6897eed05daba40fc6c1897e5390d0feb4552c19decbc3
3
+ metadata.gz: ae62871a294af728a74adc231bc5b0460c60289618c16d1fe57cfd3db4ea95f8
4
+ data.tar.gz: a86ebabe86f719dbbe05f85fe26d25ca8125c2b4623e0796c75fde836648a49d
5
5
  SHA512:
6
- metadata.gz: d9f15437ff86ee242205fdd24dd52f994a9da1c2dcd0d83814a5ef6b6ca04c69927acc4a014d9c3946686dd2063e31c16b10f246107cfbad95b9d7d8f13eb00a
7
- data.tar.gz: 4cf688c47fc6658d4c112246f1073f10bad0db3dfa10b85edf47151dd3379c0e002a12a6fdb45ce7cf90bcdc1cff59bd102f8d6f048a4ee32e63021384a9af36
6
+ metadata.gz: a08581aa77e1a2e0cb68aa93a3b84d738aa747cc64bf3c2b89cf6c9d8472bf8d8f80d7169bb651fd0de781f0f71929b6837d476a350796bf09691d2e3d72ac44
7
+ data.tar.gz: 4d232be9b7bbc4c12f4fcb01a892704a5200bc0eb21a9b7517d41fe1b1c7edae9ff0eeada100c1cf4e61a1d3124b570cffa2609585e75eac121541ae5e8c9cf5
checksums.yaml.gz.sig CHANGED
@@ -1,3 +1 @@
1
- H�2�q�=V���8�Vό��_�Q��ˢ�y-�s�;B���� hUdCi�0C���3���x�
2
- }�C���σ���G�+D�Y�ص�`�s���NN��Ak�d��+�,d�`�:�>>��`eB�.��c� S�y�^����B��qw�˧4������9��`����q8����Va,(E�Wi<��>>�K�W�P(���0��
3
- >��^�AZ$I��,��Yx!oWM��2`��.����;zq>�
1
+ �*���5<���ʃk44�����S�����v�p$!�7�\���͓����ύ��6ΐ 'K�*�D�Gv�~K*����q!��ݚn1T��O��m�7Ƅ4K-(�١���'���H`���9�뇙��M�d}ׁ%~�1)<:�-i஍�)���O�+*�|xz���R(^�(*e.�xs�X���F0ϝ?~��teƇ_QF�]6��^B�y� ���z|y�k9�(\�sE'L�L�LX�����U)^&���9��trX���y<;߇rv��^x+M����:>�w[ќ���4�i�pG42�4�E��~B�`�H� 1i�j+ YR/yqT�(��`�i���+ ��d>͇�T�ۤa9ۭs�E��
data/README.md CHANGED
@@ -39,6 +39,10 @@ report = Repolho::Main.new("app/views/reports/sales.rep.erb", rows, { company: "
39
39
  report.run
40
40
 
41
41
  html = report.content.join
42
+
43
+ # Optional: metadata after run (nil before run)
44
+ # meta = report.metadata
45
+ # meta[:title], meta[:pages], meta[:generation_time], meta[:report][:stats], meta[:groups]
42
46
  ```
43
47
 
44
48
  Or with Rails:
@@ -161,6 +165,17 @@ You can print the **overall report total** from this block by using the second a
161
165
  </report-last-page>
162
166
  ```
163
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
+
164
179
  ### Statistics context
165
180
 
166
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
@@ -86,6 +86,10 @@ module Repolho
86
86
  next: ">",
87
87
  last: "Last"
88
88
  }
89
+ @ran = false
90
+ @generation_time = nil
91
+ @title = nil
92
+ @stats_snapshot = nil
89
93
 
90
94
  @report.containers << @page
91
95
  @report.page = @page
@@ -97,7 +101,27 @@ module Repolho
97
101
  @page.content
98
102
  end
99
103
 
104
+ # Returns metadata after the report has been generated.
105
+ # Returns nil if called before #run.
106
+ # Groups reflect only the last segment that was processed (stats are reset on breaks).
107
+ def metadata
108
+ return nil unless @ran
109
+
110
+ {
111
+ title: @title,
112
+ pages: @page.page_number,
113
+ generation_time: @generation_time,
114
+ template: @path,
115
+ report: {
116
+ stats: @stats_snapshot[:report]
117
+ },
118
+ groups: @stats_snapshot[:groups]
119
+ }
120
+ end
121
+
100
122
  def run
123
+ start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
124
+
101
125
  parse
102
126
 
103
127
  @page.append_raw(render_erb(@previous))
@@ -107,6 +131,18 @@ module Repolho
107
131
  @report.call(:row, row)
108
132
  end
109
133
 
134
+ # Snapshot stats before the final :finish resets them.
135
+ # Report keeps the full accumulation; groups keep only their last segment.
136
+ @stats_snapshot = {
137
+ report: stats_to_hash(@report.stats),
138
+ groups: collect_groups_with_data
139
+ }
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
+
110
146
  inject_last_page_content
111
147
 
112
148
  @report.call(:finish, nil)
@@ -141,6 +177,9 @@ module Repolho
141
177
  end
142
178
 
143
179
  @page.append_raw(after_content)
180
+
181
+ @generation_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
182
+ @ran = true
144
183
  end
145
184
 
146
185
  private
@@ -151,8 +190,23 @@ module Repolho
151
190
  @page.mark_as_last!
152
191
  rendered = render_erb(@last_page_template)
153
192
  @page.inject_as_rows(rendered)
154
- # Page footer is rendered later by the normal :finish path,
155
- # 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
156
210
  end
157
211
 
158
212
  def inject_pagination_labels
@@ -223,12 +277,49 @@ module Repolho
223
277
  end
224
278
  end
225
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
+
226
310
  def parse
227
311
  content = File.read(@path)
312
+ extract_title(content)
228
313
  extract_sections(content)
229
314
  build_report_structure
230
315
  end
231
316
 
317
+ def extract_title(content)
318
+ doc = Nokogiri::HTML.parse(content)
319
+ title = doc.at_css("title")&.text&.strip
320
+ @title = title.nil? || title.empty? ? nil : title
321
+ end
322
+
232
323
  def extract_sections(content)
233
324
  matches = content.gsub(/\n/, "").match(/(?<previous>\A.*?)(?<body><body[^>]*>.*?)(?<report><report[^>]*>.*?<\/report>)(?<after>.*)\z/m)
234
325
 
@@ -251,6 +342,8 @@ module Repolho
251
342
  @page.min_row_for_break = report_node["min-row-for-break"].to_i
252
343
  end
253
344
 
345
+ apply_stats_filter(report_node)
346
+
254
347
  if @pagination
255
348
  @pagination_labels[:first] = report_node["pagination-first"] if report_node["pagination-first"]
256
349
  @pagination_labels[:prev] = report_node["pagination-prev"] if report_node["pagination-prev"]
@@ -329,5 +422,47 @@ module Repolho
329
422
 
330
423
  ERB.new(CGI.unescapeHTML(content)).result(@page.instance_eval { binding })
331
424
  end
425
+
426
+ # Builds a hash of attribute => {sum, min, max, count, avg} from a Stats collection.
427
+ # Prefers Integer when the value is a whole number. Skips attributes with no data.
428
+ def stats_to_hash(stats)
429
+ result = {}
430
+ stats.each do |attr, s|
431
+ next if s.count.zero?
432
+
433
+ result[attr] = {
434
+ sum: prefer_integer(s.sum),
435
+ min: prefer_integer(s.min),
436
+ max: prefer_integer(s.max),
437
+ count: s.count,
438
+ avg: prefer_integer(s.avg)
439
+ }
440
+ end
441
+ result
442
+ end
443
+
444
+ def prefer_integer(value)
445
+ return value unless value.is_a?(Numeric)
446
+ (value == value.to_i) ? value.to_i : value
447
+ end
448
+
449
+ # Collects groups that processed at least one row (flat list, no nesting).
450
+ # Only the last segment's stats are available (resets happen on attribute breaks).
451
+ def collect_groups_with_data
452
+ groups = []
453
+ collect_groups_recursive(@page.containers, groups)
454
+ groups.filter_map do |group|
455
+ stats = stats_to_hash(group.stats)
456
+ { id: group.id, stats: stats } if stats.any?
457
+ end
458
+ end
459
+
460
+ def collect_groups_recursive(containers, acc)
461
+ containers.each do |container|
462
+ next if container.is_a?(Page)
463
+ acc << container
464
+ collect_groups_recursive(container.containers, acc) if container.containers.any?
465
+ end
466
+ end
332
467
  end
333
468
  end
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.1"
4
+ VERSION = "0.2.3"
5
5
  end
data/test/main_test.rb CHANGED
@@ -471,7 +471,7 @@ module Repolho
471
471
  '<report-group id="state" attr="state" page-break-after="true">'
472
472
  ).gsub(
473
473
  '<report-page rows="10">',
474
- '<report-page rows="6">'
474
+ '<report-page rows="20">'
475
475
  )
476
476
 
477
477
  template = write_temp_template(template_content)
@@ -523,5 +523,220 @@ module Repolho
523
523
  ensure
524
524
  template&.unlink
525
525
  end
526
+
527
+ def test_metadata_returns_nil_before_run
528
+ template = write_temp_template(SIMPLE_TEMPLATE)
529
+ main = Main.new(template.path, @data)
530
+ assert_nil main.metadata
531
+ ensure
532
+ template&.unlink
533
+ end
534
+
535
+ def test_metadata_after_run_simple_report
536
+ template = write_temp_template(SIMPLE_TEMPLATE)
537
+ main = Main.new(template.path, @data)
538
+ main.run
539
+
540
+ meta = main.metadata
541
+ refute_nil meta
542
+
543
+ assert_equal "Test", meta[:title]
544
+ assert_equal template.path, meta[:template]
545
+ assert_kind_of Float, meta[:generation_time]
546
+ assert meta[:generation_time] >= 0
547
+ # rows="5" plus headers/footers produces 2 pages with 3 data rows
548
+ assert_equal 2, meta[:pages]
549
+
550
+ # Report stats are cumulative
551
+ value_stats = meta[:report][:stats]["value"]
552
+ assert_equal 45, value_stats[:sum] # 10+20+15
553
+ assert_equal 10, value_stats[:min]
554
+ assert_equal 20, value_stats[:max]
555
+ assert_equal 3, value_stats[:count]
556
+ assert_equal 15, value_stats[:avg]
557
+
558
+ # No groups in this template
559
+ assert_empty meta[:groups]
560
+ ensure
561
+ template&.unlink
562
+ end
563
+
564
+ def test_metadata_includes_groups_that_processed_data
565
+ template = write_temp_template(GROUP_TEMPLATE)
566
+ main = Main.new(template.path, @data)
567
+ main.run
568
+
569
+ meta = main.metadata
570
+ refute_nil meta
571
+
572
+ # Last group is RJ (only Carol, value 15)
573
+ groups = meta[:groups]
574
+ assert_equal 1, groups.size
575
+ assert_equal "state", groups.first[:id]
576
+
577
+ state_stats = groups.first[:stats]["value"]
578
+ assert_equal 15, state_stats[:sum]
579
+ assert_equal 15, state_stats[:min]
580
+ assert_equal 15, state_stats[:max]
581
+ assert_equal 1, state_stats[:count]
582
+ assert_equal 15, state_stats[:avg]
583
+
584
+ # Report still has the full total
585
+ assert_equal 45, meta[:report][:stats]["value"][:sum]
586
+ ensure
587
+ template&.unlink
588
+ end
589
+
590
+ def test_metadata_prefers_integers_for_whole_numbers
591
+ data = [{ "id" => 1, "value" => 10 }]
592
+ # Use a large page size so everything fits and stats are easy to predict
593
+ content = SIMPLE_TEMPLATE.gsub('rows="5"', 'rows="20"')
594
+ template = write_temp_template(content)
595
+ main = Main.new(template.path, data)
596
+ main.run
597
+
598
+ stats = main.metadata[:report][:stats]["value"]
599
+ assert_kind_of Integer, stats[:sum]
600
+ assert_kind_of Integer, stats[:min]
601
+ assert_kind_of Integer, stats[:max]
602
+ assert_kind_of Integer, stats[:count]
603
+ assert_kind_of Integer, stats[:avg]
604
+ ensure
605
+ template&.unlink
606
+ end
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
+
732
+ def test_metadata_no_title
733
+ content = SIMPLE_TEMPLATE.gsub("<title>Test</title>", "")
734
+ template = write_temp_template(content)
735
+ main = Main.new(template.path, @data)
736
+ main.run
737
+ assert_nil main.metadata[:title]
738
+ ensure
739
+ template&.unlink
740
+ end
526
741
  end
527
742
  end
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,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: repolho
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eustáquio Rangel
@@ -127,7 +127,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
127
  - !ruby/object:Gem::Version
128
128
  version: '0'
129
129
  requirements: []
130
- rubygems_version: 4.0.20
130
+ rubygems_version: 3.6.9
131
131
  specification_version: 4
132
132
  summary: Report generator for Ruby and Rails from .rep.erb templates
133
133
  test_files: []
metadata.gz.sig CHANGED
Binary file