Add OrZero types for numbers
[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 PositiveOrZeroNum
10      PositiveInt PositiveOrZeroInt
11      NegativeNum NegativeOrZeroNum
12      NegativeInt NegativeOrZeroInt
13      SingleDigit)
14 ];
15
16 use MooseX::Types::Moose qw/Num Int/;
17
18 subtype PositiveNum,
19   as Num,
20   where { $_ > 0 },
21   message { "Must be a positive number" },
22     ( $Moose::VERSION >= 2.0200
23         ? inline_as {
24             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
25                 . qq{ ($_[1] > 0) };
26         }
27         : ()
28     );
29
30 subtype PositiveOrZeroNum,
31   as Num,
32   where { $_ >= 0 },
33   message { "Must be a number greater than or equal to zero" },
34     ( $Moose::VERSION >= 2.0200
35         ? inline_as {
36             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
37                 . qq{ ($_[1] >= 0) };
38         }
39         : ()
40     );
41
42 subtype PositiveInt,
43   as Int,
44   where { $_ > 0 },
45   message { "Must be a positive integer" },
46     ( $Moose::VERSION >= 2.0200
47         ? inline_as {
48             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
49                 . qq{ ($_[1] > 0) };
50         }
51         : ()
52     );
53
54 subtype PositiveOrZeroInt,
55   as Int,
56   where { $_ >= 0 },
57   message { "Must be an integer greater than or equal to zero" },
58     ( $Moose::VERSION >= 2.0200
59         ? inline_as {
60             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
61                 . qq{ ($_[1] >= 0) };
62         }
63         : ()
64     );
65
66 subtype NegativeNum,
67   as Num,
68   where { $_ < 0 },
69   message { "Must be a negative number" },
70     ( $Moose::VERSION >= 2.0200
71         ? inline_as {
72             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
73                 . qq{ ($_[1] < 0) };
74         }
75         : ()
76     );
77
78 subtype NegativeOrZeroNum,
79   as Num,
80   where { $_ <= 0 },
81   message { "Must be a number less than or equal to zero" },
82     ( $Moose::VERSION >= 2.0200
83         ? inline_as {
84             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
85                 . qq{ ($_[1] <= 0) };
86         }
87         : ()
88     );
89
90 subtype NegativeInt,
91   as Int,
92   where { $_ < 0 },
93   message { "Must be a negative integer" },
94     ( $Moose::VERSION >= 2.0200
95         ? inline_as {
96             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
97                 . qq{ ($_[1] < 0) };
98         }
99         : ()
100     );
101
102 subtype NegativeOrZeroInt,
103   as Int,
104   where { $_ <= 0 },
105   message { "Must be an integer less than or equal to zero" },
106     ( $Moose::VERSION >= 2.0200
107         ? inline_as {
108             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
109                 . qq{ ($_[1] <= 0) };
110         }
111         : ()
112     );
113
114 subtype SingleDigit,
115   as PositiveInt,
116   where { $_ <= 9 },
117   message { "Must be a single digit" },
118     ( $Moose::VERSION >= 2.0200
119         ? inline_as {
120             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
121                 . qq{ ($_[1] <= 9) };
122         }
123         : ()
124     );
125
126 1;
127
128 __END__;
129
130 =head1 NAME
131
132 MooseX::Types::Common::Numeric - Commonly used numeric types
133
134 =head1 SYNOPSIS
135
136     use MooseX::Types::Common::Numeric qw/PositiveInt/;
137     has count => (is => 'rw', isa => PositiveInt);
138
139     ...
140     #this will fail
141     $object->count(-33);
142
143 =head1 DESCRIPTION
144
145 A set of commonly-used numeric type constraints that do not ship with Moose by
146 default.
147
148 =over
149
150 =item * PositiveNum
151
152 =item * PositiveOrZeroNum
153
154 =item * PositiveInt
155
156 =item * PositiveOrZeroInt
157
158 =item * NegativeNum
159
160 =item * NegativeOrZeroNum
161
162 =item * NegativeInt
163
164 =item * NegativeOrZeroInt
165
166 =item * SingleDigit
167
168 =back
169
170 =head1 SEE ALSO
171
172 =over
173
174 =item * L<MooseX::Types::Common::String>
175
176 =back
177
178 =head1 AUTHORS
179
180 Please see:: L<MooseX::Types::Common>
181
182 =cut