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   853 use strict;
  1         2  
  1         26  
2 1     1   13 use warnings;
  1         1  
  1         63  
3             package Number::Tolerant::Constant;
4             # ABSTRACT: a blessed constant type
5             $Number::Tolerant::Constant::VERSION = '1.708';
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   1747 use parent qw(Number::Tolerant::Type);
  1         272  
  1         5  
28              
29 5     5   8 sub construct { shift;
30 5         38 { value => $_[0], min => $_[0], max => $_[0], constant => 1 }
31             };
32              
33             sub parse {
34 7     7   11 my ($self, $string, $factory) = @_;
35 7         28 my $number = $self->number_re;
36 7 100       103 return $factory->new($string) if ($string =~ m!\A($number)\z!);
37 3         13 return;
38             }
39              
40 1     1   6 sub numify { $_[0]->{value} }
41              
42 3     3   2429 sub stringify { "$_[0]->{value}" }
43              
44             sub valid_args {
45 9     9   14 my $self = shift;
46 9         31 my $number = $self->normalize_number($_[0]);
47              
48 9 100       37 return unless defined $number;
49              
50 6 100       38 return $number if @_ == 1;
51              
52 1         6 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         4 Number::Tolerant->enable_plugin( "Number::Tolerant::Type::constant_obj");
60             }
61              
62             sub _disable {
63 1     1   516 Number::Tolerant->disable_plugin("Number::Tolerant::Type::constant_obj");
64 1         8 Number::Tolerant->enable_plugin( "Number::Tolerant::Type::constant");
65             }
66              
67             1;
68              
69             __END__