Add OrZero types for numbers
[gitmo/MooseX-Types-Common.git] / lib / MooseX / Types / Common / Numeric.pm
CommitLineData
ac73ab52 1package MooseX::Types::Common::Numeric;
2
3use strict;
4use warnings;
5
6820e939 6our $VERSION = '0.001001';
ac73ab52 7
8use MooseX::Types -declare => [
d52d9696 9 qw(PositiveNum PositiveOrZeroNum
10 PositiveInt PositiveOrZeroInt
11 NegativeNum NegativeOrZeroNum
12 NegativeInt NegativeOrZeroInt
13 SingleDigit)
ac73ab52 14];
15
16use MooseX::Types::Moose qw/Num Int/;
17
18subtype PositiveNum,
19 as Num,
d0b8bc7f 20 where { $_ > 0 },
d2461fda 21 message { "Must be a positive number" },
22 ( $Moose::VERSION >= 2.0200
23 ? inline_as {
24 $_[0]->parent()->_inline_check( $_[1] ) . ' && '
d0b8bc7f 25 . qq{ ($_[1] > 0) };
d2461fda 26 }
27 : ()
28 );
ac73ab52 29
d52d9696 30subtype 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
ac73ab52 42subtype PositiveInt,
43 as Int,
d0b8bc7f 44 where { $_ > 0 },
d2461fda 45 message { "Must be a positive integer" },
46 ( $Moose::VERSION >= 2.0200
47 ? inline_as {
48 $_[0]->parent()->_inline_check( $_[1] ) . ' && '
d0b8bc7f 49 . qq{ ($_[1] > 0) };
d2461fda 50 }
51 : ()
52 );
ac73ab52 53
d52d9696 54subtype 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
ac73ab52 66subtype NegativeNum,
67 as Num,
d0b8bc7f 68 where { $_ < 0 },
d2461fda 69 message { "Must be a negative number" },
70 ( $Moose::VERSION >= 2.0200
71 ? inline_as {
72 $_[0]->parent()->_inline_check( $_[1] ) . ' && '
d0b8bc7f 73 . qq{ ($_[1] < 0) };
d2461fda 74 }
75 : ()
76 );
ac73ab52 77
d52d9696 78subtype 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
ac73ab52 90subtype NegativeInt,
91 as Int,
d0b8bc7f 92 where { $_ < 0 },
d2461fda 93 message { "Must be a negative integer" },
94 ( $Moose::VERSION >= 2.0200
95 ? inline_as {
96 $_[0]->parent()->_inline_check( $_[1] ) . ' && '
d0b8bc7f 97 . qq{ ($_[1] < 0) };
d2461fda 98 }
99 : ()
100 );
ac73ab52 101
d52d9696 102subtype 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
ac73ab52 114subtype SingleDigit,
115 as PositiveInt,
116 where { $_ <= 9 },
d2461fda 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 );
ac73ab52 125
ac73ab52 1261;
127
128__END__;
129
ac73ab52 130=head1 NAME
131
6820e939 132MooseX::Types::Common::Numeric - Commonly used numeric types
ac73ab52 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
145A set of commonly-used numeric type constraints that do not ship with Moose by
146default.
147
148=over
149
150=item * PositiveNum
151
d52d9696 152=item * PositiveOrZeroNum
153
ac73ab52 154=item * PositiveInt
155
d52d9696 156=item * PositiveOrZeroInt
157
ac73ab52 158=item * NegativeNum
159
d52d9696 160=item * NegativeOrZeroNum
161
162=item * NegativeInt
163
164=item * NegativeOrZeroInt
ac73ab52 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
180Please see:: L<MooseX::Types::Common>
181
182=cut