Lazy/Role/Composer.pm

filter: Statementstmt 100% 52 / 52 Branchbran 89.4% 34 / 38 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 92.4% 98 / 106 CC 22 SCAR 29.4
CC
22
Cov
94%
get_field_roles
22.2
BEGIN
1.0
BEGIN
1.0
CRAP 18.9
« Lazy/Role/Check/IP.pm ↑ Summary Lazy/Role/Check/MinMax.pm »
1 package Validator::Lazy::Role::Composer;
2  
3 16 our $VERSION = '0.03';
4  
5 =head1 NAME
6  
7 Validator::Lazy::Role::Composer
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 "Composer" role for Validator::Lazy, part of Validator::Lazy package.
31  
32 Contains $validator->get_field_roles, that performs compillation of configuration of validator.
33  
34 =head1 METHODS
35  
36 =head2 C<get_field_roles>
37  
38 $validator->get_field_roles( $field, @skip_classes );
39  
40  
41 =head1 SUPPORT AND DOCUMENTATION
42  
43 After installing, you can find documentation for this module with the perldoc command.
44  
45 perldoc Validator::Lazy
46  
47 You can also look for information at:
48  
49 RT, CPAN's request tracker (report bugs here)
50 http://rt.cpan.org/NoAuth/Bugs.html?Dist=Validator-Lazy
51  
52 AnnoCPAN, Annotated CPAN documentation
53 http://annocpan.org/dist/Validator-Lazy
54  
55 CPAN Ratings
56 http://cpanratings.perl.org/d/Validator-Lazy
57  
58 Search CPAN
59 http://search.cpan.org/dist/Validator-Lazy/
60  
61  
62 =head1 AUTHOR
63  
64 ANTONC <antonc@cpan.org>
65  
66 =head1 LICENSE
67  
68 This program is free software; you can redistribute it and/or modify it
69 under the terms of the the Artistic License (2.0). You may obtain a
70 copy of the full license at:
71  
72 L<http://www.perlfoundation.org/artistic_license_2_0>
73  
74 =cut
75  
76 16 ▶use v5.14.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 16 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 16 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 16 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81  
82 =head2 C<get_field_roles>
83  
84 $validator->get_field_roles( $field, @skip_classes );
85  
86 $field - field name for which we should find all classes to validate
87 @skip_classes - classes which is already done and do not need to be affected
88  
89 =cut
90  
91 1375 ▶sub get_field_roles {
Subroutine get_field_roles: called CC=22 SCAR=31.0
Pod documented
92 1375 my ( $self, $field, @skip_classes ) = @_;
93  
94 1375 ▶ confess 'No field name passed!' unless $field && !ref $field;
Branch: 1 branch
BranchTrueFalseTotal
unless $field and not ref $field 0 1375 1375
Condition33%1/3
$field and not ref $field
!ll&&!rl&&r
001375
MC/DC0%0/2
$field and not ref $field
$field not ref $field
Truth table33%1/3
A and not(B)
A= $fieldB= not ref $field
ABresult
0X0
100
111
95 1375 ▶ confess 'Wrong field name passed!' unless $field =~ /^.+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $field =~ /^.+$/u 0 1375 1375
96  
97 1375 my @classes;
98  
99 # Just seek for classes in config
100 1375 ▶ while( my( $match, $class ) = each %{ $self->config } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->config;}) 15731 1375 17106
101  
102 15731 chomp $match;
103  
104 # match can be a regular expression
105 15731 ▶ if ( $match =~ /^\/(.+)\/$/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($match =~ m[^/(.+)/$]u) { } 6336 9395 15731
elsif ($match =~ /^\[(.+)\]$/u) { } 1006 8389 9395
elsif ($field eq $match) { } 546 7843 8389
106 6336 my $regexp = $1;
107 # if field matches with the regexp - all classes from this key will be added
108 6336 ▶ push @classes, $class if $field =~ /$regexp/;
Branch: 1 branch
BranchTrueFalseTotal
if $field =~ /$regexp/u 231 6105 6336
109 }
110 # also, match can be a list [value1|value2]
111 elsif ( $match =~ /^\[(.+)\]$/ ) {
112 1006 my @key_list = split /[\|,]/, $1;
113  
114 # list contains field name
115 1006 ▶ push @classes, $class if grep { $_ eq $field } @key_list;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $field;} @key_list 42 964 1006
116 }
117 # direct match
118 elsif ( $field eq $match ) {
119 546 push @classes, $class;
120 }
121 };
122  
123 1375 ▶ @classes = ( $field ) unless @classes;
Branch: 1 branch
BranchTrueFalseTotal
unless @classes 587 788 1375
124  
125 1375 ▶ @classes = map { ref $_ eq 'ARRAY' ? @$_ : $_ } @classes;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 738 668 1406
126  
127 # Finding subrelations
128 1375 my $changed = 1;
129 1375 my $deepness = 30;
130  
131 1375 ▶ while ( $changed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed) 1714 1375 3089
132  
133 1714 ▶ confess 'Too deep config relations' unless --$deepness;
Branch: 1 branch
BranchTrueFalseTotal
unless --$deepness 0 1714 1714
134  
135 1714 $changed = 0;
136  
137 @classes = map {
138 1714 my $class = $_;
139  
140 2634 ▶ if ( grep { $_ eq $class } @skip_classes ) {
Branch: 3 branches
BranchTrueFalseTotal
if (grep {$_ eq $class;} @skip_classes) { } 550 2084 2634
elsif (ref $class or $class =~ /\:/u) { } 1196 888 2084
elsif (my(@new_classes) = $self->get_field_roles($class, (@skip_classes, $class))) { } 339 549 888
141 550 ();
142 }
143 ▶ elsif ( ref $class || $class =~ /\:/ ) {
Condition100%3/3
ref $class or $class =~ /\:/u
l!l&&r!l&&!r
118313888
MC/DC100%2/2
ref $class or $class =~ /\:/u
ref $class $class =~ /\:/u
Truth table100%3/3
A or B
A= ref $classB= $class =~ /\:/u
ABresult
1X1
011
000
144 1196 $class;
145 }
146 elsif ( my @new_classes = $self->get_field_roles( $class, ( @skip_classes, $class ) ) ) {
147 339 $changed = 1;
148 339 @new_classes;
149 }
150 else {
151 549 $class;
152 };
153 } @classes;
154 }
155  
156 # replacing class names to packages
157 @classes = map {
158 1375 ▶ my %class =
Branch: 2 branches
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 987 420 1407
!ref($_) ? : 420 0 420
159 ref $_ eq 'HASH' ? %$_ :
160 ! ref $_ ? ( $_ => {} ) :
161 confess 'Class should be scalar of arrayref';
162  
163 1407 my @result_class;
164  
165 1407 ▶ while ( my( $class, $params ) = each %class ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %class) 1407 1407 2814
166 1407 ▶ $class = 'Validator::Lazy::Role::Check::' . $class unless $class =~ /\:/;
Branch: 1 branch
BranchTrueFalseTotal
unless $class =~ /\:/u 911 496 1407
167 1407 push @result_class, { $class => $params };
168 };
169  
170 1407 @result_class;
171  
172 } @classes;
173  
174 1375 return @classes;
175 }
176  
177 16 1;
« Lazy/Role/Check/IP.pm ↑ Summary Lazy/Role/Check/MinMax.pm »