repolho 0.2.1 → 0.2.2

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: cbce0fb32bf82c14c5a0b6046dab2334da176a57dac2d004f8294a1364811773
4
+ data.tar.gz: '0759da9158bb03a696d5a9f23a1c453c0c1fa420f913c319bd98b40b7d646907'
5
5
  SHA512:
6
- metadata.gz: d9f15437ff86ee242205fdd24dd52f994a9da1c2dcd0d83814a5ef6b6ca04c69927acc4a014d9c3946686dd2063e31c16b10f246107cfbad95b9d7d8f13eb00a
7
- data.tar.gz: 4cf688c47fc6658d4c112246f1073f10bad0db3dfa10b85edf47151dd3379c0e002a12a6fdb45ce7cf90bcdc1cff59bd102f8d6f048a4ee32e63021384a9af36
6
+ metadata.gz: 22d3581a89caefa281f804e116ea42c0d535d7e356a66103500a690aed7c1572453250db70eb90247f2b1838c6ca988a752af8a5973ac42b2fa096d39c8d7bd0
7
+ data.tar.gz: 1ca39f3fceb142d7dbc7b24902feb9fb908277e69806ef4a0da846fce9a34d512ed0200a2906dc60784bbb3809cd9bd1d98be70dd7738d91436cc2d73216b329
checksums.yaml.gz.sig CHANGED
Binary file
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:
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,13 @@ 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
+
110
141
  inject_last_page_content
111
142
 
112
143
  @report.call(:finish, nil)
@@ -141,6 +172,9 @@ module Repolho
141
172
  end
142
173
 
143
174
  @page.append_raw(after_content)
175
+
176
+ @generation_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
177
+ @ran = true
144
178
  end
145
179
 
146
180
  private
@@ -225,10 +259,17 @@ module Repolho
225
259
 
226
260
  def parse
227
261
  content = File.read(@path)
262
+ extract_title(content)
228
263
  extract_sections(content)
229
264
  build_report_structure
230
265
  end
231
266
 
267
+ def extract_title(content)
268
+ doc = Nokogiri::HTML.parse(content)
269
+ title = doc.at_css("title")&.text&.strip
270
+ @title = title.nil? || title.empty? ? nil : title
271
+ end
272
+
232
273
  def extract_sections(content)
233
274
  matches = content.gsub(/\n/, "").match(/(?<previous>\A.*?)(?<body><body[^>]*>.*?)(?<report><report[^>]*>.*?<\/report>)(?<after>.*)\z/m)
234
275
 
@@ -329,5 +370,47 @@ module Repolho
329
370
 
330
371
  ERB.new(CGI.unescapeHTML(content)).result(@page.instance_eval { binding })
331
372
  end
373
+
374
+ # Builds a hash of attribute => {sum, min, max, count, avg} from a Stats collection.
375
+ # Prefers Integer when the value is a whole number. Skips attributes with no data.
376
+ def stats_to_hash(stats)
377
+ result = {}
378
+ stats.each do |attr, s|
379
+ next if s.count.zero?
380
+
381
+ result[attr] = {
382
+ sum: prefer_integer(s.sum),
383
+ min: prefer_integer(s.min),
384
+ max: prefer_integer(s.max),
385
+ count: s.count,
386
+ avg: prefer_integer(s.avg)
387
+ }
388
+ end
389
+ result
390
+ end
391
+
392
+ def prefer_integer(value)
393
+ return value unless value.is_a?(Numeric)
394
+ (value == value.to_i) ? value.to_i : value
395
+ end
396
+
397
+ # Collects groups that processed at least one row (flat list, no nesting).
398
+ # Only the last segment's stats are available (resets happen on attribute breaks).
399
+ def collect_groups_with_data
400
+ groups = []
401
+ collect_groups_recursive(@page.containers, groups)
402
+ groups.filter_map do |group|
403
+ stats = stats_to_hash(group.stats)
404
+ { id: group.id, stats: stats } if stats.any?
405
+ end
406
+ end
407
+
408
+ def collect_groups_recursive(containers, acc)
409
+ containers.each do |container|
410
+ next if container.is_a?(Page)
411
+ acc << container
412
+ collect_groups_recursive(container.containers, acc) if container.containers.any?
413
+ end
414
+ end
332
415
  end
333
416
  end
@@ -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.2"
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,96 @@ 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
+ def test_metadata_no_title
609
+ content = SIMPLE_TEMPLATE.gsub("<title>Test</title>", "")
610
+ template = write_temp_template(content)
611
+ main = Main.new(template.path, @data)
612
+ main.run
613
+ assert_nil main.metadata[:title]
614
+ ensure
615
+ template&.unlink
616
+ end
526
617
  end
527
618
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,10 +1,11 @@
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.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eustáquio Rangel
8
+ autorequire:
8
9
  bindir: bin
9
10
  cert_chain:
10
11
  - |
@@ -35,7 +36,7 @@ cert_chain:
35
36
  L0XcRn7yJijWxXFMBrnwp/4PHjPT0l6Qci+zxGly0D7LCTHa5WSyWujq4BZw55Yp
36
37
  G13dwkTphaMB
37
38
  -----END CERTIFICATE-----
38
- date: 1980-01-02 00:00:00.000000000 Z
39
+ date: 2026-11-09 00:00:00.000000000 Z
39
40
  dependencies:
40
41
  - !ruby/object:Gem::Dependency
41
42
  name: nokogiri
@@ -113,6 +114,7 @@ licenses:
113
114
  metadata:
114
115
  homepage_uri: https://github.com/taq/repolho
115
116
  source_code_uri: https://github.com/taq/repolho
117
+ post_install_message:
116
118
  rdoc_options: []
117
119
  require_paths:
118
120
  - lib
@@ -127,7 +129,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
129
  - !ruby/object:Gem::Version
128
130
  version: '0'
129
131
  requirements: []
130
- rubygems_version: 4.0.20
132
+ rubygems_version: 3.5.3
133
+ signing_key:
131
134
  specification_version: 4
132
135
  summary: Report generator for Ruby and Rails from .rep.erb templates
133
136
  test_files: []
metadata.gz.sig CHANGED
Binary file