Commit | Line | Data |
ac73ab52 |
1 | package MooseX::Types::Common::Numeric; |
3ebb788b |
2 | # ABSTRACT: Commonly used numeric types |
ac73ab52 |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
ac73ab52 |
7 | use MooseX::Types -declare => [ |
d52d9696 |
8 | qw(PositiveNum PositiveOrZeroNum |
9 | PositiveInt PositiveOrZeroInt |
10 | NegativeNum NegativeOrZeroNum |
11 | NegativeInt NegativeOrZeroInt |
12 | SingleDigit) |
ac73ab52 |
13 | ]; |
14 | |
15 | use MooseX::Types::Moose qw/Num Int/; |
16 | |
17 | subtype PositiveNum, |
18 | as Num, |
d0b8bc7f |
19 | where { $_ > 0 }, |
d2461fda |
20 | message { "Must be a positive number" }, |
21 | ( $Moose::VERSION >= 2.0200 |
22 | ? inline_as { |
23 | $_[0]->parent()->_inline_check( $_[1] ) . ' && ' |
d0b8bc7f |
24 | . qq{ ($_[1] > 0) }; |
d2461fda |
25 | } |
26 | : () |
27 | ); |
ac73ab52 |
28 | |
d52d9696 |
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 | |
ac73ab52 |
41 | subtype PositiveInt, |
42 | as Int, |
d0b8bc7f |
43 | where { $_ > 0 }, |
d2461fda |
44 | message { "Must be a positive integer" }, |
45 | ( $Moose::VERSION >= 2.0200 |
46 | ? inline_as { |
47 | $_[0]->parent()->_inline_check( $_[1] ) . ' && ' |
d0b8bc7f |
48 | . qq{ ($_[1] > 0) }; |
d2461fda |
49 | } |
50 | : () |
51 | ); |
ac73ab52 |
52 | |
d52d9696 |
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 | |
ac73ab52 |
65 | subtype NegativeNum, |
66 | as Num, |
d0b8bc7f |
67 | where { $_ < 0 }, |
d2461fda |
68 | message { "Must be a negative number" }, |
69 | ( $Moose::VERSION >= 2.0200 |
70 | ? inline_as { |
71 | $_[0]->parent()->_inline_check( $_[1] ) . ' && ' |
d0b8bc7f |
72 | . qq{ ($_[1] < 0) }; |
d2461fda |
73 | } |
74 | : () |
75 | ); |
ac73ab52 |
76 | |
d52d9696 |
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 | |
ac73ab52 |
89 | subtype NegativeInt, |
90 | as Int, |
d0b8bc7f |
91 | where { $_ < 0 }, |
d2461fda |
92 | message { "Must be a negative integer" }, |
93 | ( $Moose::VERSION >= 2.0200 |
94 | ? inline_as { |
95 | $_[0]->parent()->_inline_check( $_[1] ) . ' && ' |
d0b8bc7f |
96 | . qq{ ($_[1] < 0) }; |
d2461fda |
97 | } |
98 | : () |
99 | ); |
ac73ab52 |
100 | |
d52d9696 |
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 | |
ac73ab52 |
113 | subtype SingleDigit, |
114 | as PositiveInt, |
115 | where { $_ <= 9 }, |
d2461fda |
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 | ); |
ac73ab52 |
124 | |
ac73ab52 |
125 | 1; |
126 | |
3ebb788b |
127 | __END__ |
ac73ab52 |
128 | |
3ebb788b |
129 | =pod |
ac73ab52 |
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 | |
d52d9696 |
149 | =item * PositiveOrZeroNum |
150 | |
ac73ab52 |
151 | =item * PositiveInt |
152 | |
d52d9696 |
153 | =item * PositiveOrZeroInt |
154 | |
ac73ab52 |
155 | =item * NegativeNum |
156 | |
d52d9696 |
157 | =item * NegativeOrZeroNum |
158 | |
159 | =item * NegativeInt |
160 | |
161 | =item * NegativeOrZeroInt |
ac73ab52 |
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 | |
ac73ab52 |
175 | =cut |