Lazy/Role/Notifications.pm

filter: Statementstmt 89.4% 34 / 38 Branchbran 100% 6 / 6 Conditioncond 100% 13 / 13 MC/DCmcdc 100% 8 / 8 Subroutinesub 88.8% 8 / 9 Podpod 100% 4 / 4 totaltotal 93.5% 73 / 78 CC 9 SCAR 10.4
CC
9
Cov
93%
add_error
4.0
add_warning
4.0
_get_full_code
3.0
CRAP 2.8
« Lazy/Role/Check/Email.pm ↑ Summary Lazy/Role/Check/Required.pm »
1 package Validator::Lazy::Role::Notifications;
2  
3 16 our $VERSION = '0.03';
4  
5 =head1 NAME
6  
7 Validator::Lazy::Role::Notifications
8  
9  
10 =head1 VERSION
11  
12 Version 0.03
13  
14  
15 =head1 SYNOPSIS
16  
17 use Validator::Lazy;
18 my $v = Validator::Lazy->new( $config );
19  
20 my $ok = $v->check( $hashref_of_your_data_to_chech ); # true / false
21 OR
22 my ( $ok, $data ) = $v->check( $hashref_of_your_data_to_chech ); # true / false
23  
24 say Dumper $v->errors; # [ { code => any_error_code, field => field_with_error, data => { variable data for more accurate error definition } } ]
25 say Dumper $v->warnings; # [ { code => any_warn_code, field => field_with_warn, data => { variable data for more accurate warn definition } } ]
26 say Dumper $v->data; # Fixed data. For example trimmed strings, corrected char case, etc...
27  
28 =head1 DESCRIPTION
29  
30 Provides "Notifications" role for Validator::Lazy, part of Validator::Lazy package.
31  
32 Methods:
33 $validator->add_error, that adding error to $validator object.
34 $validator->add_warning, that adding warning to $validator object.
35  
36 Each of these methods can be called with 0,1 or 2 params.
37  
38 If param count is 0 then default error/warn will be generated.
39  
40 By default it is a hash = { field => field_with_error, code => working_check_role_name . '_ERROR', data => {} }
41  
42 If param count is 1:
43 if param is a HASH, then we interpret it as "data" for error/warn hash
44 if param is scalar then it will be error/warn code for error/warn hash
45  
46 If param count is 2, then the first param is error/warn code, the 2nd is "data"
47  
48  
49 =head1 METHODS
50  
51 =head2 C<add_error>
52  
53 $validator->add_error( );
54 $validator->add_error( $code );
55 $validator->add_error( $data );
56 $validator->add_error( $code, $data );
57  
58  
59 =head2 C<add_warning>
60  
61 $validator->add_warning( );
62 $validator->add_warning( $code );
63 $validator->add_warning( $data );
64 $validator->add_warning( $code, $data );
65  
66 =head2 C<errors>
67 $validator->errors; # ArrayRef of errors hashrefs in order of their apearing
68  
69 =head2 C<warnings>
70 $validator->warnings; # ArrayRef of warnings hashrefs in order of their apearing
71  
72 =head2 C<error_codes>
73 $validator->error_codes; # ArrayRef of warning codes in order of their apearing
74  
75 =head2 C<warning_codes>
76 $validator->warning_codes; # ArrayRef of error codes in order of their apearing
77  
78  
79 =head1 SUPPORT AND DOCUMENTATION
80  
81 After installing, you can find documentation for this module with the perldoc command.
82  
83 perldoc Validator::Lazy
84  
85 You can also look for information at:
86  
87 RT, CPAN's request tracker (report bugs here)
88 http://rt.cpan.org/NoAuth/Bugs.html?Dist=Validator-Lazy
89  
90 AnnoCPAN, Annotated CPAN documentation
91 http://annocpan.org/dist/Validator-Lazy
92  
93 CPAN Ratings
94 http://cpanratings.perl.org/d/Validator-Lazy
95  
96 Search CPAN
97 http://search.cpan.org/dist/Validator-Lazy/
98  
99  
100 =head1 AUTHOR
101  
102 ANTONC <antonc@cpan.org>
103  
104 =head1 LICENSE
105  
106 This program is free software; you can redistribute it and/or modify it
107 under the terms of the the Artistic License (2.0). You may obtain a
108 copy of the full license at:
109  
110 L<http://www.perlfoundation.org/artistic_license_2_0>
111  
112 =cut
113  
114 16 ▶use v5.14.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 16 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
116 16 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
117 16 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
118  
119  
120 # [ { name => { error_code => 'REQUIRED', error_data => {} } }, ... ]
121 16 has errors => (
122 is => 'rw',
123 isa => 'ArrayRef[HashRef]',
124 init_arg => undef,
125 traits => ['Array'],
126 handles => {
127 _add_error => 'push',
128 },
129 );
130  
131  
132 # [ { name => { error_code => 'REQUIRED', error_data => {} } }, ... ]
133 16 has warnings => (
134 is => 'rw',
135 isa => 'ArrayRef[HashRef]',
136 init_arg => undef,
137 traits => ['Array'],
138 handles => {
139 _add_warning => 'push',
140 },
141 );
142  
143  
144 99 ▶sub add_error {
Subroutine add_error: called CC=4 SCAR=13.9
Pod documented
145 99 my ( $self, $code, $data ) = @_;
146  
147 99 ▶ $data = $code if $code && ref $code eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $code and ref $code eq "HASH" 14 85 99
Condition100%3/3
$code and ref $code eq "HASH"
!ll&&!rl&&r
493614
MC/DC100%2/2
$code and ref $code eq "HASH"
$code ref $code eq "HASH"
Truth table100%3/3
A and B
A= $codeB= ref $code eq "HASH"
ABresult
0X0
100
111
148 99 ▶ $data //= {};
Condition100%2/2
$data //= {}
l!l
3861
MC/DC100%1/1
$data //= {}
$data
Truth table100%2/2
A
A= $data
Aresult
11
00
149  
150 99 $self->_add_error( {
151 field => $self->get_full_current_field_name,
152 code => _get_full_code( $code, 'ERROR' ),
153 data => $data,
154 } );
155 }
156  
157  
158 8 ▶sub add_warning {
Subroutine add_warning: called CC=4 SCAR=13.9
Pod documented
159 8 my ( $self, $code, $data ) = @_;
160  
161 8 ▶ $data = $code if $code && ref $code eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $code and ref $code eq "HASH" 2 6 8
Condition100%3/3
$code and ref $code eq "HASH"
!ll&&!rl&&r
242
MC/DC100%2/2
$code and ref $code eq "HASH"
$code ref $code eq "HASH"
Truth table100%3/3
A and B
A= $codeB= ref $code eq "HASH"
ABresult
0X0
100
111
162 8 ▶ $data //= {};
Condition100%2/2
$data //= {}
l!l
44
MC/DC100%1/1
$data //= {}
$data
Truth table100%2/2
A
A= $data
Aresult
11
00
163  
164 8 $self->_add_warning( {
165 field => $self->get_full_current_field_name,
166 code => _get_full_code( $code, 'WARNING' ),
167 data => $data,
168 } );
169 }
170  
171  
172 6 ▶sub error_codes {
Subroutine error_codes: called CC=1 SCAR=0.0
Pod documented
173 6 my ( $self ) = @_;
174  
175 6 return [ map { $_->{code} } @{ $self->errors } ];
176 };
177  
178  
179 0 ▶sub warning_codes {
Subroutine warning_codes: not called CC=1 SCAR=6.9
Pod documented
180 0 my ( $self ) = @_;
181  
182 0 return [ map { $_->{code} } @{ $self->warnings } ];
183 };
184  
185 107 ▶sub _get_full_code {
Subroutine _get_full_code: called CC=3 SCAR=11.0
186 107 my ( $code, $type ) = @_;
187  
188 107 ▶ if ( !$code || ref $code ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $code or ref $code) 67 40 107
Condition100%3/3
not $code or ref $code
l!l&&r!l&&!r
511640
MC/DC100%2/2
not $code or ref $code
not $code ref $code
Truth table100%3/3
not(A) or B
A= not $codeB= ref $code
ABresult
1X1
011
000
189 67 my $caller = uc caller(1);
190 67 $caller =~ s/^.+::ROLE::(CHECK::)?//;
191 67 $caller =~ s/\:+/_/g;
192 67 $code = $caller . '_' . $type;
193 };
194  
195 107 return $code;
196 };
197  
198 16 1;
« Lazy/Role/Check/Email.pm ↑ Summary Lazy/Role/Check/Required.pm »