DBIx/Class/Valiant/Validator/SetSize.pm

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