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