File Coverage

blib/lib/Report/Porf/Table/Simple/TextReportConfigurator.pm
Criterion Covered Total %
statement 159 169 94.0
branch 30 56 53.5
condition 2 6 33.3
subroutine 14 16 87.5
pod 0 5 0.0
total 205 252 81.3


line stmt bran cond sub pod time code
1             # perl
2             #
3             # Class Report::Porf::Table::Simple::TextReportConfigurator
4             #
5             # Perl Open Report Framework (Porf)
6             #
7             # Configures a Report::Porf::Table::Simple to write out ASCII- or UTF-Text tables
8             #
9             # Ralf Peine, Tue May 27 11:29:41 2014
10             #
11             # More documentation at the end of file
12             #------------------------------------------------------------------------------
13              
14             $VERSION = "2.001";
15              
16             #------------------------------------------------------------------------------
17             #
18             # Example list with 10 lines (text)
19             #
20             # *============+======================+=========+=======+======================*
21             # @ Count @ Prename @ Surname @ Age @ TimeStamp @
22             # *------------+----------------------+---------+-------+----------------------*
23             # | 1 | Vorname 1 | Name 1 | 10 | 0.002329 |
24             # | 2 | Vorname 2 | Name 2 | 20 | 0.003106 |
25             # | 3 | Vorname 3 | Name 3 | 30 | 0.003822 |
26             # | 4 | Vorname 4 | Name 4 | 40 | 0.004533 |
27             # | 5 | Vorname 5 | Name 5 | 50 | 0.005235 |
28             # | 6 | Vorname 6 | Name 6 | 60 | 0.005944 |
29             # | 7 | Vorname 7 | Name 7 | 70 | 0.006656 |
30             # | 8 | Vorname 8 | Name 8 | 80 | 0.007362 |
31             # | 9 | Vorname 9 | Name 9 | 90 | 0.008069 |
32             # | 10 | Vorname 10 | Name 10 | 100 | 0.008779 |
33             # *============+======================+=========+=======+======================*
34             #
35             # # Time needed for export of 10 data lines: 0.001954
36             #
37             #------------------------------------------------------------------------------
38              
39 1     1   8 use strict;
  1         2  
  1         44  
40 1     1   6 use warnings;
  1         2  
  1         53  
41              
42             #--------------------------------------------------------------------------------
43             #
44             # Report::Porf::Table::Simple::TextReportConfigurator;
45             #
46             #--------------------------------------------------------------------------------
47              
48             package Report::Porf::Table::Simple::TextReportConfigurator;
49              
50 1     1   6 use Carp;
  1         2  
  1         94  
51              
52 1     1   7 use Report::Porf::Util;
  1         1  
  1         101  
53 1     1   29 use Report::Porf::Table::Simple;
  1         2  
  1         2635  
