File Coverage

blib/lib/Number/Tolerant/Constant.pm
Criterion Covered Total %
statement 26 26 100.0
branch 6 6 100.0
condition n/a
subroutine 10 10 100.0
pod n/a
total 42 42 100.0


line stmt bran cond sub pod time code
1 1     1   630 use strict;
  1         2  
  1         24  
2 1     1   4 use warnings;
  1         1  
  1         42  
3             package Number::Tolerant::Constant 1.709;
4             # ABSTRACT: a blessed constant type
5              
6             #pod =head1 SYNOPSIS
7             #pod
8             #pod use Number::Tolerant;
9             #pod use Number::Tolerant::Constant;
10             #pod
11             #pod my $range = tolerance(10);
12             #pod ref $range; # "Number::Tolerant" -- w/o ::Constant, would be undef
13             #pod
14             #pod =head1 DESCRIPTION
15             #pod
16             #pod When Number::Tolerant is about to return a tolerance with zero variation, it
17             #pod will return a constant instead. This module will register a constant type that
18             #pod will catch these constants and return them as Number::Tolerant objects.
19             #pod
20             #pod I wrote this module to make it simpler to use tolerances with Class::DBI, which
21             #pod would otherwise complain that the constructor hadn't returned a blessed object.
22             #pod
23             #pod =cut
24              
25             package
26             Number::Tolerant::Type::constant_obj;
27 1     1   4 use parent qw(Number::Tolerant::Type);
  1         2  
  1         5  
28              
29 5     5   6 sub construct { shift;
30 5         25 { value => $_[0], min => $_[0], max => $_[0], constant => 1 }
31             };
32              
33             sub parse {
34 7     7   13 my ($self, $string, $factory) = @_;
35 7         16 my $number = $self->number_re;
36 7 100       70 return $factory->new($string) if ($string =~ m!\A($number)\z!);
37 3         8 return;
38             }
39              
40 1     1   4 sub numify { $_[0]->{value} }
41              
42 3     3   9341 sub stringify { "$_[0]->{value}" }
43              
44             sub valid_args {
45 9     9   12 my $self = shift;
46 9         27 my $number = $self->normalize_number($_[0]);
47              
48 9 100       19 return unless defined $number;
49              
50 6 100       19 return $number if @_ == 1;
51              
52 1         3 return;
53             }
54              
55             package Number::Tolerant::Constant;
56              
57             sub import {
58 1     1   13 Number::Tolerant->disable_plugin("Number::Tolerant::Type::constant");
59 1         3 Number::Tolerant->enable_plugin( "Number::Tolerant::Type::constant_obj");
60             }
61              
62             sub _disable {
63 1     1   1713 Number::Tolerant->disable_plugin("Number::Tolerant::Type::constant_obj");
64 1         5 Number::Tolerant->enable_plugin( "Number::Tolerant::Type::constant");
65             }
66              
67             1;
68              
69             __END__