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   2129 use strict;
  5         8  
  5         118  
3 5     5   15 use warnings;
  5         4  
  5         101  
4              
5 5     5   12 use Moo;
  5         5  
  5         22  
6             extends 'Data::Transpose::Validator::Base';
7 5     5   1003 use Scalar::Util qw/looks_like_number/;
  5         30  
  5         235  
8 5     5   17 use MooX::Types::MooseLike::Base qw(:all);
  5         6  
  5         1271  
9 5     5   22 use namespace::clean;
  5         6  
  5         32  
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 1936 my ($self, $arg) = @_;
47 40         93 $self->reset_errors;
48 40 50       1874 $self->error(["undefined", "Not defined"]) unless defined $arg;
49 40 100       117 $self->error(["notanumber", "Not a number"]) unless looks_like_number($arg);
50 40 100       456 if ($self->integer) {
51 15 100       822 $self->error(["notinteger", "Not an integer"]) unless $arg =~ m/^\d+$/;
52             }
53 40 100       1433 return undef if $self->error;
54 31         338 my $min = $self->min;
55 31         1806 my $max = $self->max;
56 31 100 100     1615 if ($arg < $min or $arg > $max) {
57 7         27 $self->error(["outofrange", "Value is out of range ($min/$max)"])
58             }
59 31 100       59 $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;