more readable inlined code
[gitmo/Moose.git] / lib / Moose / Meta / TypeConstraint / Class.pm
index 0e8bae5..8e99e45 100644 (file)
@@ -4,6 +4,7 @@ use strict;
 use warnings;
 use metaclass;
 
+use B;
 use Scalar::Util 'blessed';
 use Moose::Util::TypeConstraints ();
 
@@ -13,28 +14,32 @@ __PACKAGE__->meta->add_attribute('class' => (
     reader => 'class',
 ));
 
+my $inliner = sub {
+    my $self = shift;
+    my $val  = shift;
+
+    return 'Scalar::Util::blessed(' . $val . ')'
+             . ' && ' . $val . '->isa(' . B::perlstring($self->class) . ')';
+};
+
 sub new {
     my ( $class, %args ) = @_;
 
-    $args{parent} = Moose::Util::TypeConstraints::find_type_constraint('Object');
-    my $self      = $class->_new(\%args);
+    $args{parent}
+        = Moose::Util::TypeConstraints::find_type_constraint('Object');
+
+    my $class_name = $args{class};
+    $args{constraint} = sub { $_[0]->isa($class_name) };
+
+    $args{inlined} = $inliner;
+
+    my $self = $class->_new( \%args );
 
-    $self->_create_hand_optimized_type_constraint;
     $self->compile_type_constraint();
 
     return $self;
 }
 
-sub _create_hand_optimized_type_constraint {
-    my $self = shift;
-    my $class = $self->class;
-    $self->hand_optimized_type_constraint(
-        sub {
-            blessed( $_[0] ) && $_[0]->isa($class)
-        }
-    );
-}
-
 sub parents {
     my $self = shift;
     return (