hackish impl of parameterized constraints, more examples (I think normal and paramete...
[gitmo/MooseX-Types.git] / lib / MooseX / Types.pm
index c93591f..1f40b7e 100644 (file)
@@ -1,4 +1,5 @@
 package MooseX::Types;
+use Moose;
 
 =head1 NAME
 
@@ -10,12 +11,11 @@ MooseX::Types - Organise your Moose types in libraries
 #use strict;
 
 use Moose::Util::TypeConstraints;
+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 Carp                            qw( croak );
-use Moose;
+use Carp::Clan                      qw( ^MooseX::Types );
 
 use namespace::clean -except => [qw( meta )];
 
@@ -162,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
@@ -266,19 +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
@@ -295,13 +301,55 @@ yet defined.
 =cut
 
 sub type_export_generator {
-    my ($class, $type, $full) = @_;
-    return sub { 
-        return find_type_constraint($full)
-            || MooseX::Types::UndefinedType->new($full);
+    my ($class, $type, $name) = @_;
+    return sub {
+        my $type_constraint;
+        if(my $params = shift @_) {
+            $type_constraint = $class->create_arged_type_constraint($name, @$params);
+        } else {
+            $type_constraint = $class->create_base_type_constraint($name)
+             || MooseX::Types::UndefinedType->new($name);           
+        }
+        return $class->create_type_decorator($type_constraint);
     };
 }
 
+=head2 create_arged_type_constraint ($name, @args)
+
+Given a String $name with @args find the matching typeconstraint.
+
+=cut
+
+sub create_arged_type_constraint {
+    my ($class, $name, @args) = @_;
+    ### This whole section is a real TODO :)  Ugly hack to get the base tests working.
+    my $fullname = $name."[$args[0]]";
+    return Moose::Util::TypeConstraints::create_parameterized_type_constraint($fullname);
+}
+
+=head2 create_base_type_constraint ($name)
+
+Given a String $name, find the matching typeconstraint.
+
+=cut
+
+sub create_base_type_constraint {
+    my ($class, $name) = @_;
+    return find_type_constraint($name);
+}
+
+=head2 create_type_decorator ($type_constraint)
+
+Given a $type_constraint, return a lightweight L<MooseX::Types::TypeDecorator>
+instance.
+
+=cut
+
+sub create_type_decorator {
+    my ($class, $type_constraint) = @_;
+    return MooseX::Types::TypeDecorator->new(type_constraint=>$type_constraint);
+}
+
 =head2 coercion_export_generator
 
 This generates a coercion handler function, e.g. C<to_Int($value)>. 
@@ -351,7 +399,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