When generating inlining code, distinguish between $default from calling $attr->defau...
[gitmo/Moose.git] / lib / Moose / Meta / TypeConstraint / Parameterizable.pm
index f216ec4..aa8cf2d 100644 (file)
@@ -4,17 +4,22 @@ use strict;
 use warnings;
 use metaclass;
 
-our $VERSION   = '0.87';
-$VERSION = eval $VERSION;
-our $AUTHORITY = 'cpan:STEVAN';
-
 use base 'Moose::Meta::TypeConstraint';
 use Moose::Meta::TypeConstraint::Parameterized;
 use Moose::Util::TypeConstraints ();
 
+use Carp 'confess';
+
 __PACKAGE__->meta->add_attribute('constraint_generator' => (
     accessor  => 'constraint_generator',
     predicate => 'has_constraint_generator',
+    Class::MOP::_definition_context(),
+));
+
+__PACKAGE__->meta->add_attribute('inline_generator' => (
+    accessor  => 'inline_generator',
+    predicate => 'has_inline_generator',
+    Class::MOP::_definition_context(),
 ));
 
 sub generate_constraint_for {
@@ -42,6 +47,16 @@ sub _can_coerce_constraint_from {
     };
 }
 
+sub generate_inline_for {
+    my ($self, $type, $val) = @_;
+
+    confess "Can't generate an inline constraint for $type, since none "
+          . "was defined"
+        unless $self->has_inline_generator;
+
+    return '( do { ' . $self->inline_generator->( $self, $type, $val ) . ' } )';
+}
+
 sub _parse_type_parameter {
     my ($self, $type_parameter) = @_;
     return Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($type_parameter);
@@ -67,9 +82,10 @@ sub parameterize {
     if ( $contained_tc->isa('Moose::Meta::TypeConstraint') ) {
         my $tc_name = $self->name . '[' . $contained_tc->name . ']';
         return Moose::Meta::TypeConstraint::Parameterized->new(
-            name           => $tc_name,
-            parent         => $self,
-            type_parameter => $contained_tc,
+            name               => $tc_name,
+            parent             => $self,
+            type_parameter     => $contained_tc,
+            parameterized_from => $self,
         );
     }
     else {
@@ -81,15 +97,13 @@ sub parameterize {
 
 1;
 
+# ABSTRACT: Type constraints which can take a parameter (ArrayRef)
+
 __END__
 
 
 =pod
 
-=head1 NAME
-
-Moose::Meta::TypeConstraint::Parameterizable - Type constraints which can take a parameter (ArrayRef)
-
 =head1 DESCRIPTION
 
 This class represents a parameterizable type constraint. This is a
@@ -114,21 +128,6 @@ confusing and needs some work.
 
 =head1 BUGS
 
-All complex software has bugs lurking in it, and this module is no
-exception. If you find a bug please either email me, or add the bug
-to cpan-RT.
-
-=head1 AUTHOR
-
-Stevan Little E<lt>stevan@iinteractive.comE<gt>
-
-=head1 COPYRIGHT AND LICENSE
-
-Copyright 2006-2009 by Infinity Interactive, Inc.
-
-L<http://www.iinteractive.com>
-
-This library is free software; you can redistribute it and/or modify
-it under the same terms as Perl itself.
+See L<Moose/BUGS> for details on reporting bugs.
 
 =cut