added to coerce moose compat test case
[gitmo/Mouse.git] / lib / Mouse / Meta / Attribute.pm
index 72570c8..e0473a9 100644 (file)
@@ -36,6 +36,7 @@ sub type_constraint   { $_[0]->{type_constraint}  }
 sub trigger           { $_[0]->{trigger}          }
 sub builder           { $_[0]->{builder}          }
 sub should_auto_deref { $_[0]->{auto_deref}       }
+sub should_coerce     { $_[0]->{should_coerce}    }
 
 sub has_default         { exists $_[0]->{default}         }
 sub has_predicate       { exists $_[0]->{predicate}       }
@@ -60,12 +61,15 @@ sub inlined_name {
 sub generate_accessor {
     my $attribute = shift;
 
-    my $name       = $attribute->name;
-    my $default    = $attribute->default;
-    my $type       = $attribute->type_constraint;
-    my $constraint = $attribute->find_type_constraint;
-    my $builder    = $attribute->builder;
-    my $trigger    = $attribute->trigger;
+    my $name          = $attribute->name;
+    my $default       = $attribute->default;
+    my $type          = $attribute->type_constraint;
+    my $constraint    = $attribute->find_type_constraint;
+    my $builder       = $attribute->builder;
+    my $trigger       = $attribute->trigger;
+    my $is_weak       = $attribute->is_weak_ref;
+    my $should_deref  = $attribute->should_auto_deref;
+    my $should_coerce = $attribute->should_coerce;
 
     my $self  = '$_[0]';
     my $key   = $attribute->inlined_name;
@@ -77,21 +81,24 @@ sub generate_accessor {
         my $value = '$_[1]';
 
         if ($constraint) {
-            $accessor .= 'local $_ = '.$value.';
+            if ($should_coerce) {
+                $accessor .= $value.' = $attribute->coerce_constraint('.$value.');';
+            }
+            $accessor .= 'local $_ = '.$value.';';
+            $accessor .= '
                 unless ($constraint->()) {
                     my $display = defined($_) ? overload::StrVal($_) : "undef";
                     Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $display");
             }' . "\n"
         }
 
-        $accessor .= 'return '
-            if !$attribute->is_weak_ref
-            && !$trigger
-            && !$attribute->should_auto_deref;
+        # if there's nothing left to do for the attribute we can return during
+        # this setter
+        $accessor .= 'return ' if !$is_weak && !$trigger && !$should_deref;
 
         $accessor .= $self.'->{'.$key.'} = '.$value.';' . "\n";
 
-        if ($attribute->is_weak_ref) {
+        if ($is_weak) {
             $accessor .= 'weaken('.$self.'->{'.$key.'}) if ref('.$self.'->{'.$key.'});' . "\n";
         }
 
@@ -116,7 +123,7 @@ sub generate_accessor {
         $accessor .= ' if !exists '.$self.'->{'.$key.'};' . "\n";
     }
 
-    if ($attribute->should_auto_deref) {
+    if ($should_deref) {
         if ($attribute->type_constraint eq 'ArrayRef') {
             $accessor .= 'if (wantarray) {
                 return @{ '.$self.'->{'.$key.'} || [] };
@@ -190,6 +197,9 @@ sub create {
     %args = $self->canonicalize_args($name, %args);
     $self->validate_args($name, \%args);
 
+    $args{should_coerce} = delete $args{coerce}
+        if exists $args{coerce};
+
     $args{type_constraint} = delete $args{isa}
         if exists $args{isa};
 
@@ -290,7 +300,7 @@ sub find_type_constraint {
 
     return unless $type;
 
-    my $checker = Mouse::TypeRegistry->optimized_constraints->{$type};
+    my $checker = Mouse::TypeRegistry->optimized_constraints($self->associated_class->name)->{$type};
     return $checker if $checker;
 
     return sub { blessed($_) && blessed($_) eq $type };
@@ -311,6 +321,13 @@ sub verify_type_constraint {
     Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $display");
 }
 
+sub coerce_constraint {
+    my($self, $value) = @_;
+    my $type = $self->type_constraint
+        or return $value;
+    return Mouse::TypeRegistry->typecast_constraints($self->associated_class->name, $type, $value);
+}
+
 sub _canonicalize_handles {
     my $self    = shift;
     my $handles = shift;