ugly proof of concept for parametrized types only
[gitmo/MooseX-Types.git] / lib / MooseX / Types.pm
index 853b035..5b64c50 100644 (file)
@@ -1,4 +1,5 @@
 package MooseX::Types;
+use Moose;
 
 =head1 NAME
 
@@ -9,26 +10,24 @@ MooseX::Types - Organise your Moose types in libraries
 #use warnings;
 #use strict;
 
-use Sub::Uplevel;
 use Moose::Util::TypeConstraints;
-use MooseX::Types::Base           ();
-use MooseX::Types::Util           qw( filter_tags );
+use MooseX::Types::TypeDecorator;
+use MooseX::Types::Base             ();
+use MooseX::Types::Util             qw( filter_tags );
 use MooseX::Types::UndefinedType;
-use Sub::Install                        qw( install_sub );
-use Moose;
-use namespace::clean;
+use Carp::Clan                      qw( ^MooseX::Types );
+
+use namespace::clean -except => [qw( meta )];
 
-our $VERSION = 0.01;
+our $VERSION = 0.05;
 
 my $UndefMsg = q{Action for type '%s' not yet defined in library '%s'};
 
 =head1 SYNOPSIS
 
-  #
-  # Library Definition
-  #
+=head2 Library Definition
+
   package MyLibrary;
-  use strict;
 
   # predeclare our own types
   use MooseX::Types 
@@ -55,9 +54,8 @@ my $UndefMsg = q{Action for type '%s' not yet defined in library '%s'};
 
   1;
 
-  #
-  # Usage
-  #
+=head2 Usage
+
   package Foo;
   use Moose;
   use MyLibrary qw( PositiveInt NegativeInt );
@@ -146,6 +144,12 @@ library which can export all types that come with Moose.
 You will have to define coercions for your types or your library won't
 export a L</to_$type> coercion helper for it.
 
+Note that you currently cannot define types containing C<::>, since 
+exporting would be a problem.
+
+You also don't need to use C<warnings> and C<strict>, since the
+definition of a library automatically exports those.
+
 =head1 LIBRARY USAGE
 
 You can import the L<"type helpers"|/"TYPE HANDLER FUNCTIONS"> of a
@@ -158,6 +162,12 @@ you want all of them, use the C<:all> tag. For example:
 MooseX::Types comes with a library of Moose' built-in types called
 L<MooseX::Types::Moose>.
 
+The exporting mechanism is, since version 0.5, implemented via a wrapper
+around L<Sub::Exporter>. This means you can do something like this:
+
+  use MyLibrary TypeA => { -as => 'MyTypeA' },
+                TypeB => { -as => 'MyTypeB' };
+
 =head1 WRAPPING A LIBRARY
 
 You can define your own wrapper subclasses to manipulate the behaviour
@@ -250,6 +260,10 @@ sub import {
     my ($class, %args) = @_;
     my  $callee = caller;
 
+    # everyone should want this
+    strict->import;
+    warnings->import;
+
     # inject base class into new library
     {   no strict 'refs';
         unshift @{ $callee . '::ISA' }, 'MooseX::Types::Base';
@@ -258,15 +272,19 @@ sub import {
     # generate predeclared type helpers
     if (my @orig_declare = @{ $args{ -declare } || [] }) {
         my ($tags, $declare) = filter_tags @orig_declare;
+        my @to_export;
 
         for my $type (@$declare) {
+
+            croak "Cannot create a type containing '::' ($type) at the moment"
+                if $type =~ /::/;
+
+            # add type to library and remember to export
             $callee->add_type($type);
-            $callee->export_type_into(
-                $callee, $type, 
-                sprintf($UndefMsg, $type, $callee), 
-                -full => 1,
-            );
+            push @to_export, $type;
         }
+
+        $callee->import({ -full => 1, -into => $callee }, @to_export);
     }
 
     # run type constraints import
@@ -282,11 +300,22 @@ yet defined.
 
 =cut
 
+use Data::Dump qw/dump/;
+
 sub type_export_generator {
     my ($class, $type, $full) = @_;
-    return sub { 
-        return find_type_constraint($full)
-            || MooseX::Types::UndefinedType->new($full);
+    return sub {
+        ## todo, this needs to be some sort of ->process_args on the actual
+        ## containing type constraints.  This is ugly proof of concept
+        if(my $param = shift @_) {
+            #my @tc_args = map { find_type_constraint($full) } @args;
+            $full = $full .'['.  $param->[0]->name .']';
+        }
+        
+        my $type_constraint = find_type_constraint($full)
+         || MooseX::Types::UndefinedType->new($full);
+
+        return MooseX::Types::TypeDecorator->new(type_constraint=>$type_constraint);
     };
 }
 
@@ -339,7 +368,10 @@ a type's actual full name.
 
 =head1 SEE ALSO
 
-L<Moose>, L<Moose::Util::TypeConstraints>, L<MooseX::Types::Moose>
+L<Moose>, 
+L<Moose::Util::TypeConstraints>, 
+L<MooseX::Types::Moose>,
+L<Sub::Exporter>
 
 =head1 AUTHOR AND COPYRIGHT