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