54              
55             #--------------------------------------------------------------------------------
56             #
57             # Creation / Filling Of Instances
58             #
59             #--------------------------------------------------------------------------------
60              
61             # --- create Instance -----------------
62             sub new
63             {
64 10     10 0 474 my $caller = $_[0];
65 10   33     68 my $class = ref($caller) || $caller;
66            
67             # let the class go
68 10         19 my $self = {};
69 10         37 bless $self, $class;
70              
71 10         297 return $self;
72             }
73              
74             # --- verbose ---------------------------------------------------------------
75              
76             sub set_verbose {
77 0     0 0 0 my ($self, # instance_ref
78             $value # value to set
79             ) = @_;
80            
81 0         0 $self->{verbose} = $value;
82             }
83              
84             sub get_verbose {
85 0     0 0 0 my ($self, # instance_ref
86             ) = @_;
87            
88 0         0 return $self->{verbose};
89             }
90              
91             #--------------------------------------------------------------------------------
92             #
93             # Configure format
94             #
95             #--------------------------------------------------------------------------------
96              
97             # --- create new report and configure -------------------------------------------
98             sub create_and_configure_report {
99 9     9 0 13 my ($self # instance_ref
100             ) = @_;
101              
102 9         56 return $self->configure_report(Report::Porf::Table::Simple->new());
103             }
104              
105             # --- Configure Export For Text ------------------------------------------------
106             sub configure_report {
107 10     10 0 23 my ($self, # instance_ref
108             $report2configure, # report to be configured
109             ) = @_;
110              
111 10         31 $report2configure->set_format('Text');
112              
113 10         29 $report2configure->set_default_column_width (10);
114 10         30 $report2configure->set_default_align ('Left');
115              
116 10         24 $report2configure->set_file_start('');
117 10         32 $report2configure->set_file_end ('');
118              
119 10         29 $report2configure->set_table_start("\n");
120 10         30 $report2configure->set_table_end ("\n");
121              
122 10         30 $report2configure->set_row_start("|");
123 10         30 $report2configure->set_row_end ("\n");
124            
125 10         26 $report2configure->set_header_row_start("|");
126 10         31 $report2configure->set_header_row_end ("\n");
127 10         30 $report2configure->set_header_start(" ");
128 10         26 $report2configure->set_header_end (" |");
129            
130 10         26 $report2configure->set_cell_start(" ");
131 10         24 $report2configure->set_cell_end (" |");
132              
133 10         33 $report2configure->set_horizontal_separation_start ("*");
134 10         25 $report2configure->set_horizontal_separation_column_separator ("+");
135 10         28 $report2configure->set_horizontal_separation_end ("*\n");
136 10         62 $report2configure->set_horizontal_separation_char ("-");
137 10         28 $report2configure->set_horizontal_separation_bold_char ("=");
138              
139             #===================================================================================
140             #
141             # Configure Actions, no need to speed up
142             #
143             #===================================================================================
144              
145             # === Column store Action ============================================================
146             $report2configure->set_configure_column_action(
147             sub {
148 52     52   72 my $report = shift; # instance_ref
149 52         164 my %options = @_; # options
150            
151 52 50       133 print_hash_ref(\%options) if verbose($report, 2);
152            
153 52         129 my $cell_start = $report->get_cell_start();
154 52         372 my $cell_end = $report->get_cell_end();
155              
156 52         66 my $left = ""; # left from value
157 52         61 my $right = ""; # right from value
158              
159             # --- default value ---------------------------------------
160 52         140 my $default_value = get_option_value(\%options, qw (-default_value -def_val -dv));
161 52 50       356 $default_value = $report->get_default_cell_value() unless defined $default_value;
162              
163             # --- value ---------------------------------------
164 52         136 my $value = interprete_value_options(\%options);
165 52         88 my $value_ref = ref($value);
166 52         54 my $value_action;
167              
168 52 50       102 die "value action not defined" unless defined $value;
169            
170 52 100       84 if ($value_ref) {
171 2 50       10 if ($value_ref =~ /^CODE/) {
172 2         3 $value_action = $value;
173             }
174             else {
175             # or what ??
176 0         0 die "# ref(value_ref) = $value_ref unknown";
177             # $value_action = eval ("sub { return $$value; };");
178             }
179             }
180             else {
181 50         147 $value = complete_value_code($value, $default_value);
182 50         241 $value_action = $report->create_action("$value;");
183             }
184              
185             # --- format value ----------------------------------------
186 52         142 my $format = get_option_value(\%options, qw (-format -f));
187              
188 52 50       111 if ($format) {
189 0         0 my $format_ref = ref($format);
190              
191 0 0       0 die "You used a '$format_ref' type to set the 'Format', "
192             ."but currently only strings are supported!"
193             if $format_ref;
194            
195 0 0       0 print "format $format\n" if verbose($report, 3);
196              
197 0         0 $left = "sprintf(\"$format\", ".$left;
198 0         0 $right .= ")";
199             }
200              
201             # --- coloring is not supported for text ------------------
202 52         122 my $color = get_option_value(\%options, qw (-color -c));
203              
204 52 50 33     116 print "color cannot be supported for text (color = $color)\n" if verbose($report, 3) && $color;
205              
206             # --- width / align ---------------------------------------
207 52         122 my $width = get_option_value(\%options, qw (-width -w));
208 52         123 my $align = get_option_value(\%options, qw (-align -a));
209              
210 52 100       160 $width = $report->get_default_column_width()
211             unless defined $width;
212              
213 52 100       80 if ($align) {
214 50         74 my $align_ref = ref($align);
215              
216 50 50       91 die "You used a '$align_ref' type to set the 'Align', "
217             ."but currently only strings are supported!"
218             if $align_ref;
219              
220 50         118 $align = interprete_alignment($align);
221            
222 50 50       191 print "align $align\n" if verbose($report, 3);
223             }
224             else {
225 2         4 $align = '';
226             }
227            
228 52 50       117 if (defined $width) {
229 52         61 my $width_ref = ref($width);
230              
231 52 50       94 die "You used a '$width_ref' type to set the 'Width', "
232             ."but currently only strings are supported!"
233             if $width_ref;
234            
235 52 50       113 print "width $width\n" if verbose($report, 3);
236              
237 52         211 push (@{$report->get_column_widths_ref()}, $width);
  52         135  
238            
239 52 100       108 $align = "left" unless $align;
240 52         87 $align = lc($align);
241              
242 52         217 $left = "const_length_$align($width, ".$left;
243 52         126 $right .= ")";
244             }
245            
246             # --- configure header ----------------------------
247              
248 52         128 my $header_text = get_option_value(\%options, qw (-header -h));
249 52 50       399 $header_text = '' unless defined $header_text;
250 52         60 push (@{$report->get_header_texts_ref()}, $header_text);
  52         142  
251            
252             # --- ---------------------------------------------
253             # --- ---------------------------------------------
254             # --- ---------------------------------------------
255              
256             # --- build cell content action ---------------------------------------------
257 52         180 my $cell_action_str = $left.'$value_action->($_[0])'.$right;
258              
259 52         97 my $eval_str = 'sub { return $cell_start.'.$cell_action_str.'.$cell_end; }';
260              
261 52 50       121 print "### eval_str = sub { return $cell_start$cell_action_str$cell_end; }\n"
262             if verbose($report, 3);
263            
264 52         15253 my $cell_action = eval ($eval_str);
265            
266 52 50       146 print "### ref(cell_action) ".ref($cell_action) ."\n" if verbose($report, 3);
267            
268 52         153 $report->add_cell_output_action($cell_action);
269 10         139 });
270              
271             # === Configure Complete Action ====================================================
272             $report2configure->set_configure_complete_action(
273             sub {
274 10     10   21 my ($report, # instance_ref
275             ) = @_;
276              
277             # --- Build Header Lines -------------------------------------------
278 10         20 my $start_col = '';
279 10         14 my $start_col_bold = '';
280              
281 10         19 my $additional_sep_start_chars = '';
282 10         12 my $additional_sep_end_chars = '';
283 10         75 my $additional_bold_sep_start_chars = '';
284 10         19 my $additional_bold_sep_end_chars = '';
285 10         49 my $columSeparatorLength = length($report->get_horizontal_separation_column_separator());
286            
287 10         31 my $cell_start_length = length($report->get_cell_start());
288 10         29 my $cell_end_length = length($report->get_cell_end()) - $columSeparatorLength;
289              
290 10         39 my $bold_cell_start_length = length($report->get_header_start());
291 10         151 my $bold_cell_end_length = length($report->get_header_end()) - $columSeparatorLength;
292              
293             # print "$cell_start_length, $cell_end_length\n";
294            
295 10 50       62 $additional_sep_start_chars = $report->get_horizontal_separation_char() x $cell_start_length
296             if $cell_start_length > 0;
297 10 50       44 $additional_sep_end_chars = $report->get_horizontal_separation_char() x $cell_end_length
298             if $cell_end_length > 0;
299            
300 10 50       45 $additional_bold_sep_start_chars = $report->get_horizontal_separation_bold_char() x $bold_cell_start_length
301             if $bold_cell_start_length > 0;
302 10 50       45 $additional_bold_sep_end_chars = $report->get_horizontal_separation_bold_char() x $bold_cell_end_length
303             if $bold_cell_end_length > 0;
304            
305 10         37 my $sep = $report->get_horizontal_separation_start()
306             .$additional_sep_start_chars;
307 10         27 my $sep_bold = $report->get_horizontal_separation_start()
308             .$additional_bold_sep_start_chars;
309              
310             # --- build separator lines ---
311 10         14 foreach my $width (@{$report->get_column_widths_ref()}) {
  10         33  
312 52         122 $sep .= $start_col.
313             $report->get_horizontal_separation_char() x $width;
314              
315 52         132 $sep_bold .= $start_col_bold.
316             $report->get_horizontal_separation_bold_char() x $width;
317              
318 52         137 $start_col_bold =
319             $additional_bold_sep_start_chars x $columSeparatorLength
320             .$report->get_horizontal_separation_column_separator()
321             .$additional_bold_sep_end_chars;
322              
323 52         136 $start_col =
324             $additional_sep_start_chars x $columSeparatorLength
325             .$report->get_horizontal_separation_column_separator()
326             .$additional_sep_end_chars;
327             }
328              
329 10         37 $sep .= $report->get_horizontal_separation_char()
330             .$report->get_horizontal_separation_end();
331 10         54 $sep_bold .= $report->get_horizontal_separation_bold_char()
332             .$report->get_horizontal_separation_end();
333              
334 10         40 $report->set_header_line ($sep);
335 10         174 $report->set_separator_line ($sep);
336 10         35 $report->set_bold_header_line($sep_bold);
337 10         38 $report->set_table_end ($sep_bold);
338 10         172 });
339            
340             #===================================================================================
341             #
342             # Runtime Actions, no need to speed up
343             #
344             #===================================================================================
345              
346             # === Header Output Action ============================================================
347             $report2configure->set_header_output_action(
348             sub {
349 8     8   17 my ($report, # instance_ref
350             $data_ref # data to output
351             ) = @_;
352              
353 8         28 my $header_string = $report->get_header_row_start();
354 8         14 my $c = 0;
355 8         10 foreach my $header_text (@{$data_ref}) {
  8         20  
356 42         102 $header_string .= $report->get_header_start()
357             .const_length_center($report->get_column_widths_ref()->[$c],
358             $header_text)
359             .$report->get_header_end();
360 42         85 $c++;
361             }
362              
363 8         30 $header_string .= $report->get_header_row_end();
364              
365 8 50       80 print "### Header String:$header_string" if verbose($report, 2);
366            
367 8         40 return $header_string;
368 10         73 });
369              
370             # === Start Table Output Action ============================================================
371             $report2configure->set_start_table_output_action(
372             sub {
373 8     8   76 my ($report, # instance_ref
374             $data_ref # data to output
375             ) = @_;
376              
377 8         31 return $report->get_file_start()
378             .$report->get_table_start()
379             .$report->get_bold_header_line()
380             .$report->get_header_output()
381             .$report->get_header_line();
382            
383 10         80 });
384              
385             # === End Table Output Action ============================================================
386             $report2configure->set_end_table_output_action(
387             sub {
388 8     8   15 my ($report, # instance_ref
389             $data_ref # data to output
390             ) = @_;
391              
392 8         33 return $report->get_table_end()
393             .$report->get_file_end();
394 10         64 });
395              
396             #===================================================================================
397             #
398             # Runtime Mass Data Actions, be careful, don't slow it down !!
399             #
400             #===================================================================================
401              
402 10         19 my $previous_data_ref = '';
403              
404             # === Row Output Action ============================================================
405             $report2configure->set_row_output_action(
406             sub {
407 53     53   72 my ($report, # instance_ref
408             $data_ref # data to output
409             ) = @_;
410              
411 53         143 my $cell_output_actions_ref = $report->get_cell_output_actions();
412              
413 53         223 my $row_string = '';
414              
415             # --- Add something when group changes ----
416 53         365 my $row_group_changes_action = $report->get_row_group_changes_action();
417            
418 53 50       116 $row_string .= $row_group_changes_action->($previous_data_ref, $data_ref)
419             if $row_group_changes_action;
420              
421 53         77 $previous_data_ref = $data_ref;
422              
423             # --- start new row -----------------------
424 53         157 $row_string .= $report->get_row_start();
425 53         97 foreach my $action (@$cell_output_actions_ref) {
426 290 50       1113 print "### action $action\n" if verbose($report, 4);
427 290         12342 $row_string .= $action->($data_ref);
428             }
429              
430 53         170 $row_string .= $report->get_row_end();
431              
432 53 50       128 print "### Row String:$row_string" if verbose($report, 2);
433            
434 53         5200 return $row_string;
435 10         80 });
436              
437 10         116 return $report2configure;
438             }
439             1;
440              
441             =head1 NAME
442              
443             C
444              
445             Configures a Report::Porf::Table::Simple to write out ASCII- or UTF-Text tables.
446              
447             Part of Perl Open Report Framework (Porf).
448              
449             =head1 Documentation
450              
451             See Report::Porf::Framework.pm for documentation of features and usage.
452              
453             =head1 LICENSE AND COPYRIGHT
454              
455             Copyright (c) 2013 by Ralf Peine, Germany. All rights reserved.
456              
457             This library is free software; you can redistribute it and/or modify
458             it under the same terms as Perl itself, either Perl version 5.6.0 or,
459             at your option, any later version of Perl 5 you may have available.
460              
461             =head1 DISCLAIMER OF WARRANTY
462              
463             This library is distributed in the hope that it will be useful,
464             but without any warranty; without even the implied warranty of
465             merchantability or fitness for a particular purpose.
466              
467             =cut