Brick/Numbers.pm

filter: Statementstmt 83.6% 51 / 61 Branchbran 18.7% 3 / 16 Conditioncond 29.1% 7 / 24 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 73.6% 14 / 19 Podpod 0.0% 0 / 1 totaltotal 54.7% 75 / 137 CC 17 SCAR 13.0
CC
17
Cov
60%
_is_only_decimal_digits
6.0
__ANON__
6.0
__ANON__
6.0
CRAP 3.7
« Brick/Regexes.pm ↑ Summary Brick/Selectors.pm »
1 package Brick::Numbers;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 5 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 $VERSION = '0.904';
8  
9 package Brick::Bucket;
10 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 =encoding utf8
14  
15 =head1 NAME
16  
17 Brick - This is the description
18  
19 =head1 SYNOPSIS
20  
21 use Brick;
22  
23 =head1 DESCRIPTION
24  
25 =cut
26  
27 =over 4
28  
29 =item number_within_range( HASHREF )
30  
31 Hash fields:
32  
33 minimum - the lower bound
34 maximum - the higher bound
35 inclusive - true includes bounds, false excludes bounds
36  
37 =cut
38  
39 sub number_within_range {
40 2 ▶ my( $bucket, $setup ) = @_;
Subroutine number_within_range: called CC=5 SCAR=17.4
Pod undocumented
41  
42 2 my @missing = sort grep { ! defined $setup->{$_} } qw( minimum maximum );
43  
44 2 ▶ if( @missing ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@missing) 0 2 2
45 5 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
46 0 ▶ croak( sprintf "number_within_range missing %s%s attibute%s",
Branch: 2 branches
BranchTrueFalseTotal
$missing[1] ? : 0 0 0
$missing[1] ? : 0 0 0
47 $missing[0],
48 $missing[1] ? " and $missing[1]" : '',
49 $missing[1] ? 's' : ''
50 );
51 }
52  
53 2 my $format_sub = $bucket->_is_decimal_integer( $setup );
54  
55 my $range_sub = $setup->{inclusive} ?
56 2 ▶ $bucket->_inclusive_within_numeric_range( $setup )
Branch: 1 branch
BranchTrueFalseTotal
$setup->{'inclusive'} ? : 1 1 2
57 :
58 $bucket->_exclusive_within_numeric_range( $setup );
59  
60 2 my $composed_sub = $bucket->__compose_satisfy_all( $format_sub, $range_sub );
61  
62 2 $bucket->__make_constraint( $composed_sub, $setup );
63 }
64  
65 sub _is_only_decimal_digits {
66 0 ▶ my( $bucket, $setup ) = @_;
Subroutine _is_only_decimal_digits: not called CC=2 SCAR=17.9
67  
68 0 my @caller = $bucket->__caller_chain_as_list();
69  
70 my $sub = $bucket->_matches_regex( {
71 description => "The $setup->{field} value only has decimal digits",
72 field => $setup->{field},
73 0 ▶ name => $setup->{name} || $caller[0]{'sub'},
Condition0%0/3
$setup->{'name'} || $caller[0]{'sub'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$setup->{'name'} || $caller[0]{'sub'}
$setup->{'name'} $caller[0]{'sub'}
Truth table0%0/3
A || B
A= $setup->{'name'}B= $caller[0]{'sub'}
ABresult
1X1
011
000
74 regex => qr/
75 \A
76 \d+ # digits only
77 \z
78 /x,
79 } );
80  
81 0 my $composed = $bucket->__compose_satisfy_all( $sub );
82  
83 $bucket->add_to_bucket( {
84 0 name => $caller[0]{'sub'},
85 code => $composed,
86 } );
87 }
88  
89 sub _is_decimal_integer {
90 2 ▶ my( $bucket, $setup ) = @_;
Subroutine _is_decimal_integer: called CC=2 SCAR=7.1
91  
92 2 my @caller = $bucket->__caller_chain_as_list();
93  
94 5 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
95 my $sub = $bucket->_matches_regex( {
96 description => "The $setup->{field} is an integer in base 10",
97 field => $setup->{field},
98 2 ▶ name => $setup->{name} || $caller[0]{'sub'},
Condition33%1/3
$setup->{'name'} || $caller[0]{'sub'}
l!l&&r!l&&!r
020
MC/DC0%0/2
$setup->{'name'} || $caller[0]{'sub'}
$setup->{'name'} $caller[0]{'sub'}
Truth table33%1/3
A || B
A= $setup->{'name'}B= $caller[0]{'sub'}
ABresult
1X1
011
000
99 regex => qr/
100 \A
101 (?:[+-])? # optional leading sign
102 \d+
103 \z
104 /x,
105 } );
106  
107 2 my $composed = $bucket->__compose_satisfy_all( $sub );
108  
109 $bucket->add_to_bucket( {
110 2 name => $caller[0]{'sub'},
111 code => $composed,
112 } );
113 }
114  
115 sub _inclusive_within_numeric_range {
116 1 ▶ my( $bucket, $setup ) = @_;
Subroutine _inclusive_within_numeric_range: called CC=2 SCAR=7.6
117  
118 1 my @caller = $bucket->__caller_chain_as_list();
119  
120 $bucket->add_to_bucket( {
121 name => $setup->{name} || $caller[0]{'sub'},
122 description => "Find number within the range [$setup->{minimum}, $setup->{maximum}] inclusively",
123 1 ▶ fields => [ $setup->{field} ],
Condition33%1/3
$setup->{'name'} || $caller[0]{'sub'}
l!l&&r!l&&!r
010
MC/DC0%0/2
$setup->{'name'} || $caller[0]{'sub'}
$setup->{'name'} $caller[0]{'sub'}
Truth table33%1/3
A || B
A= $setup->{'name'}B= $caller[0]{'sub'}
ABresult
1X1
011
000
124 code => $bucket->__compose_satisfy_all(
125 $bucket->_numeric_equal_or_greater_than( $setup ),
126 $bucket->_numeric_equal_or_less_than( $setup ),
127 ),
128 } );
129 }
130  
131 sub _exclusive_within_numeric_range {
132 1 ▶ my( $bucket, $setup ) = @_;
Subroutine _exclusive_within_numeric_range: called CC=2 SCAR=7.6
133  
134 1 my @caller = $bucket->__caller_chain_as_list();
135  
136 $bucket->add_to_bucket( {
137 name => $setup->{name} || $caller[0]{'sub'},
138 description => "Find number within the range [$setup->{minimum}, $setup->{maximum}] exclusively",
139 1 ▶ fields => [ $setup->{field} ],
Condition33%1/3
$setup->{'name'} || $caller[0]{'sub'}
l!l&&r!l&&!r
010
MC/DC0%0/2
$setup->{'name'} || $caller[0]{'sub'}
$setup->{'name'} $caller[0]{'sub'}
Truth table33%1/3
A || B
A= $setup->{'name'}B= $caller[0]{'sub'}
ABresult
1X1
011
000
140 code => $bucket->__compose_satisfy_all(
141 $bucket->_numeric_strictly_greater_than( $setup ),
142 $bucket->_numeric_strictly_less_than( $setup ),
143 ),
144 } );
145  
146 }
147  
148 sub _numeric_equal_or_greater_than {
149 1 ▶ my( $bucket, $setup ) = @_;
Subroutine _numeric_equal_or_greater_than: called CC=2 SCAR=9.9
150  
151 1 my @caller = $bucket->__caller_chain_as_list();
152  
153 $bucket->add_to_bucket({
154 name => $setup->{name} || $caller[0]{'sub'},
155 description => "The number is equal to or greater than $setup->{minimum}",
156 fields => [ $setup->{field} ],
157 code => sub {
158 die {
159 message => "The number in $setup->{field} was $_[0]->{ $setup->{field} }, but should have been greater than or equal to $setup->{minimum}",
160 failed_field => $setup->{field},
161 handler => $caller[0]{'sub'},
162 } unless $_[0]->{ $setup->{field} } >= $setup->{minimum}
163 0 ▶ },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{$setup->{"field"}} >= $setup->{"minimum"} 0 0 0
164 1 ▶ } );
Condition33%1/3
$setup->{'name'} || $caller[0]{'sub'}
l!l&&r!l&&!r
010
MC/DC0%0/2
$setup->{'name'} || $caller[0]{'sub'}
$setup->{'name'} $caller[0]{'sub'}
Truth table33%1/3
A || B
A= $setup->{'name'}B= $caller[0]{'sub'}
ABresult
1X1
011
000
165 }
166  
167 sub _numeric_strictly_greater_than {
168 1 ▶ my( $bucket, $setup ) = @_;
Subroutine _numeric_strictly_greater_than: called CC=2 SCAR=9.9
169  
170 1 my @caller = $bucket->__caller_chain_as_list();
171  
172 $bucket->add_to_bucket({
173 name => $setup->{name} || $caller[0]{'sub'},
174 description => "The number is greater than $setup->{minimum}",
175 fields => [ $setup->{field} ],
176 code => sub {
177 die {
178 message => "The number in $setup->{field} was $_[0]->{ $setup->{field} }, but should have been strictly greater than $setup->{minimum}",
179 failed_field => $setup->{field},
180 handler => $caller[0]{'sub'},
181 0 ▶ } unless $_[0]->{ $setup->{field} } > $setup->{minimum};
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{$setup->{"field"}} > $setup->{"minimum"} 0 0 0
182 },
183 1 ▶ } );
Condition33%1/3
$setup->{'name'} || $caller[0]{'sub'}
l!l&&r!l&&!r
010
MC/DC0%0/2
$setup->{'name'} || $caller[0]{'sub'}
$setup->{'name'} $caller[0]{'sub'}
Truth table33%1/3
A || B
A= $setup->{'name'}B= $caller[0]{'sub'}
ABresult
1X1
011
000
184 }
185  
186 sub _numeric_equal_or_less_than {
187 1 ▶ my( $bucket, $setup ) = @_;
Subroutine _numeric_equal_or_less_than: called CC=2 SCAR=9.9
188  
189 1 my @caller = $bucket->__caller_chain_as_list();
190  
191 $bucket->add_to_bucket({
192 name => $setup->{name} || $caller[0]{'sub'},
193 description => "The number is equal to or less than $setup->{maximum}",
194 fields => [ $setup->{field} ],
195 code => sub {
196 die {
197 message => "The number in $setup->{field} was $_[0]->{ $setup->{field} }, but should have been less than or equal to $setup->{maximum}",
198 failed_field => $setup->{field},
199 handler => $caller[0]{'sub'},
200 0 ▶ } unless $_[0]->{ $setup->{field} } <= $setup->{maximum};
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{$setup->{"field"}} <= $setup->{"maximum"} 0 0 0
201 },
202 1 ▶ } );
Condition33%1/3
$setup->{'name'} || $caller[0]{'sub'}
l!l&&r!l&&!r
010
MC/DC0%0/2
$setup->{'name'} || $caller[0]{'sub'}
$setup->{'name'} $caller[0]{'sub'}
Truth table33%1/3
A || B
A= $setup->{'name'}B= $caller[0]{'sub'}
ABresult
1X1
011
000
203 }
204  
205 sub _numeric_strictly_less_than {
206 1 ▶ my( $bucket, $setup ) = @_;
Subroutine _numeric_strictly_less_than: called CC=2 SCAR=9.9
207  
208 1 my @caller = $bucket->__caller_chain_as_list();
209  
210 $bucket->add_to_bucket({
211 name => $setup->{name} || $caller[0]{'sub'},
212 description => "The number is less than $setup->{maximum}",
213 fields => [ $setup->{field} ],
214 code => sub {
215 die {
216 message => "The number in $setup->{field} was $_[0]->{ $setup->{field} }, but should have been strictly less than $setup->{maximum}",
217 failed_field => $setup->{field},
218 handler => $caller[0]{'sub'},
219 0 ▶ } unless $_[0]->{ $setup->{field} } < $setup->{maximum};
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{$setup->{"field"}} < $setup->{"maximum"} 0 0 0
220 },
221 1 ▶ } );
Condition33%1/3
$setup->{'name'} || $caller[0]{'sub'}
l!l&&r!l&&!r
010
MC/DC0%0/2
$setup->{'name'} || $caller[0]{'sub'}
$setup->{'name'} $caller[0]{'sub'}
Truth table33%1/3
A || B
A= $setup->{'name'}B= $caller[0]{'sub'}
ABresult
1X1
011
000
222 }
223  
224 =back
225  
226 =head1 TO DO
227  
228 TBA
229  
230 =head1 SEE ALSO
231  
232 TBA
233  
234 =head1 SOURCE AVAILABILITY
235  
236 This source is in Github:
237  
238 https://github.com/briandfoy/brick
239  
240 =head1 AUTHOR
241  
242 brian d foy, C<< <briandfoy@pobox.com> >>
243  
244 =head1 COPYRIGHT
245  
246 Copyright © 2007-2025, brian d foy <briandfoy@pobox.com>. All rights reserved.
247  
248 You may redistribute this under the terms of the Artistic License 2.0.
249  
250 =cut
251  
252 5 1;
« Brick/Regexes.pm ↑ Summary Brick/Selectors.pm »