File Coverage

blib/lib/Report/Porf/Framework.pm
Criterion Covered Total %
statement 143 160 89.3
branch 40 66 60.6
condition 10 17 58.8
subroutine 35 37 94.5
pod 1 23 4.3
total 229 303 75.5


line stmt bran cond sub pod time code
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             $VERSION = "2.001";
15              
16             #------------------------------------------------------------------------------
17             #
18             # Example list with 10 lines (html)
19             #
20             #
21             #
22             #
23             #
24             #
CounTimeStamp Age Prename Surname
25             #
26             #
27             #
1 0.000433 10 Vorname 1 Name 1
28             #
2 0.000638 20 Vorname 2 Name 2
29             #
3 0.000781 30 Vorname 3 Name 3
30             #
4 0.000922 40 Vorname 4 Name 4
31             #
5 0.001062 50 Vorname 5 Name 5
32             #
6 0.001203 60 Vorname 6 Name 6
33             #
7 0.001346 70 Vorname 7 Name 7
34             #
8 0.001486 80 Vorname 8 Name 8
35             #
9 0.001631 90 Vorname 9 Name 9
36             #
10 0.001773 100 Vorname 10 Name 10
37             #
38             #

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