File Coverage

blib/lib/Data/Transpose/Validator/NumericRange.pm
Criterion Covered Total %
statement 30 30 100.0
branch 13 14 92.8
condition 3 3 100.0
subroutine 7 7 100.0
pod 1 1 100.0
total 54 55 98.1


line stmt bran cond sub pod time code
1             package Data::Transpose::Validator::NumericRange;
2 5     5   2897 use strict;
  5         6  
  5         152  
3 5     5   20 use warnings;
  5         5  
  5         126  
4              
5 5     5   20 use Moo;
  5         8  
  5         27  
6             extends 'Data::Transpose::Validator::Base';
7 5     5   1294 use Scalar::Util qw/looks_like_number/;
  5         7  
  5         358  
8 5     5   24 use MooX::Types::MooseLike::Base qw(:all);
  5         5  
  5         1758  
9 5     5   26 use namespace::clean;
  5         8  
  5         49  
10              
11             =head1 NAME
12              
13             Data::Transpose::Validator::NumericRange - Validate numbers in a range
14              
15             =head1 METHODS
16              
17             =head2 new(min => $min, max => $max, integer => $bool)
18              
19             Constructor, setting the minimum, the maximum and the C
20             option, which will validate only integers.
21              
22             =cut
23              
24              
25             has min => (is => 'rw',
26             isa => Num,
27             required => 1);
28             has max => (is => 'rw',
29             isa => Num,
30             required => 1);
31             has integer => (is => 'rw',
32             isa => Bool,
33             default => sub { 0 },
34             );
35              
36              
37             =head2 is_valid($number)
38              
39             The validator. Returns a true value if the number is in the range
40             passed to the constructor.
41              
42             =cut
43              
44              
45             sub is_valid {
46 40     40 1 4897 my ($self, $arg) = @_;
47 40         187 $self->reset_errors;
48 40 50       3418 $self->error(["undefined", "Not defined"]) unless defined $arg;
49 40 100       210 $self->error(["notanumber", "Not a number"]) unless looks_like_number($arg);
50 40 100       784 if ($self->integer) {
51 15 100       1676 $self->error(["notinteger", "Not an integer"]) unless $arg =~ m/^\d+$/;
52             }
53 40 100       1964 return undef if $self->error;
54 31         628 my $min = $self->min;
55 31         2903 my $max = $self->max;
56 31 100 100     2480 if ($arg < $min or $arg > $max) {
57 7         50 $self->error(["outofrange", "Value is out of range ($min/$max)"])
58             }
59 31 100       89 $self->error? return 0 : return 1;
60             }
61              
62             =head1 INTERNAL ACCESSORS
63              
64             =head2 min
65              
66             Return the minimum
67              
68             =head2 min
69              
70             Return the maximum
71              
72             =head2 integer
73              
74             Return true if we have to validate only integers
75              
76             =cut
77              
78              
79             1;