Simple/Library/Scalar.pm

filter: Statementstmt 100% 53 / 53 Branchbran 100% 32 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 16 / 16 Podpod n/a totaltotal 100% 101 / 101 CC 20 SCAR 12.2
CC
20
Cov
100%
__ANON__
6.0
__ANON__
5.0
__ANON__
4.0
CRAP 3.4
« Simple/Library/Hash.pm ↑ Summary Simple/Library/OO.pm »
1 =head1 NAME
2  
3 Declare::Constraints::Simple::Library::Scalar - Scalar Constraints
4  
5 =cut
6  
7 package Declare::Constraints::Simple::Library::Scalar;
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 13 ▶use Carp::Clan qw(^Declare::Constraints::Simple);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 SYNOPSIS
16  
17 # match one of a set of regexes
18 my $some_regexes = Matches(qr/foo/, qr/bar/);
19  
20 # allow only defined values
21 my $is_defined = IsDefined;
22  
23 # between 5 and 50 chars
24 my $five_to_fifty = HasLength(5, 50);
25  
26 # match against a set of values
27 my $command_constraint = IsOneOf(qw(create update delete));
28  
29 # check for trueness
30 my $is_true = IsTrue;
31  
32 # simple equality
33 my $is_foo = IsEq('foo');
34  
35 =head1 DESCRIPTION
36  
37 This library contains all constraints to validate scalar values.
38  
39 =head1 CONSTRAINTS
40  
41 =head2 Matches(@regex)
42  
43 my $c = Matches(qr/foo/, qr/bar/);
44  
45 If one of the parameters matches the expression, this is true.
46  
47 =cut
48  
49 constraint 'Matches',
50 24 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
51 24 my @rx = @_;
52 24 ▶ croak 'Matches needs at least one Regexp as argument'
Branch: 1 branch
BranchTrueFalseTotal
unless @rx 1 23 24
53 unless @rx;
54 23 for (@rx) {
55 24 ▶ croak 'Matches only takes Regexps as arguments'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ eq "Regexp" 1 23 24
56 unless ref($_) eq 'Regexp';
57 }
58 43 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
59 43 ▶ return _false('Undefined Value') unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 1 42 43
60 42 for (@rx) {
61 43 ▶ return _true if $_[0] =~ /$_/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /$_/ 35 8 43
62 }
63 7 return _false('Regex does not match');
64 22 };
65 13 };
66  
67 =head2 IsDefined()
68  
69 True if the value is defined.
70  
71 =cut
72  
73 constraint 'IsDefined',
74 8 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
75 20 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
76 20 ▶ return _result((defined($_[0]) ? 1 : 0), 'Undefined Value');
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 15 5 20
77 8 };
78 13 };
79  
80 =head2 HasLength([$min, [$max]])
81  
82 Is true if the value has a length above C<$min> (which defaults to 1> and,
83 if supplied, under the value of C<$max>. A simple
84  
85 my $c = HasLength;
86  
87 checks if the value has a length of at least 1.
88  
89 =cut
90  
91 constraint 'HasLength',
92 15 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
93 15 my ($min, $max) = @_;
94 15 ▶ $min = 1 unless defined $min;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $min 10 5 15
95 15 ▶ $max = 0 unless defined $max;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max 12 3 15
96 9 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
97 9 my ($val) = @_;
98 9 ▶ return _false('Undefined Value') unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 1 8 9
99 8 ▶ return _false('Value too short') unless $min <= length($val);
Branch: 1 branch
BranchTrueFalseTotal
unless $min <= length $val 2 6 8
100 6 ▶ return _true unless $max;
Branch: 1 branch
BranchTrueFalseTotal
unless $max 3 3 6
101 3 ▶ return _result(((length($val) <= $max) ? 1 : 0),
Branch: 1 branch
BranchTrueFalseTotal
length $val <= $max ? : 2 1 3
102 'Value too long');
103 15 };
104 13 };
105  
106 =head2 IsOneOf(@values)
107  
108 True if one of the C<@values> equals the passed value. C<undef> values
109 work with this too, so
110  
111 my $c = IsOneOf(1, 2, undef);
112  
113 will return true on an undefined value.
114  
115 =cut
116  
117 constraint 'IsOneOf',
118 6 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
119 6 my @vals = @_;
120 6 ▶ return sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
121 6 for (@vals) {
122 12 ▶ unless (defined $_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_) 2 10 12
123 2 ▶ return _true unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 1 1 2
124 1 next;
125 }
126 10 ▶ next unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 1 9 10
127 9 ▶ return _true if $_[0] eq $_;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq $_ 2 7 9
128 }
129 3 return _false('No Value matches');
130 6 };
131 13 };
132  
133 =head2 IsTrue()
134  
135 True if the value evulates to true in boolean context.
136  
137 =cut
138  
139 constraint 'IsTrue',
140 16 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
141 16 ▶ return sub { $_[0] ? _true : _false('Value evaluates to False') };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 8 8 16
142 13 };
143  
144 =head2 IsEq($comparator)
145  
146 Valid if the value is C<eq> the C<$comparator>.
147  
148 =cut
149  
150 constraint 'IsEq',
151 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
152 2 my ($compare) = @_;
153 8 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
154 8 return _result(
155 ($compare eq $_[0]),
156 "'$_[0]' does not equal '$compare'"
157 );
158 2 };
159 13 };
160  
161 =head1 SEE ALSO
162  
163 L<Declare::Constraints::Simple>, L<Declare::Constraints::Simple::Library>
164  
165 =head1 AUTHOR
166  
167 Robert 'phaylon' Sedlacek C<E<lt>phaylon@dunkelheit.atE<gt>>
168  
169 =head1 LICENSE AND COPYRIGHT
170  
171 This module is free software, you can redistribute it and/or modify it
172 under the same terms as perl itself.
173  
174 =cut
175  
176 13 1;
« Simple/Library/Hash.pm ↑ Summary Simple/Library/OO.pm »