File Coverage

blib/lib/Report/Generator.pm
Criterion Covered Total %
statement 42 49 85.7
branch 7 16 43.7
condition 1 8 12.5
subroutine 9 9 100.0
pod 2 2 100.0
total 61 84 72.6


line stmt bran cond sub pod time code
1             package Report::Generator;
2              
3 2     2   171410 use warnings;
  2         4  
  2         58  
4 2     2   10 use strict;
  2         4  
  2         63  
5              
6 2     2   10 use Carp qw(croak);
  2         6  
  2         103  
7 2     2   10 use Config;
  2         2  
  2         67  
8 2     2   1475 use Config::Any;
  2         21431  
  2         71  
9 2     2   891 use Params::Util qw(_HASH);
  2         5559  
  2         956  
10              
11             =head1 NAME
12              
13             Report::Generator - utility to generate reports
14              
15             =cut
16              
17             our $VERSION = '0.002';
18              
19             =head1 SYNOPSIS
20              
21             Report::Generator->new( { cfg => '/path/to/config/file' } )->genreport();
22              
23             =head1 DESCRIPTION
24              
25             C provides an infrastructure to generate reports using
26             render classes based on a configuration file. The render classes require
27             usually additional configuration - you should consult their documentation
28             when you're going to use them.
29              
30             =head1 SUBROUTINES/METHODS
31              
32             =head2 new
33              
34             Instantiates a new Report::Generator. Requires an hash reference
35             containing bootstrap parameters:
36              
37             =over 4
38              
39             =item C
40              
41             Must be either a path to a configuration file, which can be read using
42             L or a hash with a compatible configuration.
43              
44             =back
45              
46             =cut
47              
48             sub new
49             {
50 1     1 1 902525 my ( $proto, $attr ) = @_;
51              
52 1 50       7 if ( ref $proto )
53             {
54 0   0     0 $attr ||= {};
55 0   0     0 $attr->{cfg} ||= $proto->{cfg};
56 0         0 $proto = ref($proto);
57             }
58              
59 1 50 33     17 defined( _HASH($attr) ) and defined( $attr->{cfg} )
60             or croak("$proto->new({cfg => 'path/to/config'})");
61              
62 1         4 my $self = bless( $attr, $proto );
63              
64 1         4 return $self;
65             }
66              
67             sub _loadconfig
68             {
69 1     1   2 my ($self) = @_;
70              
71 1 50       35 if ( -f $self->{cfg} )
72             {
73 0         0 my $cfg = Config::Any->load_files(
74             {
75             files => [ $self->{cfg} ],
76             use_ext => 1,
77             flatten_to_hash => 1,
78             }
79             );
80 0         0 $self->{cfg} = $cfg->{ $self->{cfg} };
81             }
82              
83 1 50       8 defined( _HASH( $self->{cfg} ) ) or croak("Invalid configuration");
84             }
85              
86             =head2 generate
87              
88             Generates the report based on the configuration.
89              
90             =cut
91              
92             sub generate
93             {
94 1     1 1 8 my ($self) = @_;
95 1         6 $self->_loadconfig();
96 1         3 my $renderer = $self->{cfg}->{renderer};
97 1 50       10 unless ( $renderer->isa('Report::Generator::Render') )
98             {
99 1         2 my $fn = $renderer;
100 1         6 $fn =~ s|::|/|g;
101 1         3 $fn .= ".pm";
102 1         3 local $@ = undef;
103 1         3 eval { require $fn; };
  1         795  
104 1 50       9 $@ and croak "Can't load '$renderer': $@";
105             }
106              
107 1         12 $self->{renderer} = $renderer->new( $self->{cfg}->{$renderer} );
108 1         6 $self->{rendered} = $self->{renderer}->render();
109              
110 1 50       196 if( $self->{rendered} )
111             {
112             # run post-gen actions
113 0 0       0 if ( $self->{cfg}->{post_processing} )
114             {
115 0         0 system( $self->{cfg}->{post_processing} );
116             }
117             }
118             else
119             {
120 1         5 $self->{error} = $self->{renderer}->{error};
121             }
122              
123 1         9 return $self->{rendered};
124             }
125              
126             =head1 AUTHOR
127              
128             Jens Rehsack, C<< >>
129              
130             =head1 BUGS
131              
132             Please report any bugs or feature requests to
133             C, or through the web interface at
134             L. I
135             will be notified, and then you'll automatically be notified of progress
136             on your bug as I make changes.
137              
138             =head1 SUPPORT
139              
140             You can find documentation for this module with the perldoc command.
141              
142             perldoc Report::Generator
143              
144             You can also look for information at:
145              
146             =over 4
147              
148             =item * RT: CPAN's request tracker
149              
150             L
151              
152             =item * AnnoCPAN: Annotated CPAN documentation
153              
154             L
155              
156             =item * CPAN Ratings
157              
158             L
159              
160             =item * Search CPAN
161              
162             L
163              
164             =back
165              
166             =head1 ACKNOWLEDGEMENTS
167              
168              
169             =head1 LICENSE AND COPYRIGHT
170              
171             Copyright 2010 Jens Rehsack.
172              
173             This program is free software; you can redistribute it and/or modify it
174             under the terms of either: the GNU General Public License as published
175             by the Free Software Foundation; or the Artistic License.
176              
177             See http://dev.perl.org/licenses/ for more information.
178              
179              
180             =cut
181              
182             1; # End of Report::Generator