Lazy/Role/Check.pm

filter: Statementstmt 97.9% 47 / 48 Branchbran 90.0% 9 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 96.9% 64 / 66 CC 10 SCAR 18.9
CC
10
Cov
97%
check
10.0
BEGIN
1.0
BEGIN
1.0
CRAP 6.6
« Lazy/Role/Check/Form.pm ↑ Summary Lazy.pm »
1 package Validator::Lazy::Role::Check;
2  
3 16 our $VERSION = '0.03';
4  
5 =head1 NAME
6  
7 Validator::Lazy::Role::Check
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 "Check" role for Validator::Lazy, part of Validator::Lazy package.
31  
32 Contains checking loop during $validator->check call.
33  
34 Each check iteration will try to execute for each field-class of each field in your_data_hashref sequence of:
35 Your::Role::before
36 Your::Role::check
37 Your::Role::after
38  
39 Your::Role::before - should be used for precheck, or modification
40  
41 of form param value. For example you can do trimlr here
42  
43  
44 Your::Role::check - main functional of called role.
45  
46 The form value shoild be checked here
47  
48 and result should be mirrired im validator object
49  
50  
51 Your::Role::after - After the value is checked,
52  
53 Role may want to prepare value for something.
54  
55 For example the role can convert data into internal format,
56  
57 store checked file to disc or something like that.
58  
59  
60 =head1 METHODS
61  
62 =head2 C<check>
63  
64 $validator->check( $your_data_hashref );
65  
66  
67 =head1 SUPPORT AND DOCUMENTATION
68  
69 After installing, you can find documentation for this module with the perldoc command.
70  
71 perldoc Validator::Lazy
72  
73 You can also look for information at:
74  
75 RT, CPAN's request tracker (report bugs here)
76 http://rt.cpan.org/NoAuth/Bugs.html?Dist=Validator-Lazy
77  
78 AnnoCPAN, Annotated CPAN documentation
79 http://annocpan.org/dist/Validator-Lazy
80  
81 CPAN Ratings
82 http://cpanratings.perl.org/d/Validator-Lazy
83  
84 Search CPAN
85 http://search.cpan.org/dist/Validator-Lazy/
86  
87  
88 =head1 AUTHOR
89  
90 ANTONC <antonc@cpan.org>
91  
92 =head1 LICENSE
93  
94 This program is free software; you can redistribute it and/or modify it
95 under the terms of the the Artistic License (2.0). You may obtain a
96 copy of the full license at:
97  
98 L<http://www.perlfoundation.org/artistic_license_2_0>
99  
100 =cut
101  
102 16 ▶use v5.14.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
103 16 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
104 16 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
105 16 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
106 16 ▶use Moose::Util qw/ ensure_all_roles apply_all_roles /;
Subroutine BEGIN: called CC=1 SCAR=0.0
107  
108  
109 =head2 C<check>
110  
111 $validator->check( $your_data_hashref );
112  
113 =cut
114  
115 152 ▶sub check {
Subroutine check: called CC=10 SCAR=23.0
Pod documented
116 152 my ( $self, %data ) = @_;
117  
118 152 $self->data( {} );
119 152 $self->errors( [] );
120 152 $self->warnings( [] );
121  
122 # Each key-value of data is independent "form"
123 152 for my $field ( sort keys %data ) {
124  
125 475 $self->current_field( $field );
126 475 my $value = $data{ $field };
127  
128 475 my @roles = $self->get_field_roles( $field );
129  
130 475 for ( @roles ) {
131 910 ( my $role, undef ) = %$_; # just a key
132  
133 910 ensure_all_roles(
134 $self,
135 $role,
136 {
137 -alias => {
138 before_check => _get_role_method_name( $role, 'before' ),
139 check => _get_role_method_name( $role, 'check' ),
140 after_check => _get_role_method_name( $role, 'after' ),
141 }
142 }
143 );
144 };
145  
146 # Overwrite default(last role in this case) check method to common check dispatcheer role
147 475 apply_all_roles( $self, 'Validator::Lazy::Role::Check' );
148  
149 475 for ( @roles ) {
150 852 my ( $role, $param ) = %$_;
151  
152 852 for my $method ( qw/ before check after / ) {
153 2556 ▶ my $check = $self->can( _get_role_method_name( $role, $method ) ) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $check = $self->can(_get_role_method_name($role, $method)) or next 1608 948 2556
154  
155 eval{
156 1608 $value = $check->( $self, $value, $param );
157 1608 1;
158 }
159 1608 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $value = &$check($self, $value, $param); 1 } } or do { $self->add_error("CHECK_DIED", {"field", $field, "data", {"value", $value, "role", $role, "method", $method}}) } 1608 0 1608
160 0 $self->add_error(
161 'CHECK_DIED',
162 {
163 field => $field,
164 data => {
165 value => $value,
166 role => $role,
167 method => $method,
168 },
169 }
170 );
171 };
172 }
173  
174 852 ▶ my @errors = @{ $self->errors } or next;
Branch: 1 branch
BranchTrueFalseTotal
my(@errors) = @{$self->errors;} or next 286 566 852
175 286 ▶ last if $errors[-1]{field} eq $self->get_full_current_field_name;
Branch: 1 branch
BranchTrueFalseTotal
if $errors[-1]{"field"} eq $self->get_full_current_field_name 94 192 286
176 }
177  
178 475 $data{ $field } = $value;
179 }
180  
181 152 $self->data( \%data );
182  
183 152 my $ok = ! scalar @{ $self->errors };
184  
185 152 ▶ return wantarray ? ( $ok, \%data ) : ( $ok );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 19 133 152
186 }
187  
188  
189 5286 ▶sub _get_role_method_name {
Subroutine _get_role_method_name: called CC=1 SCAR=0.0
190 5286 my ( $role, $method ) = @_;
191  
192 5286 my $method_prefix = $role;
193 5286 $method_prefix =~ s/\:+/-/ig;
194  
195 5286 return $method_prefix . '_' . $method;
196 };
197  
198 16 1;
« Lazy/Role/Check/Form.pm ↑ Summary Lazy.pm »