Simple/Library/Array.pm

filter: Statementstmt 100% 66 / 66 Branchbran 97.3% 37 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod n/a totaltotal 99.1% 115 / 116 CC 22 SCAR 14.5
CC
22
Cov
99%
__ANON__
6.0
__ANON__
6.0
__ANON__
5.0
CRAP 4.3
« Simple/Library/Referencial.pm ↑ Summary Simple/Library/Hash.pm »
1 =head1 NAME
2  
3 Declare::Constraints::Simple::Library::Array - Array Constraints
4  
5 =cut
6  
7 package Declare::Constraints::Simple::Library::Array;
8 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 13 ▶use Declare::Constraints::Simple-Library;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use Carp::Clan qw(^Declare::Constraints::Simple);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 SYNOPSIS
15  
16 # accept a list of pairs
17 my $pairs_validation = IsArrayRef( HasArraySize(2,2) );
18  
19 # integer => object pairs
20 my $pairs = And( OnEvenElements(IsInt),
21 OnOddElements(IsObject) );
22  
23 # a three element array
24 my $tri = And( HasArraySize(3,3),
25 OnArrayElements(0, IsInt,
26 1, IsDefined,
27 2, IsClass) );
28  
29 =head1 DESCRIPTION
30  
31 This module contains all constraints that can be applied to array
32 references.
33  
34 =head1 CONSTRAINTS
35  
36 =head2 HasArraySize([$min, [$max]])
37  
38 With C<$min> defaulting to 1. So a specification of
39  
40 my $profile = HasArraySize;
41  
42 checks for at least one value. To force an exact size of the array,
43 specify the same values for both:
44  
45 my $profile = HasArraySize(3, 3);
46  
47 =cut
48  
49 constraint 'HasArraySize',
50 12 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
51 12 my ($min, $max) = @_;
52 12 ▶ $min = 1 unless defined $min;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $min 5 7 12
53 13 ▶ return sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
54 13 ▶ return _false('Undefined Value') unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 1 12 13
55 12 ▶ return _false('Not an ArrayRef')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" 1 11 12
56 unless ref($_[0]) eq 'ARRAY';
57 return _false("Less than $min Array elements")
58 11 ▶ unless scalar(@{$_[0]}) >= $min;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$_[0];} >= $min 3 8 11
59 8 ▶ return _true
Branch: 1 branch
BranchTrueFalseTotal
unless $max 4 4 8
60 unless $max;
61 return _false("More than $max Array elements")
62 4 ▶ unless scalar(@{$_[0]}) <= $max;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$_[0];} <= $max 1 3 4
63 3 return _true;
64 12 };
65 13 };
66  
67 =head2 OnArrayElements($key => $constraint, $key => $constraint, ...)
68  
69 Applies the the C<$constraint>s to the corresponding C<$key>s if they are
70 present. For required keys see C<HasArraySize>.
71  
72 =cut
73  
74 constraint 'OnArrayElements',
75 7 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
76 7 my %keymap = @_;
77 7 my @keys = sort keys %keymap;
78 7 for (@keys) {
79 14 ▶ croak "Not an array index: $_" if $_ =~ /\D/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /\D/ 0 14 14
80 }
81
82 7 ▶ return sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
83 7 ▶ return _false('Undefined Value') unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 1 6 7
84 6 ▶ return _false('Not an ArrayRef')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" 1 5 6
85 unless ref($_[0]) eq 'ARRAY';
86 5 for my $k (@keys) {
87 7 ▶ last if $k > $#{$_[0]};
Branch: 1 branch
BranchTrueFalseTotal
if $k > $#{$_[0];} 2 5 7
88 5 my $r = $keymap{$k}->($_[0][$k]);
89 5 _info($k);
90 5 ▶ return $r unless $r->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $r->is_valid 2 3 5
91 }
92 3 return _true;
93 }
94 7 };
95  
96 =head2 OnEvenElements($constraint)
97  
98 Runs the constraint on all even elements of an array. See also
99 C<OnOddElements>.
100  
101 =cut
102  
103 constraint 'OnEvenElements',
104 9 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
105 9 my ($c) = @_;
106  
107 9 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
108 9 ▶ return _false('Undefined Value') unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 1 8 9
109 8 ▶ return _false('Not an ArrayRef')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" 1 7 8
110 unless ref($_[0]) eq 'ARRAY';
111 7 my $p = 0;
112 7 ▶ while ($p <= $#{$_[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p <= $#{$_[0];}) 8 5 13
113 8 my $r = $c->($_[0][$p]);
114 8 _info($p);
115 8 ▶ return $r unless $r->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $r->is_valid 2 6 8
116 6 $p += 2;
117 }
118 5 return _true;
119 9 };
120 13 };
121  
122  
123 =head2 OnOddElements($constraint)
124  
125 Runs the constraint on all odd elements of an array. See also
126 C<OnEvenElements>.
127  
128 =cut
129  
130 constraint 'OnOddElements',
131 8 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
132 8 my ($c) = @_;
133  
134 7 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
135 7 ▶ return _false('Undefined Value') unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 1 6 7
136 6 ▶ return _false('Not an ArrayRef')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" 1 5 6
137 unless ref($_[0]) eq 'ARRAY';
138 5 my $p = 1;
139 5 ▶ while ($p <= $#{$_[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p <= $#{$_[0];}) 4 4 8
140 4 my $r = $c->($_[0][$p]);
141 4 _info($p);
142 4 ▶ return $r unless $r->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $r->is_valid 1 3 4
143 3 $p += 2;
144 }
145 4 return _true;
146 8 };
147 13 };
148  
149 =head1 SEE ALSO
150  
151 L<Declare::Constraints::Simple>, L<Declare::Constraints::Simple::Library>
152  
153 =head1 AUTHOR
154  
155 Robert 'phaylon' Sedlacek C<E<lt>phaylon@dunkelheit.atE<gt>>
156  
157 =head1 LICENSE AND COPYRIGHT
158  
159 This module is free software, you can redistribute it and/or modify it
160 under the same terms as perl itself.
161  
162 =cut
163  
164 13 1;
« Simple/Library/Referencial.pm ↑ Summary Simple/Library/Hash.pm »