cleanup synopsis example and finished coercion fix
John Napiorkowski [Thu, 24 Jun 2010 20:51:42 +0000 (16:51 -0400)]
README
lib/MooseX/Types/Parameterizable.pm
t/05-pod-examples.t

diff --git a/README b/README
index 3c233a2..4f5099d 100644 (file)
--- a/README
+++ b/README
@@ -2,43 +2,56 @@ NAME
     MooseX::Types::Parameterizable - Create your own Parameterizable Types.
 
 SYNOPSIS
-    Within your MooseX::Types declared library module:
+    The follow is example usage.
 
+        package Test::MooseX::Types::Parameterizable::Synopsis;
+
+        use Moose;
         use MooseX::Types::Parameterizable qw(Parameterizable);
-    
-        subtype Set,
-            as class_type("Set::Scalar");
+        use MooseX::Types::Moose qw(Str Int ArrayRef);
+        use MooseX::Types -declare=>[qw(Varchar)];
 
-        subtype UniqueInt,
-            as Parameterizable[Int, Set],
-            where {
-                my ($int, $set) = @_;
-                return !$set->has($int);
-            };
-        
-        subtype PositiveSet,
-            as Set,
-            where {
-                my ($set) = @_;
-                return !grep {$_ <0 } $set->members;
-            };
-        
-        subtype PositiveUniqueInt,
-            as UniqueInt[PositiveSet];
-    
-        my $set = Set::Scalar->new(1,2,3);
+        ## Create a type constraint that is a string but parameterizes an integer
+        ## that is used as a maximum length constraint on that string, similar to
+        ## an SQL Varchar type.
 
-        UniqueInt([$set])->check(100);  ## Okay, 100 isn't in (1,2,3)
-        UniqueInt([$set])->check(-99);  ## Okay, -99 isn't in (1,2,3)
-        UniqueInt([$set])->check(2);  ## Not OK, 2 is in (1,2,3)
-    
-        PositiveUniqueInt([$set])->check(100);  ## Okay, 100 isn't in (1,2,3)
-        PositiveUniqueInt([$set])->check(-99);  ## Not OK, -99 not Positive Int
-        PositiveUniqueInt([$set])->check(2);  ## Not OK, 2 is in (1,2,3)
-    
-        my $negative_set = Set::Scalar->new(-1,-2,-3);
-    
-        UniqueInt([$negative_set])->check(100);  ## Throws exception
+        subtype Varchar,
+          as Parameterizable[Str,Int],
+          where {
+            my($string, $int) = @_;
+            $int >= length($string) ? 1:0;
+          },
+          message { "'$_' is too long"  };
+
+        coerce Varchar,
+          from ArrayRef,
+          via { 
+            my ($arrayref, $int) = @_;
+            join('', @$arrayref);
+          };
+
+        has 'varchar_five' => (isa=>Varchar[5], is=>'ro', coerce=>1);
+        has 'varchar_ten' => (isa=>Varchar[10], is=>'ro');
+  
+        ## Object created since attributes are valid
+        my $object1 = __PACKAGE__->new(
+            varchar_five => '1234',
+            varchar_ten => '123456789',
+        );
+
+        ## Dies with an invalid constraint for 'varchar_five'
+        my $object2 = __PACKAGE__->new(
+            varchar_five => '12345678',
+            varchar_ten => '123456789',
+        );
+
+        ## varchar_five coerces as expected
+        my $object3 = __PACKAGE__->new(
+            varchar_five => [qw/aa bb/],
+            varchar_ten => '123456789',
+        );
+
+    See t/05-pod-examples.t for runnable versions of all POD code
 
 DESCRIPTION
     A MooseX::Types library for creating parameterizable types. A
index 861773b..b75924b 100644 (file)
@@ -17,36 +17,53 @@ MooseX::Types::Parameterizable - Create your own Parameterizable Types.
 
 The follow is example usage.
 
+    package Test::MooseX::Types::Parameterizable::Synopsis;
+
     use Moose;
     use MooseX::Types::Parameterizable qw(Parameterizable);
-    use MooseX::Types::Moose qw(Str Int);
+    use MooseX::Types::Moose qw(Str Int ArrayRef);
     use MooseX::Types -declare=>[qw(Varchar)];
 
+    ## Create a type constraint that is a string but parameterizes an integer
+    ## that is used as a maximum length constraint on that string, similar to
+    ## an SQL Varchar type.
+
     subtype Varchar,
       as Parameterizable[Str,Int],
       where {
         my($string, $int) = @_;
         $int >= length($string) ? 1:0;
       },
-      message {
-        "'$_' is too long"
+      message { "'$_' is too long"  };
+
+    coerce Varchar,
+      from ArrayRef,
+      via { 
+        my ($arrayref, $int) = @_;
+        join('', @$arrayref);
       };
 
-    has varchar_five => (isa=>Varchar[5], is=>'ro');
-    has varchar_ten => (isa=>Varchar[10], is=>'ro');
+    has 'varchar_five' => (isa=>Varchar[5], is=>'ro', coerce=>1);
+    has 'varchar_ten' => (isa=>Varchar[10], is=>'ro');
   
-    ## This works fine
+    ## Object created since attributes are valid
     my $object1 = __PACKAGE__->new(
         varchar_five => '1234',
         varchar_ten => '123456789',
     );
 
-    ## This explodes with a type constraint error
+    ## Dies with an invalid constraint for 'varchar_five'
     my $object2 = __PACKAGE__->new(
-        varchar_five => '12345678', ## Too long string
+        varchar_five => '12345678',
         varchar_ten => '123456789',
     );
 
+    ## varchar_five coerces as expected
+    my $object3 = __PACKAGE__->new(
+        varchar_five => [qw/aa bb/],
+        varchar_ten => '123456789',
+    );
 See t/05-pod-examples.t for runnable versions of all POD code
          
 =head1 DESCRIPTION
index e98bd45..e5ded22 100644 (file)
@@ -57,6 +57,14 @@ use Test::More;
 
     Test::More::ok $@, 'There was an error';
     Test::More::like $@, qr('12345678' is too long), 'Correct custom error';
+
+    my $object3 = __PACKAGE__->new(
+        varchar_five => [qw/aa bb/],
+        varchar_ten => '123456789',
+    );
+
+    Test::More::is $object3->varchar_five, 'aabb',
+      'coercion as expected';
 }
 
 done_testing;