repolho 0.1.7 → 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: 2947412a1701b9659d7abb8c24137db2bf71245dcd17dc6c7f2bc5524602e646
4
- data.tar.gz: 8bb7baf19c2dff982915c782ddb45699b9b134cb841ca23d5712feca21a019d7
3
+ metadata.gz: fd3898009be42319daf8afaa54b0a064b8952d107a8cef3ad0d3d5692c2d17ad
4
+ data.tar.gz: dabe5fac8684e094140bf276d14cddfef3c3ba03693b66049ea5d2a68de30fd5
5
5
  SHA512:
6
- metadata.gz: dc19d0a464fc9abe19eabcb06ea687b4ead5f401165a137f29ba76d6d3d7f8db422bf749e8627f3054357f93cd04928d928054b2ea2d5bcda4e64de303074122
7
- data.tar.gz: 263caa4a89a5fe552b484dc36e4cee55600ce5d63f21c2d279d29e35c8ebb28cb75cc557a574db332a0435431ec49d965f621bbb9313dd6f6c659d2311b208ee
6
+ metadata.gz: d89cf51d6df90425a07e33e666f3a6e63434e0ec7085b1701d10777e371cef6149e43d16b408f809360211ea934f1e514037747dcdede233938f752146e11661
7
+ data.tar.gz: 3f9ec0ebbeaa07dc650c05f6a7ed488d289febc9712c5ac96547a4cc6875a63093b254c1f58ed4bbf723263eb43b6f459e75f3a923b8f7803b2c3921e6bcb510
checksums.yaml.gz.sig CHANGED
Binary file
data/README.md CHANGED
@@ -77,25 +77,44 @@ Its content is injected as normal rows into the page flow, so:
77
77
 
78
78
  Because the block is only ever emitted at the end, you do not need to check `last_page?` inside it.
79
79
 
80
- Example:
80
+ You can print the **overall report total** from this block by using the second argument of the statistics methods (see below):
81
81
 
82
82
  ```html
83
- <report>
84
- <report-page rows="40">
85
- <report-header>...</report-header>
86
- <report-row>...</report-row>
87
- <report-footer>
88
- <tr><td>Page <%= page_number %></td></tr>
89
- </report-footer>
90
- </report-page>
91
-
92
- <report-last-page>
93
- <tr><td colspan="5">Final notes and signature</td></tr>
94
- <tr><td colspan="5">______________________________</td></tr>
95
- </report-last-page>
96
- </report>
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 -->
97
114
  ```
98
115
 
116
+ The `report` object is available in every ERB context (headers, footers, rows and last-page content).
117
+
99
118
  ### Allowed HTML inside page content
100
119
 
101
120
  Each page is emitted as a `<tbody class="repolho-page" data-page="N">`.
@@ -255,11 +274,12 @@ To preview the structure, rename the file to `.html` and open it in a browser
255
274
  | Method | Description |
256
275
  |--------|-------------|
257
276
  | `attr(:field)` | Value of the field on the current row |
258
- | `sum(:field)` | Sum within the current container |
259
- | `avg(:field)` | Average |
260
- | `min(:field)` / `max(:field)` | Minimum / maximum |
261
- | `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 |
262
281
  | `number` | Current page or group number |
282
+ | `report` | The root `Report` container (useful to call `report.sum(:field)`) |
263
283
 
264
284
  ## Rails helpers
265
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
@@ -89,6 +89,8 @@ module Repolho
89
89
 
90
90
  @report.containers << @page
91
91
  @report.page = @page
92
+ @report.report = @report
93
+ @page.report = @report
92
94
  end
93
95
 
94
96
  def content
@@ -299,6 +301,7 @@ module Repolho
299
301
  container.attr = element["attr"]
300
302
  container.reprint_header_on_page_break = element["reprint-header-on-page-break"] == "true"
301
303
  container.page = @page
304
+ container.report = @report
302
305
  container
303
306
  end
304
307
 
@@ -311,7 +314,7 @@ module Repolho
311
314
  def render_erb(content)
312
315
  return "" if content.nil? || content.empty?
313
316
 
314
- ERB.new(content).result(@page.instance_eval { binding })
317
+ ERB.new(CGI.unescapeHTML(content)).result(@page.instance_eval { binding })
315
318
  end
316
319
  end
317
320
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Repolho
4
- VERSION = "0.1.7"
4
+ VERSION = "0.1.8"
5
5
  end
data/test/main_test.rb CHANGED
@@ -402,6 +402,38 @@ module Repolho
402
402
  template&.unlink
403
403
  end
404
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
+
405
437
  def test_page_number_available_in_after_erb
406
438
  after_erb = <<~HTML
407
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.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eustáquio Rangel
metadata.gz.sig CHANGED
Binary file