Zero is not negative or positive
[gitmo/MooseX-Types-Common.git] / lib / MooseX / Types / Common / Numeric.pm
1 package MooseX::Types::Common::Numeric;
2
3 use strict;
4 use warnings;
5
6 our $VERSION = '0.001001';
7
8 use MooseX::Types -declare => [
9   qw(PositiveNum PositiveInt NegativeNum NegativeInt SingleDigit)
10 ];
11
12 use MooseX::Types::Moose qw/Num Int/;
13
14 subtype PositiveNum,
15   as Num,
16   where { $_ > 0 },
17   message { "Must be a positive number" },
18     ( $Moose::VERSION >= 2.0200
19         ? inline_as {
20             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
21                 . qq{ ($_[1] > 0) };
22         }
23         : ()
24     );
25
26 subtype PositiveInt,
27   as Int,
28   where { $_ > 0 },
29   message { "Must be a positive integer" },
30     ( $Moose::VERSION >= 2.0200
31         ? inline_as {
32             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
33                 . qq{ ($_[1] > 0) };
34         }
35         : ()
36     );
37
38 subtype NegativeNum,
39   as Num,
40   where { $_ < 0 },
41   message { "Must be a negative number" },
42     ( $Moose::VERSION >= 2.0200
43         ? inline_as {
44             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
45                 . qq{ ($_[1] < 0) };
46         }
47         : ()
48     );
49
50 subtype NegativeInt,
51   as Int,
52   where { $_ < 0 },
53   message { "Must be a negative integer" },
54     ( $Moose::VERSION >= 2.0200
55         ? inline_as {
56             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
57                 . qq{ ($_[1] < 0) };
58         }
59         : ()
60     );
61
62 subtype SingleDigit,
63   as PositiveInt,
64   where { $_ <= 9 },
65   message { "Must be a single digit" },
66     ( $Moose::VERSION >= 2.0200
67         ? inline_as {
68             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
69                 . qq{ ($_[1] <= 9) };
70         }
71         : ()
72     );
73
74 1;
75
76 __END__;
77
78 =head1 NAME
79
80 MooseX::Types::Common::Numeric - Commonly used numeric types
81
82 =head1 SYNOPSIS
83
84     use MooseX::Types::Common::Numeric qw/PositiveInt/;
85     has count => (is => 'rw', isa => PositiveInt);
86
87     ...
88     #this will fail
89     $object->count(-33);
90
91 =head1 DESCRIPTION
92
93 A set of commonly-used numeric type constraints that do not ship with Moose by
94 default.
95
96 =over
97
98 =item * PositiveNum
99
100 =item * PositiveInt
101
102 =item * NegativeNum
103
104 =item * Int
105
106 =item * SingleDigit
107
108 =back
109
110 =head1 SEE ALSO
111
112 =over
113
114 =item * L<MooseX::Types::Common::String>
115
116 =back
117
118 =head1 AUTHORS
119
120 Please see:: L<MooseX::Types::Common>
121
122 =cut