File Coverage

blib/lib/Perl/Critic/Exception/AggregateConfiguration.pm
Criterion Covered Total %
statement 53 54 98.1
branch 9 12 75.0
condition n/a
subroutine 14 14 100.0
pod 6 6 100.0
total 82 86 95.3


line stmt bran cond sub pod time code
1             package Perl::Critic::Exception::AggregateConfiguration;
2              
3 40     40   19828 use 5.010001;
  40         160  
4 40     40   226 use strict;
  40         87  
  40         854  
5 40     40   210 use warnings;
  40         112  
  40         1219  
6              
7 40     40   243 use Carp qw{ confess };
  40         90  
  40         2071  
8 40     40   765 use English qw(-no_match_vars);
  40         3859  
  40         295  
9 40     40   15098 use Readonly;
  40         8136  
  40         1925  
10              
11 40     40   13871 use Perl::Critic::Utils qw{ :characters };
  40         167  
  40         1387  
12              
13             our $VERSION = '1.148';
14              
15             #-----------------------------------------------------------------------------
16              
17             use Exception::Class (
18 40         419 'Perl::Critic::Exception::AggregateConfiguration' => {
19             isa => 'Perl::Critic::Exception',
20             description => 'A collected set of configuration exceptions.',
21             fields => [ qw{ exceptions } ],
22             alias => 'throw_aggregate',
23             },
24 40     40   24249 );
  40         141  
25              
26             #-----------------------------------------------------------------------------
27              
28             Readonly::Array our @EXPORT_OK => qw< throw_aggregate >;
29              
30             #-----------------------------------------------------------------------------
31              
32             sub new {
33 49959     49959 1 121195 my ($class, %options) = @_;
34              
35 49959         95367 my $exceptions = $options{exceptions};
36 49959 50       124771 if (not $exceptions) {
37 49959         117602 $options{exceptions} = [];
38             }
39              
40 49959         193238 return $class->SUPER::new(%options);
41             }
42              
43             #-----------------------------------------------------------------------------
44              
45             sub add_exception {
46 166     166 1 154243 my ( $self, $exception ) = @_;
47              
48 166         315 push @{ $self->exceptions() }, $exception;
  166         3146  
49              
50 166         1185 return;
51             }
52              
53             #-----------------------------------------------------------------------------
54              
55             sub add_exceptions_from {
56 1     1 1 4 my ( $self, $aggregate ) = @_;
57              
58 1         3 push @{ $self->exceptions() }, @{ $aggregate->exceptions() };
  1         25  
  1         27  
59              
60 1         8 return;
61             }
62              
63             #-----------------------------------------------------------------------------
64              
65             sub add_exception_or_rethrow {
66 40790     40790 1 80586 my ( $self, $eval_error ) = @_;
67              
68 40790 100       102085 return if not $eval_error;
69 4 50       51 confess $eval_error if not ref $eval_error;
70              
71 4 100       44 if ( $eval_error->isa('Perl::Critic::Exception::Configuration') ) {
    50          
72 3         15 $self->add_exception($eval_error);
73             }
74             elsif (
75             $eval_error->isa('Perl::Critic::Exception::AggregateConfiguration')
76             ) {
77 1         16 $self->add_exceptions_from($eval_error);
78             }
79             else {
80 0         0 die $eval_error; ## no critic (RequireCarping)
81             }
82              
83 4         13 return;
84             }
85              
86             #-----------------------------------------------------------------------------
87              
88             sub has_exceptions {
89 59365     59365 1 120240 my ( $self ) = @_;
90              
91 59365 100       90766 return @{ $self->exceptions() } ? 1 : 0;
  59365         1172617  
92             }
93              
94             #-----------------------------------------------------------------------------
95              
96             Readonly::Scalar my $MESSAGE_PREFIX => $EMPTY;
97             Readonly::Scalar my $MESSAGE_SUFFIX => "\n";
98             Readonly::Scalar my $MESSAGE_SEPARATOR => $MESSAGE_SUFFIX . $MESSAGE_PREFIX;
99              
100             sub full_message {
101 152     152 1 54733 my ( $self ) = @_;
102              
103 152         376 my $message = $MESSAGE_PREFIX;
104 152         283 $message .= join $MESSAGE_SEPARATOR, @{ $self->exceptions() };
  152         3842  
105 152         178751 $message .= $MESSAGE_SUFFIX;
106              
107 152         451 return $message;
108             }
109              
110             1;
111              
112             #-----------------------------------------------------------------------------
113              
114             __END__
115              
116             =pod
117              
118             =for stopwords
119              
120             =head1 NAME
121              
122             Perl::Critic::Exception::AggregateConfiguration - A collection of a set of problems found in the configuration and/or command-line options.
123              
124             =head1 DESCRIPTION
125              
126             A set of configuration settings can have multiple problems. This is
127             an object for collecting all the problems found so that the user can
128             see them in one run.
129              
130              
131             =head1 INTERFACE SUPPORT
132              
133             This is considered to be a public class. Any changes to its interface
134             will go through a deprecation cycle.
135              
136              
137             =head1 METHODS
138              
139             =over
140              
141             =item C<add_exception( $exception )>
142              
143             Accumulate the parameter with rest of the exceptions.
144              
145              
146             =item C<add_exceptions_from( $aggregate )>
147              
148             Accumulate the exceptions from another instance of this class.
149              
150              
151             =item C<exceptions()>
152              
153             Returns a reference to an array of the collected exceptions.
154              
155              
156             =item C<add_exception_or_rethrow( $eval_error )>
157              
158             If the parameter is an instance of
159             L<Perl::Critic::Exception::Configuration|Perl::Critic::Exception::Configuration>
160             or
161             L<Perl::Critic::Exception::AggregateConfiguration|Perl::Critic::Exception::AggregateConfiguration>,
162             add it. Otherwise, C<die> with the parameter, if it is a reference,
163             or C<confess> with it. If the parameter is false, simply returns.
164              
165              
166             =item C<has_exceptions()>
167              
168             Answer whether any configuration problems have been found.
169              
170              
171             =item C<full_message()>
172              
173             Concatenate the exception messages. See
174             L<Exception::Class/"full_message">.
175              
176              
177             =back
178              
179              
180             =head1 AUTHOR
181              
182             Elliot Shank <perl@galumph.com>
183              
184             =head1 COPYRIGHT
185              
186             Copyright (c) 2007-2011 Elliot Shank.
187              
188             This program is free software; you can redistribute it and/or modify
189             it under the same terms as Perl itself. The full text of this license
190             can be found in the LICENSE file included with this module.
191              
192             =cut
193              
194             ##############################################################################
195             # Local Variables:
196             # mode: cperl
197             # cperl-indent-level: 4
198             # fill-column: 78
199             # indent-tabs-mode: nil
200             # c-indentation-style: bsd
201             # End:
202             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :