File Coverage

blib/lib/Report/Porf/Table/Simple/CsvReportConfigurator.pm
Criterion Covered Total %
statement 134 146 91.7
branch 18 38 47.3
condition 4 12 33.3
subroutine 19 21 90.4
pod 0 9 0.0
total 175 226 77.4


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