Commit | Line | Data |
ac73ab52 |
1 | package MooseX::Types::Common::Numeric; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6820e939 |
6 | our $VERSION = '0.001001'; |
ac73ab52 |
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, |
d0b8bc7f |
16 | where { $_ > 0 }, |
d2461fda |
17 | message { "Must be a positive number" }, |
18 | ( $Moose::VERSION >= 2.0200 |
19 | ? inline_as { |
20 | $_[0]->parent()->_inline_check( $_[1] ) . ' && ' |
d0b8bc7f |
21 | . qq{ ($_[1] > 0) }; |
d2461fda |
22 | } |
23 | : () |
24 | ); |
ac73ab52 |
25 | |
26 | subtype PositiveInt, |
27 | as Int, |
d0b8bc7f |
28 | where { $_ > 0 }, |
d2461fda |
29 | message { "Must be a positive integer" }, |
30 | ( $Moose::VERSION >= 2.0200 |
31 | ? inline_as { |
32 | $_[0]->parent()->_inline_check( $_[1] ) . ' && ' |
d0b8bc7f |
33 | . qq{ ($_[1] > 0) }; |
d2461fda |
34 | } |
35 | : () |
36 | ); |
ac73ab52 |
37 | |
38 | subtype NegativeNum, |
39 | as Num, |
d0b8bc7f |
40 | where { $_ < 0 }, |
d2461fda |
41 | message { "Must be a negative number" }, |
42 | ( $Moose::VERSION >= 2.0200 |
43 | ? inline_as { |
44 | $_[0]->parent()->_inline_check( $_[1] ) . ' && ' |
d0b8bc7f |
45 | . qq{ ($_[1] < 0) }; |
d2461fda |
46 | } |
47 | : () |
48 | ); |
ac73ab52 |
49 | |
50 | subtype NegativeInt, |
51 | as Int, |
d0b8bc7f |
52 | where { $_ < 0 }, |
d2461fda |
53 | message { "Must be a negative integer" }, |
54 | ( $Moose::VERSION >= 2.0200 |
55 | ? inline_as { |
56 | $_[0]->parent()->_inline_check( $_[1] ) . ' && ' |
d0b8bc7f |
57 | . qq{ ($_[1] < 0) }; |
d2461fda |
58 | } |
59 | : () |
60 | ); |
ac73ab52 |
61 | |
62 | subtype SingleDigit, |
63 | as PositiveInt, |
64 | where { $_ <= 9 }, |
d2461fda |
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 | ); |
ac73ab52 |
73 | |
ac73ab52 |
74 | 1; |
75 | |
76 | __END__; |
77 | |
ac73ab52 |
78 | =head1 NAME |
79 | |
6820e939 |
80 | MooseX::Types::Common::Numeric - Commonly used numeric types |
ac73ab52 |
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 |