File Coverage

lib/Data/Processor/Error/Collection.pm
Criterion Covered Total %
statement 50 50 100.0
branch 3 4 75.0
condition n/a
subroutine 14 14 100.0
pod 7 8 87.5
total 74 76 97.3


line stmt bran cond sub pod time code
1 19     19   247 use 5.10.1;
  19         59  
2 19     19   94 use strict;
  19         28  
  19         370  
3 19     19   80 use warnings;
  19         30  
  19         670  
4             package Data::Processor::Error::Collection;
5 19     19   112 use Carp;
  19         62  
  19         1232  
6 19     19   6558 use Data::Processor::Error::Instance;
  19         47  
  19         743  
7              
8 19     19   121 use overload ('""' => \&to_string);
  19         33  
  19         92  
9              
10             =head1 NAME
11             Data::Processor::Error::Collection - Collect errors for Data::Processor
12              
13             =head1 METHODS
14             =head2 new
15              
16             my $errors = Data::Processor::Error::Collection->new();
17              
18             =cut
19             sub new {
20 319     319 1 467 my $class = shift;
21 319         698 my $self = {
22             errors => [] # the error instances are going into here
23             };
24 319         525 bless ($self, $class);
25 319         1262 return $self;
26             }
27              
28             =head2 add
29             Adds an error.
30             parameters:
31             - message
32             - path
33             =cut
34             sub add {
35 39     39 1 71 my $self = shift;
36 39         126 my %p = @_;
37 39         229 my $error = Data::Processor::Error::Instance->new(%p);
38 39         72 push @{$self->{errors}}, $error;
  39         193  
39             }
40              
41             =head2 add_error
42             Adds an error object
43             =cut
44             sub add_error {
45 12     12 1 20 my $self = shift;
46 12         32 my $e = shift;
47 12         22 push @{$self->{errors}}, $e;
  12         48  
48             }
49              
50             =head2 add_collection
51             Adds another error collection
52             =cut
53             sub add_collection{
54 96     96 1 151 my $self = shift;
55 96         118 my $other = shift;
56 96         191 my @e = $other->as_array();
57 96         325 for (@e){
58 12         42 $self->add_error($_);
59             }
60              
61             }
62              
63             =head2 any_error_contains
64             Return true if any of the collected errors contains a given string.
65             $error->collection->any_error_contains(
66             string => "error_msg",
67             field => "message", # any of the data fields of an error
68             );
69             =cut
70             sub any_error_contains {
71 8     8 1 20 my $self = shift;
72 8         39 my %p = @_;
73 8         21 for ('string', 'field'){
74             croak "cannot check for errors without '$_'"
75 16 50       44 unless $p{$_};
76             }
77 8         15 for my $error (@{$self->{errors}}){
  8         20  
78 23 100       289 return 1 if $error->{$p{field}} =~ /$p{string}/;
79             }
80             }
81              
82             =head2 as_array
83             Return all collected errors as an array.
84             =cut
85             sub as_array {
86 308     308 1 397 my $self = shift;
87 308         372 return @{$self->{errors}};
  308         2011  
88             }
89              
90             =head2 count
91             Return count of errors.
92             =cut
93             sub count {
94 71     71 1 525 my $self = shift;
95 71         96 return scalar @{$self->{errors}};
  71         314  
96             }
97              
98             sub to_string {
99 200     200 0 553 my $self = shift;
100 200         512 return join "\n", $self->as_array;
101             }
102             1;
103