Valiant/Validator/SetSize.pm

filter: Statementstmt 91.6% 33 / 36 Branchbran 66.6% 8 / 12 Conditioncond 56.2% 9 / 16 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 88.8% 8 / 9 Podpod n/a totaltotal 72.2% 60 / 83 CC 13 SCAR 21.1
CC
13
Cov
78%
normalize_shortcut
12.0
validate_each
11.9
BEGIN
1.0
CRAP 8.3
« Valiant/ResultSet.pm ↑ Summary Valiant/Validator/Result.pm »
1 package DBIx::Class::Valiant::Validator::SetSize;
2  
3 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use Valiant::I18N;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 with 'Valiant::Validator::Each';
8  
9 7 has min => (is=>'ro', required=>0, predicate=>'has_min');
10 7 has max => (is=>'ro', required=>0, predicate=>'has_max');
11 24 ▶has skip_if_empty => (is=>'ro', required=>1, default=>sub {0});
Subroutine __ANON__: called CC=1 SCAR=0.0
12 25 ▶has skip_if_blank => (is=>'ro', required=>1, default=>sub {0});
Subroutine __ANON__: called CC=1 SCAR=0.0
13 25 ▶has too_few_msg => (is=>'ro', required=>1, default=>sub {_t 'too_few'});
Subroutine __ANON__: called CC=1 SCAR=0.0
14 25 ▶has too_many_msg => (is=>'ro', required=>1, default=>sub {_t 'too_many'});
Subroutine __ANON__: called CC=1 SCAR=0.0
15  
16 0 ▶sub normalize_shortcut {
Subroutine normalize_shortcut: not called CC=3 SCAR=24.8
17 0 my ($class, $arg) = @_;
18 0 ▶ if( (ref($arg)||'') eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $arg || "") eq "ARRAY") 0 0 0
Condition0%0/2
ref $arg || ''
l!l
00
MC/DC0%0/1
ref $arg || ''
ref $arg
Truth table0%0/2
A
A= ref $arg
Aresult
11
00
19 return +{
20 0 min => $arg->[0],
21 max => $arg->[1],
22 }
23 }
24 }
25  
26 32 ▶sub validate_each {
Subroutine validate_each: called CC=11 SCAR=24.8
27 32 my ($self, $record, $attribute, $value, $opts) = @_;
28  
29 32 ▶ return if($self->skip_if_blank && !exists $value->{all_cache});
Branch: 1 branch
BranchTrueFalseTotal
if $self->skip_if_blank and not exists $value->{"all_cache"} 0 32 32
Condition33%1/3
$self->skip_if_blank and not exists $value->{"all_cache"}
!ll&&!rl&&r
3200
MC/DC0%0/2
$self->skip_if_blank and not exists $value->{"all_cache"}
$self->skip_if_blank not exists $value->{"all_cache"}
Truth table33%1/3
A and not(B)
A= $self->skip_if_blankB= not exists $value->{"all_cache"}
ABresult
0X0
100
111
30  
31 # If a row is marked to be deleted then don't bother to validate it
32 32 ▶ my @rows = grep { not $_->is_removed } @{$value->get_cache||[]};
Condition100%2/2
$value->get_cache || []
l!l
1715
MC/DC100%1/1
$value->get_cache || []
$value->get_cache
Truth table100%2/2
A
A= $value->get_cache
Aresult
11
00
33 32 ▶ if(!@rows) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@rows) 19 13 32
34 19 ▶ return if $self->skip_if_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->skip_if_empty 15 4 19
35 }
36
37 # This bit here where we exclude rows with errors from the count might not be right. It's likely
38 # that if someone submits enough rows that we don't want to see this error just because one or more
39 # has errors.
40 #
41  
42 17 ▶ my $count = scalar(grep { !$_->{__valiant_donot_insert} } grep { $_->in_storage || !$_->errors->size } @rows);
Condition66%2/3
$_->in_storage || !$_->errors->size
l!l&&r!l&&!r
1270
MC/DC0%0/2
$_->in_storage || !$_->errors->size
$_->in_storage !$_->errors->size
Truth table66%2/3
A || not(B)
A= $_->in_storageB= !$_->errors->size
ABresult
1X1
011
000
43 #my $count = scalar(grep { !$_->errors->size } @rows);
44 #my $count = scalar(@rows);
45
46 17 ▶ $record->errors->add($attribute, $self->too_few_msg, +{%$opts, count=>$count, min=>$self->min})
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_min and $count < $self->min 6 11 17
Condition66%2/3
$self->has_min and $count < $self->min
!ll&&!rl&&r
0116
MC/DC50%1/2
$self->has_min and $count < $self->min
$self->has_min $count < $self->min
Truth table66%2/3
A and B
A= $self->has_minB= $count < $self->min
ABresult
0X0
100
111
47 if $self->has_min and $count < $self->min;
48  
49 17 ▶ $record->errors->add($attribute, $self->too_many_msg, +{%$opts, count=>$count, max=>$self->max})
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_max and $count > $self->max 0 17 17
Condition66%2/3
$self->has_max and $count > $self->max
!ll&&!rl&&r
890
MC/DC0%0/2
$self->has_max and $count > $self->max
$self->has_max $count > $self->max
Truth table66%2/3
A and B
A= $self->has_maxB= $count > $self->max
ABresult
0X0
100
111
50 if $self->has_max and $count > $self->max;
51 }
52  
53 7 1;
54  
55 =head1 NAME
56  
57 DBIx::Class::Valiant::Validator::SetSize - Verify a DBIC related resultset
58  
59 =head1 SYNOPSIS
60  
61 package Example::Schema::Result::Person;
62  
63 use base 'Example::Schema::Result';
64  
65 __PACKAGE__->load_components(qw/
66 Valiant::Result
67 Core
68 /);
69  
70 __PACKAGE__->table("person");
71  
72 __PACKAGE__->add_columns(
73 id => { data_type => 'bigint', is_nullable => 0, is_auto_increment => 1 },
74 username => { data_type => 'varchar', is_nullable => 0, size => 48 },
75 first_name => { data_type => 'varchar', is_nullable => 0, size => 24 },
76 last_name => { data_type => 'varchar', is_nullable => 0, size => 48 },
77 password => {
78 data_type => 'varchar',
79 is_nullable => 0,
80 size => 64,
81 },
82 );
83  
84 __PACKAGE__->has_many(
85 credit_cards =>
86 'Example::Schema::Result::CreditCard',
87 { 'foreign.person_id' => 'self.id' }
88 );
89  
90 __PACKAGE__->validates(
91 credit_cards => (
92 set_size=>+{ skip_if_empty=>1, min=>2, max=>4 },
93 )
94 );
95  
96 =head1 DESCRIPTION
97  
98 Validations on related resultsets. This constrains minimum / maximum sizes on the set and
99 permits optional sets (where the min/max is only applied when the set has entries).
100  
101 =head1 ATTRIBUTES
102  
103 This validator supports the following attributes:
104  
105 =head2 skip_if_empty
106  
107 Skip validations if the resultset is empty. In this context empty means that you have zero records.
108 Probably not that useful; yhou might actually want 'skip_if_blank' instead.
109  
110 =head2 skip_if_blank
111  
112 Allows you to skip validations if the resultset is blank. In this context blank means that you
113 have not prefetched the relationship or loaded it in any way. 'blank' is different from 'has none'.
114 You might for example want to skip size validations if you have not prefetched the relationship.
115  
116 =head2 min
117  
118 =head2 max
119  
120 The minimum or maximum number of rows that the resultset can contain. Optional (but I suspect you'd
121 set at least one otherwise why bother with this constraint?
122  
123 =head2 too_few_msg
124  
125 =head2 too_many_msg
126  
127 Error messages associated with the 'min' or 'max' constraints. Defaults to 'too_few' or 'too_many'
128 translation tags.
129  
130 =head1 SHORTCUT FORM
131  
132 This validator supports the follow shortcut forms:
133  
134 validates attribute => ( set_size => [1,4], ... );
135  
136 Which is the same as:
137  
138 validates attribute => (
139 set_size => {
140 min => 1,
141 max => 4,
142 }
143 );
144  
145 =head1 GLOBAL PARAMETERS
146  
147 This validator supports all the standard shared parameters: C<if>, C<unless>,
148 C<message>, C<strict>, C<allow_undef>, C<allow_blank>.
149  
150 =head1 SEE ALSO
151
152 L<Valiant>, L<Valiant::Validator>, L<Valiant::Validator::Each>.
153  
154 =head1 AUTHOR
155
156 See L<Valiant>
157
158 =head1 COPYRIGHT & LICENSE
159
160 See L<Valiant>
161  
162 =cut
« Valiant/ResultSet.pm ↑ Summary Valiant/Validator/Result.pm »