MuForm/Types.pm

filter: Statementstmt 78.6% 81 / 103 Branchbran 12.5% 1 / 8 Conditioncond 44.4% 4 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 62.5% 30 / 48 Podpod n/a totaltotal 67.8% 118 / 174 SCAR 22.5
CC
8
Cov
72%
__ANON__
12.0
__ANON__
6.0
__ANON__
6.0
CRAP 9.5
« MuForm/Model/Object.pm ↑ Summary MuForm/Merge.pm »
1 package Data::MuForm::Types;
2 # ABSTRACT: Type::Tiny types
3  
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Scalar::Util "looks_like_number";
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Type::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use Types::Standard -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Type::Library -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 our $class_messages = {
14 PositiveNum => "Must be a positive number",
15 PositiveInt => "Must be a positive integer",
16 NegativeNum => "Must be a negative number",
17 NegativeInt => "Must be a negative integer",
18 SingleDigit => "Must be a single digit",
19 SimpleStr => 'Must be a single line of no more than 255 chars',
20 NonEmptySimpleStr => "Must be a non-empty single line of no more than 255 chars",
21 Password => "Must be between 4 and 255 chars",
22 StrongPassword =>"Must be between 8 and 255 chars, and contain a non-alpha char",
23 NonEmptyStr => "Must not be empty",
24 State => "Not a valid state",
25 Email => "Email is not valid",
26 Zip => "Zip is not valid",
27 IPAddress => "Not a valid IP address",
28 NoSpaces =>'Must not contain spaces',
29 WordChars => 'Must be made up of letters, digits, and underscores',
30 NotAllDigits => 'Must not be all digits',
31 Printable => 'Field contains non-printable characters',
32 PrintableAndNewline => 'Field contains non-printable characters',
33 SingleWord => 'Field must contain a single word',
34 };
35  
36  
37 3 ▶declare 'PositiveNum', as Num, where { $_ >= 0 }, message { "Must be a positive number" };
Subroutine __ANON__: called CC=1 SCAR=0.0
38  
39 7 ▶declare 'PositiveInt', as Int, where { $_ >= 0 }, message { "Must be a positive integer" };
Subroutine __ANON__: called CC=1 SCAR=0.0
40  
41 0 ▶declare 'NegativeNum', as Num, where { $_ <= 0 }, message { "Must be a negative number" };
Subroutine __ANON__: not called CC=1 SCAR=2.6
42  
43 0 ▶declare 'NegativeInt', as Int, where { $_ <= 0 }, message { "Must be a negative integer" };
Subroutine __ANON__: not called CC=1 SCAR=2.6
44  
45 0 ▶declare 'SingleDigit', as 'PositiveInt', where { $_ <= 9 }, message { "Must be a single digit" };
Subroutine __ANON__: not called CC=1 SCAR=2.6
46  
47 declare 'SimpleStr',
48 as Str,
49 0 ▶ where { ( length($_) <= 255 ) && ( $_ !~ m/\n/ ) },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
length $_ <= 255 and not $_ =~ /\n/ 0 0 0
50 0 ▶ message { $class_messages->{SimpleStr} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
51  
52 declare 'NonEmptySimpleStr',
53 as 'SimpleStr',
54 0 ▶ where { length($_) > 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 0 ▶ message { $class_messages->{NonEmptySimpleStr} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
56  
57 declare 'Password',
58 as 'NonEmptySimpleStr',
59 0 ▶ where { length($_) >= 4 && length($_) <= 255 },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
length $_ >= 4 and length $_ <= 255 0 0 0
60 0 ▶ message { $class_messages->{Password} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
61  
62 declare 'StrongPassword',
63 as 'Password',
64 0 ▶ where { ( length($_) >= 8 ) && length($_) <= 255 && (m/[^a-zA-Z]/) },
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
length $_ >= 8 and length $_ <= 255 and /[^a-zA-Z]/ 0 0 0
Condition0%0/3
length $_ >= 8 and length $_ <= 255
!ll&&!rl&&r
000
MC/DC0%0/2
length $_ >= 8 and length $_ <= 255
length $_ >= 8 length $_ <= 255
Truth table0%0/3
A and B
A= length $_ >= 8B= length $_ <= 255
ABresult
0X0
100
111
65 0 ▶ message { $class_messages->{StrongPassword} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
66  
67 0 ▶declare 'NonEmptyStr', as Str, where { length($_) > 0 }, message { $class_messages->{NonEmptyStr} };
Subroutine __ANON__: not called CC=1 SCAR=2.6
68  
69 declare 'State', as Str, where {
70 4 ▶ my $value = $_;
Subroutine __ANON__: called CC=1 SCAR=0.0
71 4 my $state = <<EOF;
72 AL AK AZ AR CA CO CT DE FL GA HI ID IL IN IA KS KY LA ME MD
73 MA MI MN MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA PR RI
74 SC SD TN TX UT VT VA WA WV WI WY DC AP FP FPO APO GU VI
75 EOF
76 4 return ( $state =~ /\b($value)\b/i );
77 2 ▶}, message { $class_messages->{State} };
Subroutine __ANON__: called CC=1 SCAR=0.0
78  
79 declare 'Email', as Str, where {
80 2 ▶ my $value = shift;
Subroutine __ANON__: called CC=2 SCAR=7.0
81 2 require Email::Valid;
82 2 my $valid;
83 2 ▶ return ( $valid = Email::Valid->address($value) ) &&
Condition66%2/3
($valid = 'Email::Valid'->address($value)) && $valid eq $value
!ll&&!rl&&r
101
MC/DC50%1/2
($valid = 'Email::Valid'->address($value)) && $valid eq $value
($valid = 'Email::Valid'->address($value)) $valid eq $value
Truth table66%2/3
A && B
A= ($valid = 'Email::Valid'->address($value))B= $valid eq $value
ABresult
0X0
100
111
84 ( $valid eq $value );
85 1 ▶}, message { $class_messages->{Email} };
Subroutine __ANON__: called CC=1 SCAR=0.0
86  
87 declare 'Zip',
88 as Str,
89 0 ▶ where { /^(\s*\d{5}(?:[-]\d{4})?\s*)$/ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
90 0 ▶ message { $class_messages->{Zip} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
91  
92 declare 'IPAddress', as Str, where {
93 9 ▶ /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
Subroutine __ANON__: called CC=1 SCAR=0.0
94 5 ▶}, message { $class_messages->{IPAddress} };
Subroutine __ANON__: called CC=1 SCAR=0.0
95  
96 declare 'NoSpaces',
97 as Str,
98 8 ▶ where { ! /\s/ },
Subroutine __ANON__: called CC=1 SCAR=0.0
99 3 ▶ message { $class_messages->{NoSpaces} };
Subroutine __ANON__: called CC=1 SCAR=0.0
100  
101 declare 'WordChars',
102 as Str,
103 11 ▶ where { ! /\W/ },
Subroutine __ANON__: called CC=1 SCAR=0.0
104 6 ▶ message { $class_messages->{WordChars} };
Subroutine __ANON__: called CC=1 SCAR=0.0
105  
106 declare 'NotAllDigits',
107 as Str,
108 10 ▶ where { ! /^\d+$/ },
Subroutine __ANON__: called CC=1 SCAR=0.0
109 4 ▶ message { $class_messages->{NotAllDigits} };
Subroutine __ANON__: called CC=1 SCAR=0.0
110  
111 declare 'Printable',
112 as Str,
113 0 ▶ where { /^\p{IsPrint}*\z/ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
114 0 ▶ message { $class_messages->{Printable} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
115  
116 declare 'PrintableAndNewline',
117 as Str,
118 0 ▶ where { /^[\p{IsPrint}\n]*\z/ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
119 0 ▶ message { $class_messages->{PrintableAndNewline} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
120  
121 declare 'SingleWord',
122 as Str,
123 8 ▶ where { /^\w*\z/ },
Subroutine __ANON__: called CC=1 SCAR=0.0
124 3 ▶ message { $class_messages->{SingleWord} };
Subroutine __ANON__: called CC=1 SCAR=0.0
125  
126 declare 'Collapse',
127 as Str,
128 3 ▶ where{ ! /\s{2,}/ };
Subroutine __ANON__: called CC=1 SCAR=0.0
129  
130 coerce 'Collapse',
131 from Str,
132 1 ▶ via { s/\s+/ /g; return $_; };
Subroutine __ANON__: called CC=1 SCAR=0.0
133  
134 declare 'Lower',
135 as Str,
136 20 ▶ where { ! /[[:upper:]]/ };
Subroutine __ANON__: called CC=1 SCAR=0.0
137  
138 coerce 'Lower',
139 from Str,
140 6 ▶ via { lc };
Subroutine __ANON__: called CC=1 SCAR=0.0
141  
142 declare 'Upper',
143 as Str,
144 20 ▶ where { ! /[[:lower:]]/ };
Subroutine __ANON__: called CC=1 SCAR=0.0
145  
146 coerce 'Upper',
147 from Str,
148 6 ▶ via { uc };
Subroutine __ANON__: called CC=1 SCAR=0.0
149  
150 declare 'Trim',
151 as Str,
152 3 ▶ where { ! /^\s+/ &&
Subroutine __ANON__: called CC=2 SCAR=7.2
Condition66%2/3
/^\s+/ || /\s+$/
l!l&&r!l&&!r
201
MC/DC50%1/2
/^\s+/ || /\s+$/
/^\s+/ /\s+$/
Truth table66%2/3
A || B
A= /^\s+/B= /\s+$/
ABresult
1X1
011
000
153 3 ! /\s+$/ };
154  
155 coerce 'Trim',
156 from Str,
157 1 ▶ via { s/^\s+// &&
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
s/^\s+// and s/\s+$// 1 0 1
158 s/\s+$//;
159 1 return $_; };
160 3 1;
161  
162 __END__
163  
164 =pod
165  
166 =encoding UTF-8
167  
168 =head1 NAME
169  
170 Data::MuForm::Types - Type::Tiny types
171  
172 =head1 VERSION
173  
174 version 0.04
175  
176 =head1 SYNOPSIS
177  
178 These types are provided by Type::Tiny. These types must not be quoted
179 when they are used:
180  
181 has 'posint' => ( is => 'rw', isa => PositiveInt);
182 has_field 'email' => ( apply => [ Email ] );
183  
184 To import these types into your forms, you must either specify (':all')
185 or list the types you want to use:
186  
187 use Data::MuForm::Types (':all');
188  
189 or:
190  
191 use Data::MuForm::Types ('Email', 'PositiveInt');
192  
193 =head1 DESCRIPTION
194  
195 =head1 Type Constraints
196  
197 These types check the value and issue an error message.
198  
199 =over
200  
201 =item Email
202  
203 Uses Email::Valid
204  
205 =item State
206  
207 Checks that the state is in a list of two uppercase letters.
208  
209 =item Zip
210  
211 =item IPAddress
212  
213 Must be a valid IPv4 address.
214  
215 =item NoSpaces
216  
217 No spaces in string allowed.
218  
219 =item WordChars
220  
221 Must be made up of letters, digits, and underscores.
222  
223 =item NotAllDigits
224  
225 Might be useful for passwords.
226  
227 =item Printable
228  
229 Must not contain non-printable characters.
230  
231 =item SingleWord
232  
233 Contains a single word.
234  
235 =back
236  
237 =head2 Type Coercions
238  
239 These types will transform the value without an error message;
240  
241 =over
242  
243 =item Collapse
244  
245 Replaces multiple spaces with a single space
246  
247 =item Upper
248  
249 Makes the string all upper case
250  
251 =item Lower
252  
253 Makes the string all lower case
254  
255 =item Trim
256  
257 Trims the string of starting and ending spaces
258  
259 =back
260  
261 =head1 AUTHOR
262  
263 Gerda Shank
264  
265 =head1 COPYRIGHT AND LICENSE
266  
267 This software is copyright (c) 2017 by Gerda Shank.
268  
269 This is free software; you can redistribute it and/or modify it under
270 the same terms as the Perl 5 programming language system itself.
271  
272 =cut
« MuForm/Model/Object.pm ↑ Summary MuForm/Merge.pm »