added enum test, more docs both internal and external and a few minor code clarification
John Napiorkowski [Fri, 26 Sep 2008 21:09:07 +0000 (21:09 +0000)]
Changes
lib/MooseX/Types.pm
lib/MooseX/Types/TypeDecorator.pm
t/13_typedecorator.t
t/lib/DecoratorLibrary.pm

diff --git a/Changes b/Changes
index 297da0f..89eec4b 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,9 +1,9 @@
 
-0.06    Fri Aug  5 12:00:00 EST 2008
+0.06    Fri Sep  25 12:00:00 EST 2008
         - Added support for parameterized types and type unions, tests for all
         that and documentation updates.
         
-0.05    ...
+0.05    [Indetermined]
         - moved export mechanism to Sub::Exporter. ::Base contains
           a bunch of wrapping logic to allow the export-along functionality
           for the helper symbols
index f2c60a7..06b285f 100644 (file)
@@ -339,9 +339,16 @@ yet defined.
 
 sub type_export_generator {
     my ($class, $type, $name) = @_;
+    
+    ## Return an anonymous subroutine that will generate the proxied type
+    ## constraint for you.
+    
     return sub {
         my $type_constraint;
         if(defined(my $params = shift @_)) {
+            ## We currently only allow a TC to accept a single, ArrayRef
+            ## parameter, as in HashRef[Int], where [Int] is what's inside the
+            ## ArrayRef passed.
             if(ref $params eq 'ARRAY') {
                 $type_constraint = $class->create_arged_type_constraint($name, @$params);
             } else {
@@ -355,6 +362,10 @@ sub type_export_generator {
          
         my $type_decorator = $class->create_type_decorator($type_constraint);
         
+        ## If there are additional args, that means it's probably stuff that
+        ## needs to be returned to the subtype.  Not an ideal solution here but
+        ## doesn't seem to cause trouble.
+        
         if(@_) {
             return ($type_decorator, @_);
         } else {
@@ -365,14 +376,15 @@ sub type_export_generator {
 
 =head2 create_arged_type_constraint ($name, @args)
 
-Given a String $name with @args find the matching typeconstraint.
+Given a String $name with @args find the matching typeconstraint and parameterize
+it with @args.
 
 =cut
 
 sub create_arged_type_constraint {
     my ($class, $name, @args) = @_;
     my $type_constraint = Moose::Util::TypeConstraints::find_or_create_type_constraint($name);
-       return $type_constraint->parameterize(@args)
+       return $type_constraint->parameterize(@args);
 }
 
 =head2 create_base_type_constraint ($name)
@@ -440,11 +452,36 @@ sub check_export_generator {
 
 =head1 CAVEATS
 
+The following are lists of gotcha's and their workarounds for developers coming
+from the standard string based type constraint names
+
+=head2 Uniqueness
+
 A library makes the types quasi-unique by prefixing their names with (by
 default) the library package name. If you're only using the type handler
 functions provided by MooseX::Types, you shouldn't ever have to use
 a type's actual full name.
 
+=head2 Argument separation ('=>' versus ',')
+
+The Perlop manpage has this to say about the '=>' operator: "The => operator is
+a synonym for the comma, but forces any word (consisting entirely of word
+characters) to its left to be interpreted as a string (as of 5.001). This
+includes words that might otherwise be considered a constant or function call."
+
+Due to this stringification, the following will NOT work as you might think:
+
+  subtype StrOrArrayRef => as Str|ArrayRef;
+  
+The 'StrOrArrayRef' will have it's stringification activated this causes the
+subtype to not be created.  Since the bareword type constraints are not strings
+you really should not try to treat them that way.  You will have to use the ','
+operator instead.  The author's of this package realize that all the L<Moose>
+documention and examples nearly uniformly use the '=>' version of the comma
+operator and this could be an issue if you are converting code.
+
+Patches welcome for discussion.
+    
 =head1 SEE ALSO
 
 L<Moose>, 
index 57827b2..cf3f517 100644 (file)
@@ -9,9 +9,14 @@ use Moose::Meta::TypeConstraint::Union;
 
 use overload(
     '""' => sub {
-        shift->__type_constraint->name;  
+        return shift->__type_constraint->name; 
     },
     '|' => sub {
+        
+        ## It's kind of ugly that we need to know about Union Types, but this
+        ## is needed for syntax compatibility.  Maybe someday we'll all just do
+        ## Or[Str,Str,Int]
+        
         my @tc = grep {ref $_} @_;
         my $union = Moose::Meta::TypeConstraint::Union->new(type_constraints=>\@tc);
         return Moose::Util::TypeConstraints::register_type_constraint($union);
@@ -49,11 +54,11 @@ sub new {
             croak "Argument must be ->isa('Moose::Meta::TypeConstraint') or ->isa('MooseX::Types::UndefinedType')";
         }
     } else {
-        croak "This method [new] requires a single argument";        
+        croak "This method [new] requires a single argument of 'arg'.";        
     }
 }
 
-=head type_constraint ($type_constraint)
+=head __type_constraint ($type_constraint)
 
 Set/Get the type_constraint.
 
@@ -74,7 +79,7 @@ handle $self->isa since AUTOLOAD can't.
 =cut
 
 sub isa {
-    my ($self, $target) = @_;
+    my ($self, $target) = @_; 
     if(defined $target) {
         return $self->__type_constraint->isa($target);
     } else {
index c225938..3f250e6 100644 (file)
@@ -2,7 +2,7 @@
 use warnings;
 use strict;
 
-use Test::More tests => 47;
+use Test::More tests => 49;
 use Test::Exception;
 use FindBin;
 use lib "$FindBin::Bin/lib";
@@ -16,7 +16,7 @@ use lib "$FindBin::Bin/lib";
     );
     use DecoratorLibrary qw(
         MyArrayRefBase MyArrayRefInt01 MyArrayRefInt02 StrOrArrayRef
-        AtLeastOneInt 
+        AtLeastOneInt Jobs
     );
     
     has 'arrayrefbase' => (is=>'rw', isa=>MyArrayRefBase, coerce=>1);
@@ -28,6 +28,7 @@ use lib "$FindBin::Bin/lib";
     has 'pipeoverloading' => (is=>'rw', isa=>Int|Str);   
     has 'deep' => (is=>'rw', isa=>ArrayRef[ArrayRef[HashRef[Int]]] );
     has 'deep2' => (is=>'rw', isa=>ArrayRef[Int|ArrayRef[HashRef[Int|Object]]] );
+    has 'enum' => (is=>'rw', isa=>Jobs);
 }
 
 ## Make sure we have a 'create object sanity check'
@@ -202,3 +203,13 @@ ok $type->deep2([1,2,3])
 
 is_deeply $type->deep2, [1,2,3],
  => 'Assignment is correct';
+## Test jobs
+
+ok $type->enum('Programming')
+ => 'Good Assignment of Programming to Enum';
+
+
+throws_ok sub {
+    $type->enum('ddddd');
+}, qr/Attribute \(enum\) does not pass the type constraint/ => 'Enum properly fails';
index 180a959..880b456 100644 (file)
@@ -10,6 +10,7 @@ use MooseX::Types
         MyHashRefOfStr
         StrOrArrayRef
         AtLeastOneInt
+        Jobs
     )];
 
 ## Some questionable messing around
@@ -68,5 +69,8 @@ subtype StrOrArrayRef,
 subtype AtLeastOneInt,
     as ArrayRef[Int],
     where { @$_ > 0 };
-
+    
+enum Jobs,
+    (qw/Programming Teaching Banking/);
+    
 1;