File Coverage

blib/lib/MooseX/Types/Common/Numeric.pm
Criterion Covered Total %
statement 18 18 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 24 24 100.0


line stmt bran cond sub pod time code
1             package MooseX::Types::Common::Numeric;
2             # ABSTRACT: Commonly used numeric types
3              
4             our $VERSION = '0.001015';
5              
6 1     1   104326 use strict;
  1         3  
  1         42  
7 1     1   5 use warnings;
  1         2  
  1         79  
8              
9 1         8 use MooseX::Types -declare => [
10             qw(PositiveNum PositiveOrZeroNum
11             PositiveInt PositiveOrZeroInt
12             NegativeNum NegativeOrZeroNum
13             NegativeInt NegativeOrZeroInt
14             SingleDigit)
15 1     1   711 ];
  1         437192  
16              
17 1     1   11380 use Moose ();
  1         232657  
  1         58  
18 1     1   752 use MooseX::Types::Moose qw/Num Int/;
  1         7094  
  1         13  
19 1     1   7481 use if MooseX::Types->VERSION >= 0.42, 'namespace::autoclean';
  1         7  
  1         57  
20              
21             subtype PositiveNum,
22             as Num,
23             where { $_ > 0 },
24             message { "Must be a positive number" },
25             ( $Moose::VERSION >= 2.0200
26             ? inline_as {
27             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
28             . qq{ ($_[1] > 0) };
29             }
30             : ()
31             );
32              
33             subtype PositiveOrZeroNum,
34             as Num,
35             where { $_ >= 0 },
36             message { "Must be a number greater than or equal to zero" },
37             ( $Moose::VERSION >= 2.0200
38             ? inline_as {
39             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
40             . qq{ ($_[1] >= 0) };
41             }
42             : ()
43             );
44              
45             subtype PositiveInt,
46             as Int,
47             where { $_ > 0 },
48             message { "Must be a positive integer" },
49             ( $Moose::VERSION >= 2.0200
50             ? inline_as {
51             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
52             . qq{ ($_[1] > 0) };
53             }
54             : ()
55             );
56              
57             subtype PositiveOrZeroInt,
58             as Int,
59             where { $_ >= 0 },
60             message { "Must be an integer greater than or equal to zero" },
61             ( $Moose::VERSION >= 2.0200
62             ? inline_as {
63             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
64             . qq{ ($_[1] >= 0) };
65             }
66             : ()
67             );
68              
69             subtype NegativeNum,
70             as Num,
71             where { $_ < 0 },
72             message { "Must be a negative number" },
73             ( $Moose::VERSION >= 2.0200
74             ? inline_as {
75             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
76             . qq{ ($_[1] < 0) };
77             }
78             : ()
79             );
80              
81             subtype NegativeOrZeroNum,
82             as Num,
83             where { $_ <= 0 },
84             message { "Must be a number less than or equal to zero" },
85             ( $Moose::VERSION >= 2.0200
86             ? inline_as {
87             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
88             . qq{ ($_[1] <= 0) };
89             }
90             : ()
91             );
92              
93             subtype NegativeInt,
94             as Int,
95             where { $_ < 0 },
96             message { "Must be a negative integer" },
97             ( $Moose::VERSION >= 2.0200
98             ? inline_as {
99             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
100             . qq{ ($_[1] < 0) };
101             }
102             : ()
103             );
104              
105             subtype NegativeOrZeroInt,
106             as Int,
107             where { $_ <= 0 },
108             message { "Must be an integer less than or equal to zero" },
109             ( $Moose::VERSION >= 2.0200
110             ? inline_as {
111             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
112             . qq{ ($_[1] <= 0) };
113             }
114             : ()
115             );
116              
117             subtype SingleDigit,
118             as Int,
119             where { $_ >= -9 and $_ <= 9 },
120             message { "Must be a single digit" },
121             ( $Moose::VERSION >= 2.0200
122             ? inline_as {
123             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
124             . qq{ ($_[1] >= -9 and $_[1] <= 9) };
125             }
126             : ()
127             );
128              
129             1;
130              
131             __END__
132              
133             =pod
134              
135             =encoding UTF-8
136              
137             =head1 NAME
138              
139             MooseX::Types::Common::Numeric - Commonly used numeric types
140              
141             =head1 VERSION
142              
143             version 0.001015
144              
145             =head1 SYNOPSIS
146              
147             use MooseX::Types::Common::Numeric qw/PositiveInt/;
148             has count => (is => 'rw', isa => PositiveInt);
149              
150             ...
151             #this will fail
152             $object->count(-33);
153              
154             =head1 DESCRIPTION
155              
156             A set of commonly-used numeric type constraints that do not ship with Moose by
157             default.
158              
159             =over
160              
161             =item * C<PositiveNum>
162              
163             =item * C<PositiveOrZeroNum>
164              
165             =item * C<PositiveInt>
166              
167             =item * C<PositiveOrZeroInt>
168              
169             =item * C<NegativeNum>
170              
171             =item * C<NegativeOrZeroNum>
172              
173             =item * C<NegativeInt>
174              
175             =item * C<NegativeOrZeroInt>
176              
177             =item * C<SingleDigit>
178              
179             =back
180              
181             =head1 SEE ALSO
182              
183             =over
184              
185             =item * L<MooseX::Types::Common::String>
186              
187             =back
188              
189             =head1 SUPPORT
190              
191             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Types-Common>
192             (or L<bug-MooseX-Types-Common@rt.cpan.org|mailto:bug-MooseX-Types-Common@rt.cpan.org>).
193              
194             There is also a mailing list available for users of this distribution, at
195             L<http://lists.perl.org/list/moose.html>.
196              
197             There is also an irc channel available for users of this distribution, at
198             L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
199              
200             =head1 AUTHORS
201              
202             =over 4
203              
204             =item *
205              
206             Matt S Trout - mst (at) shadowcat.co.uk (L<https://www.shadowcat.co.uk/>)
207              
208             =item *
209              
210             K. James Cheetham <jamie@shadowcat.co.uk>
211              
212             =item *
213              
214             Guillermo Roditi <groditi@gmail.com>
215              
216             =back
217              
218             =head1 COPYRIGHT AND LICENSE
219              
220             This software is copyright (c) 2009 by Matt S Trout - mst (at) shadowcat.co.uk (L<https://www.shadowcat.co.uk/>).
221              
222             This is free software; you can redistribute it and/or modify it under
223             the same terms as the Perl 5 programming language system itself.
224              
225             =cut