SingleDigit now accepts [-9 .. 9] not [1 .. 9]
[gitmo/MooseX-Types-Common.git] / lib / MooseX / Types / Common / Numeric.pm
index af9e01a..30e580b 100644 (file)
@@ -1,12 +1,15 @@
 package MooseX::Types::Common::Numeric;
+# ABSTRACT: Commonly used numeric types
 
 use strict;
 use warnings;
 
-our $VERSION = '0.001001';
-
 use MooseX::Types -declare => [
-  qw(PositiveNum PositiveInt NegativeNum NegativeInt SingleDigit)
+  qw(PositiveNum PositiveOrZeroNum
+     PositiveInt PositiveOrZeroInt
+     NegativeNum NegativeOrZeroNum
+     NegativeInt NegativeOrZeroInt
+     SingleDigit)
 ];
 
 use MooseX::Types::Moose qw/Num Int/;
@@ -23,6 +26,18 @@ subtype PositiveNum,
         : ()
     );
 
+subtype PositiveOrZeroNum,
+  as Num,
+  where { $_ >= 0 },
+  message { "Must be a number greater than or equal to zero" },
+    ( $Moose::VERSION >= 2.0200
+        ? inline_as {
+            $_[0]->parent()->_inline_check( $_[1] ) . ' && '
+                . qq{ ($_[1] >= 0) };
+        }
+        : ()
+    );
+
 subtype PositiveInt,
   as Int,
   where { $_ > 0 },
@@ -35,6 +50,18 @@ subtype PositiveInt,
         : ()
     );
 
+subtype PositiveOrZeroInt,
+  as Int,
+  where { $_ >= 0 },
+  message { "Must be an integer greater than or equal to zero" },
+    ( $Moose::VERSION >= 2.0200
+        ? inline_as {
+            $_[0]->parent()->_inline_check( $_[1] ) . ' && '
+                . qq{ ($_[1] >= 0) };
+        }
+        : ()
+    );
+
 subtype NegativeNum,
   as Num,
   where { $_ < 0 },
@@ -47,6 +74,18 @@ subtype NegativeNum,
         : ()
     );
 
+subtype NegativeOrZeroNum,
+  as Num,
+  where { $_ <= 0 },
+  message { "Must be a number less than or equal to zero" },
+    ( $Moose::VERSION >= 2.0200
+        ? inline_as {
+            $_[0]->parent()->_inline_check( $_[1] ) . ' && '
+                . qq{ ($_[1] <= 0) };
+        }
+        : ()
+    );
+
 subtype NegativeInt,
   as Int,
   where { $_ < 0 },
@@ -59,25 +98,35 @@ subtype NegativeInt,
         : ()
     );
 
+subtype NegativeOrZeroInt,
+  as Int,
+  where { $_ <= 0 },
+  message { "Must be an integer less than or equal to zero" },
+    ( $Moose::VERSION >= 2.0200
+        ? inline_as {
+            $_[0]->parent()->_inline_check( $_[1] ) . ' && '
+                . qq{ ($_[1] <= 0) };
+        }
+        : ()
+    );
+
 subtype SingleDigit,
-  as PositiveInt,
-  where { $_ <= 9 },
+  as Int,
+  where { $_ >= -9 and $_ <= 9 },
   message { "Must be a single digit" },
     ( $Moose::VERSION >= 2.0200
         ? inline_as {
             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
-                . qq{ ($_[1] <= 9) };
+                . qq{ ($_[1] >= -9 and $_[1] <= 9) };
         }
         : ()
     );
 
 1;
 
-__END__;
+__END__
 
-=head1 NAME
-
-MooseX::Types::Common::Numeric - Commonly used numeric types
+=pod
 
 =head1 SYNOPSIS
 
@@ -95,15 +144,23 @@ default.
 
 =over
 
-=item * PositiveNum
+=item * C<PositiveNum>
+
+=item * C<PositiveOrZeroNum>
 
-=item * PositiveInt
+=item * C<PositiveInt>
 
-=item * NegativeNum
+=item * C<PositiveOrZeroInt>
 
-=item * Int
+=item * C<NegativeNum>
 
-=item * SingleDigit
+=item * C<NegativeOrZeroNum>
+
+=item * C<NegativeInt>
+
+=item * C<NegativeOrZeroInt>
+
+=item * C<SingleDigit>
 
 =back
 
@@ -115,8 +172,4 @@ default.
 
 =back
 
-=head1 AUTHORS
-
-Please see:: L<MooseX::Types::Common>
-
 =cut