Porf/Framework.pm

filter: Statementstmt 89.6% 147 / 164 Branchbran 60.6% 40 / 66 Conditioncond 58.8% 10 / 17 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 94.5% 35 / 37 Podpod 4.3% 1 / 23 totaltotal 74.5% 237 / 318 CC 41 SCAR 19.3
CC
41
Cov
80%
auto_report
18.0
extract_format_of_filename
14.2
create
6.5
CRAP 6.9
« Porf/examples/demo.pl ↑ Summary Porf/Util.pm »
1 # Perl
2 #
3 # Class Report::Porf::Framework
4 #
5 # Perl Open Report Framework (Porf)
6 #
7 # Framework to create/configure Reports for any output format.
8 #
9 # Ralf Peine, Tue May 27 11:30:17 2014
10 #
11 # More documentation at the end of file
12 #------------------------------------------------------------------------------
13  
14 1 $VERSION = "2.001";
15  
16 #------------------------------------------------------------------------------
17 #
18 # Example list with 10 lines (html)
19 #
20 # <html>
21 # <table border='1' rules='groups'>
22 #
23 # <thead>
24 # <tr><th>Coun</th><th>TimeStamp </th><th>Age </th><th>Prename </th><th>Surname </th></tr>
25 # </thead>
26 #
27 # <tr><td>1 </td><td>0.000433 </td><td>10 </td><td>Vorname 1 </td><td>Name 1 </td></tr>
28 # <tr><td>2 </td><td>0.000638 </td><td>20 </td><td>Vorname 2 </td><td>Name 2 </td></tr>
29 # <tr><td>3 </td><td>0.000781 </td><td>30 </td><td>Vorname 3 </td><td>Name 3 </td></tr>
30 # <tr><td>4 </td><td>0.000922 </td><td>40 </td><td>Vorname 4 </td><td>Name 4 </td></tr>
31 # <tr><td>5 </td><td>0.001062 </td><td>50 </td><td>Vorname 5 </td><td>Name 5 </td></tr>
32 # <tr><td>6 </td><td>0.001203 </td><td>60 </td><td>Vorname 6 </td><td>Name 6 </td></tr>
33 # <tr><td>7 </td><td>0.001346 </td><td>70 </td><td>Vorname 7 </td><td>Name 7 </td></tr>
34 # <tr><td>8 </td><td>0.001486 </td><td>80 </td><td>Vorname 8 </td><td>Name 8 </td></tr>
35 # <tr><td>9 </td><td>0.001631 </td><td>90 </td><td>Vorname 9 </td><td>Name 9 </td></tr>
36 # <tr><td>10 </td><td>0.001773 </td><td>100 </td><td>Vorname 10 </td><td>Name 10 </td></tr>
37 #
38 # </table><p/>
39 # # Time needed for export of 10 data lines: 0.002013
40 #
41 # </html>
42 #
43  
44 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 #--------------------------------------------------------------------------------
48 #
49 # Report::Porf::Framework
50 #
51 #--------------------------------------------------------------------------------
52  
53 package Report::Porf::Framework;
54  
55 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 1 ▶use Report::Porf::Table::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 ▶use Report::Porf::Table::Simple::AutoColumnConfigurator;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 # only for default creation
62 1 ▶use Report::Porf::Table::Simple::HtmlReportConfigurator;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 ▶use Report::Porf::Table::Simple::TextReportConfigurator;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 1 ▶use Report::Porf::Table::Simple::CsvReportConfigurator;
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 1 our %store; # of named frameworks
67  
68 1 our $DefaultFramework = '';
69  
70 #--------------------------------------------------------------------------------
71 #
72 # Creation / Filling Of Instances
73 #
74 #--------------------------------------------------------------------------------
75  
76 # --- new Instance, Do NOT call direct!!! -----------------
77 sub _new
78 2 ▶{
Subroutine _new: called CC=2 SCAR=7.1
79 2 my $caller = $_[0];
80 2 ▶ my $class = ref($caller) || $caller;
Condition33%1/3
ref $caller || $caller
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $caller || $caller
ref $caller $caller
Truth table33%1/3
A || B
A= ref $callerB= $caller
ABresult
1X1
011
000
81
82 # let the class go
83 2 my $self = {};
84 2 bless $self, $class;
85  
86 2 $self->{Configurators} = {};
87  
88 2 return $self;
89 }
90  
91 # --- CreateInstance --------------------------------------
92 2 ▶sub create {
Subroutine create: called CC=6 SCAR=18.7
Pod documented
93 2 ▶ my %options = @_ if scalar @_;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ 2 0 2
94  
95 2 my $framework = Report::Porf::Framework->_new();
96 2 $framework->use_default_configurator_creators();
97  
98 2 ▶ $framework->set_name($options{-name}) if $options{-name};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"-name"} 2 0 2
99 2 ▶ $framework->set_description($options{-description}) if $options{-description};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"-description"} 1 1 2
100 2 $framework->set_default_format('text');
101 2 $framework->set_max_rows(10);
102  
103 2 foreach my $key (sort(keys(%options))) {
104 3 ▶ if ($key =~ /^\-create(\w+)ConfiguratorAction$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^\-create(\w+)ConfiguratorAction$/) 0 3 3
105 0 my $format = $1;
106 0 $framework->set_configurator_action($format => $options{$key});
107 }
108 }
109  
110 2 $framework->store();
111  
112 2 return $framework;
113 }
114  
115 # --- Activate the default configurators for every known output format -------------
116 2 ▶sub use_default_configurator_creators {
Subroutine use_default_configurator_creators: called CC=1 SCAR=0.0
Pod undocumented
117 2 my ($self, # instance_ref
118 ) = @_;
119  
120 3 ▶ $self->set_configurator_action(HTML => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
121 3 my $configurator = Report::Porf::Table::Simple::HtmlReportConfigurator->new();
122 3 $configurator->set_alternate_row_colors('#DDDDDD', '#FFFFFF');
123 3 return $configurator;
124 2 });
125 $self->set_configurator_action
126 2 ▶ (Text => sub { return Report::Porf::Table::Simple::TextReportConfigurator->new();});
Subroutine __ANON__: called CC=1 SCAR=0.0
127 $self->set_configurator_action
128 2 ▶ (CSV => sub { return Report::Porf::Table::Simple::CsvReportConfigurator->new();});
Subroutine __ANON__: called CC=1 SCAR=0.0
129
130 $self->set_auto_configurator_action(
131 2 ▶ sub {return Report::Porf::Table::Simple::AutoColumnConfigurator->new();});
Subroutine __ANON__: called CC=1 SCAR=0.0
132 }
133  
134 #--------------------------------------------------------------------------------
135 #
136 # Attributes
137 #
138 #--------------------------------------------------------------------------------
139  
140 # --- Name ----------------------------------------------------------------------------
141  
142 2 ▶sub set_name {
Subroutine set_name: called CC=3 SCAR=15.1
Pod undocumented
143 2 my ($self, # instance_ref
144 $value # value to set
145 ) = @_;
146  
147 2 ▶ if ($self->{Name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Name"}) 0 2 2
148 0 ▶ if ($self->{Name} ne $value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Name"} ne $value) 0 0 0
149 0 die "Name of framework cannot be changed!";
150 }
151 }
152 2 $self->{Name} = $value;
153 }
154  
155 3 ▶sub get_name {
Subroutine get_name: called CC=1 SCAR=0.0
Pod undocumented
156 3 my ($self, # instance_ref
157 ) = @_;
158
159 3 return $self->{Name};
160 }
161  
162 # --- Description ----------------------------------------------------------------------------
163  
164 1 ▶sub set_description {
Subroutine set_description: called CC=1 SCAR=0.0
Pod undocumented
165 1 my ($self, # instance_ref
166 $value # value to set
167 ) = @_;
168  
169 1 $self->{Description} = $value;
170 }
171  
172 0 ▶sub get_description {
Subroutine get_description: not called CC=1 SCAR=6.9
Pod undocumented
173 0 my ($self, # instance_ref
174 ) = @_;
175
176 0 return $self->{Description};
177 }
178  
179 # --- Default Format ----------------------------------------------------------------------------
180  
181 2 ▶sub set_default_format {
Subroutine set_default_format: called CC=1 SCAR=0.0
Pod undocumented
182 2 my ($self, # instance_ref
183 $value # value to set
184 ) = @_;
185  
186 2 $self->{DefaultFormat} = $value;
187 }
188  
189 8 ▶sub get_default_format {
Subroutine get_default_format: called CC=1 SCAR=0.0
Pod undocumented
190 8 my ($self, # instance_ref
191 ) = @_;
192
193 8 return $self->{DefaultFormat};
194 }
195  
196 # --- MaxRows ----------------------------------------------------------------------------
197  
198 2 ▶sub set_max_rows {
Subroutine set_max_rows: called CC=1 SCAR=0.0
Pod undocumented
199 2 my ($self, # instance_ref
200 $value # value to set
201 ) = @_;
202  
203 2 $self->{MaxRows} = $value;
204 }
205  
206 4 ▶sub get_max_rows {
Subroutine get_max_rows: called CC=1 SCAR=0.0
Pod undocumented
207 4 my ($self, # instance_ref
208 ) = @_;
209
210 4 return $self->{MaxRows};
211 }
212  
213 # --- Configurator ---------------------------------------------------------------
214  
215 6 ▶sub set_configurator_action {
Subroutine set_configurator_action: called CC=3 SCAR=11.4
Pod undocumented
216 6 my ($self, # instance_ref
217 $format, # (html/text/csv)
218 $action # value to set
219 ) = @_;
220
221 6 ▶ die "Format not set" unless $format;
Branch: 1 branch
BranchTrueFalseTotal
unless $format 0 6 6
222 6 ▶ die "No action given" unless ref ($action) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $action eq "CODE" 0 6 6
223  
224 6 $self->{Configurators}->{lc($format)} = $action;
225 }
226  
227 14 ▶sub get_configurator_action {
Subroutine get_configurator_action: called CC=2 SCAR=7.1
Pod undocumented
228 14 my ($self, # instance_ref
229 $format, # (html/text/csv)
230 ) = @_;
231
232 14 ▶ die "Format not set" unless $format;
Branch: 1 branch
BranchTrueFalseTotal
unless $format 0 14 14
233  
234 14 return $self->{Configurators}->{lc($format)};
235 }
236  
237 # --- AutoConfigurator ---------------------------------------------------------------
238  
239 2 ▶sub set_auto_configurator_action {
Subroutine set_auto_configurator_action: called CC=2 SCAR=7.1
Pod undocumented
240 2 my ($self, # instance_ref
241 $action # value to set
242 ) = @_;
243
244 2 ▶ die "No action given" unless ref ($action) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $action eq "CODE" 0 2 2
245  
246 2 $self->{AutoConfigurator} = $action;
247 }
248  
249 13 ▶sub get_auto_configurator_action {
Subroutine get_auto_configurator_action: called CC=1 SCAR=0.0
Pod undocumented
250 13 my ($self, # instance_ref
251 ) = @_;
252
253 13 return $self->{AutoConfigurator};
254 }
255  
256 #--------------------------------------------------------------------------------
257 #
258 # Methods
259 #
260 #--------------------------------------------------------------------------------
261  
262 # --- get framework --- Creates default, if not existing ------------------------
263 16 ▶sub get {
Subroutine get: called CC=5 SCAR=16.3
Pod undocumented
264 16 my ($name # Optional: name of framework
265 ) = @_;
266  
267 16 my $framework;
268  
269 16 ▶ $name = $DefaultFramework unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 16 0 16
270  
271 16 ▶ unless ($name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 16 0 16
272 16 $name = '-default';
273 16 $framework = $store{$name};
274 16 ▶ $framework = create(
Branch: 1 branch
BranchTrueFalseTotal
unless $framework 1 15 16
275 -name => $name,
276 -description => 'Automatic created default framework')
277 ->store()
278 unless $framework;
279 }
280  
281 16 $framework = $store{$name};
282  
283 16 ▶ confess "No report framework with name '$name' stored"
Branch: 1 branch
BranchTrueFalseTotal
unless $framework 0 16 16
284 unless $framework;
285  
286 16 return $framework;
287 }
288  
289 # --- store Framework in internal %store ------------------------------------------
290  
291 3 ▶sub store {
Subroutine store: called CC=2 SCAR=7.0
Pod undocumented
292 3 my ($self, # instance_ref
293 ) = @_;
294  
295 3 my $name = $self->get_name();
296
297 3 ▶ confess "cannot store unnamed framework" unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 3 3
298  
299 3 $store{$name} = $self;
300 }
301  
302 # --- Define the default framework to use --------------------------------------------------
303 0 ▶sub set_default_framework {
Subroutine set_default_framework: not called CC=1 SCAR=6.9
Pod undocumented
304 0 my ($name, # instance_ref
305 ) = @_;
306  
307 0 get($name); # dies if not existing
308 0 my $old = $DefaultFramework;
309 0 $DefaultFramework = $name;
310  
311 0 return $old;
312 }
313  
314 # --- create Report Configurator --------------------------------------------------
315  
316 14 ▶sub create_report_configurator {
Subroutine create_report_configurator: called CC=2 SCAR=7.0
Pod undocumented
317 14 my ($self, # instance_ref
318 $format # format of report
319 ) = @_;
320  
321 14 my $action = $self->get_configurator_action(lc($format));
322  
323 14 ▶ die "don't know how to configure report for format '$format'"
Branch: 1 branch
BranchTrueFalseTotal
unless $action 0 14 14
324 unless $action;
325  
326 14 return $action->();
327 }
328  
329 # --- create Report ---------------------------------------------------------------
330  
331 14 ▶sub create_report {
Subroutine create_report: called CC=2 SCAR=7.0
Pod undocumented
332 14 my ($self, # instance_ref
333 $format # format of report
334 ) = @_;
335  
336 14 ▶ $format = $self->set_default_format('text') unless $format;
Branch: 1 branch
BranchTrueFalseTotal
unless $format 0 14 14
337  
338 14 my $report_configurator = $self->create_report_configurator($format);
339 14 return $report_configurator->create_and_configure_report();
340 }
341  
342 # --- extract format out of file name, if $file is an file name ---------------
343 8 ▶sub extract_format_of_filename {
Subroutine extract_format_of_filename: called CC=6 SCAR=26.5
Pod undocumented
344 8 my ($self, # instance_ref
345 $file_name # format of report
346 ) = @_;
347  
348 8 my $format = '';
349  
350 8 ▶ if ($file_name && ref($file_name) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_name and ref $file_name eq "") 0 8 8
Condition66%2/3
$file_name and ref $file_name eq ""
!ll&&!rl&&r
440
MC/DC0%0/2
$file_name and ref $file_name eq ""
$file_name ref $file_name eq ""
Truth table66%2/3
A and B
A= $file_nameB= ref $file_name eq ""
ABresult
0X0
100
111
351 0 ▶ return 'Text' if $file_name =~ /\.(txt|text)$/io;
Branch: 1 branch
BranchTrueFalseTotal
if $file_name =~ /\.(txt|text)$/io 0 0 0
352 0 ▶ return 'Html' if $file_name =~ /\.(htm|html)$/io;
Branch: 1 branch
BranchTrueFalseTotal
if $file_name =~ /\.(htm|html)$/io 0 0 0
353 0 ▶ return 'Csv' if $file_name =~ /\.csv$/io;
Branch: 1 branch
BranchTrueFalseTotal
if $file_name =~ /\.csv$/io 0 0 0
354 }
355  
356 8 return '';
357 }
358  
359  
360 # --- create auto report configuration -----------------------------------------------------------------
361 2 ▶sub create_auto_report_configuration {
Subroutine create_auto_report_configuration: called CC=1 SCAR=0.0
Pod undocumented
362 2 my $report_framework = Report::Porf::Framework::get();
363 2 my $configurator = $report_framework->get_auto_configurator_action()->();
364  
365 2 return $configurator->create_report_configuration(@_);
366 }
367  
368 # --- create auto report configuration -----------------------------------------------------------------
369 1 ▶sub report_configuration_as_string {
Subroutine report_configuration_as_string: called CC=1 SCAR=0.0
Pod undocumented
370 1 my $report_framework = Report::Porf::Framework::get();
371 1 my $configurator = $report_framework->get_auto_configurator_action()->();
372  
373 1 return $configurator->report_configuration_as_string(@_);
374 }
375  
376  
377 # --- auto report -----------------------------------------------------------------
378 12 ▶sub auto_report {
Subroutine auto_report: called CC=17 SCAR=28.9
Pod undocumented
379 12 my ($list_ref, # what to print out
380 @all_args # named args
381 ) = @_;
382  
383 12 ▶ return 0 unless defined $list_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $list_ref 1 11 12
384  
385 11 ▶ unless (ref($list_ref) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $list_ref eq "ARRAY") 0 11 11
386 0 ▶ my $type = ref($list_ref) || "no reference";
Condition0%0/2
ref $list_ref || 'no reference'
l!l
00
MC/DC0%0/1
ref $list_ref || 'no reference'
ref $list_ref
Truth table0%0/2
A
A= ref $list_ref
Aresult
11
00
387 0 die "auto_report() needs ref to array as first arg but is $type";
388 }
389  
390 11 ▶ return 0 unless scalar @$list_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$list_ref 1 10 11
391  
392 10 my $report_framework = Report::Porf::Framework::get();
393 10 my $configurator = $report_framework->get_auto_configurator_action()->();
394  
395 10 my %args = ();
396 10 my $file_item = '';
397 10 my $format = '';
398
399 10 ▶ if (scalar @all_args == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @all_args == 1) { } 0 10 10
400 0 $file_item = $all_args[0];
401 }
402 else {
403 10 %args = @all_args;
404 10 ▶ $file_item = $args{-file} if $args{-file};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"-file"} 6 4 10
405 }
406  
407 10 my $max_rows = '';
408 10 ▶ $max_rows = $args{-max_rows} if defined $args{-max_rows};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"-max_rows"} 1 9 10
409 10 ▶ $max_rows = '' unless defined $max_rows;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max_rows 0 10 10
410  
411 10 ▶ if (defined $args{-format}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'-format'}) { } 2 8 10
412 2 $format = $args{-format};
413 }
414 else {
415 8 $format = $report_framework->extract_format_of_filename($file_item);
416 }
417
418 10 ▶ $format = $report_framework->get_default_format() unless $format;
Branch: 1 branch
BranchTrueFalseTotal
unless $format 8 2 10
419  
420 10 my $report = $configurator->create_report($list_ref, $report_framework, $format);
421  
422 # only max rows without file item set (print not too many rows to stdout)
423 10 ▶ if (!$file_item || $max_rows) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $file_item or $max_rows) 5 5 10
Condition100%3/3
not $file_item or $max_rows
l!l&&r!l&&!r
415
MC/DC100%2/2
not $file_item or $max_rows
not $file_item $max_rows
Truth table100%3/3
not(A) or B
A= not $file_itemB= $max_rows
ABresult
1X1
011
000
424 5 ▶ $max_rows = $report_framework->get_max_rows() if $max_rows eq '' && !$file_item;
Branch: 1 branch
BranchTrueFalseTotal
if $max_rows eq "" and not $file_item 4 1 5
Condition66%2/3
$max_rows eq "" and not $file_item
!ll&&!rl&&r
104
MC/DC50%1/2
$max_rows eq "" and not $file_item
$max_rows eq "" not $file_item
Truth table66%2/3
A and not(B)
A= $max_rows eq ""B= not $file_item
ABresult
0X0
100
111
425 5 ▶ if ($max_rows > 0 && scalar @$list_ref > $max_rows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_rows > 0 and scalar @$list_ref > $max_rows) 2 3 5
Condition66%2/3
$max_rows > 0 and scalar @$list_ref > $max_rows
!ll&&!rl&&r
032
MC/DC50%1/2
$max_rows > 0 and scalar @$list_ref > $max_rows
$max_rows > 0 scalar @$list_ref > $max_rows
Truth table66%2/3
A and B
A= $max_rows > 0B= scalar @$list_ref > $max_rows
ABresult
0X0
100
111
426 2 $max_rows--;
427 2 my @rows = @{$list_ref}[0..$max_rows];
428 2 $list_ref = \@rows;
429 }
430 }
431  
432 10 $report->write_all($list_ref, $file_item);
433  
434 10 return scalar @$list_ref; # rows printed out
435 }
436  
437 1 1;
438  
439 __END__
« Porf/examples/demo.pl ↑ Summary Porf/Util.pm »