add TODO
[gitmo/Mouse.git] / lib / Mouse / Meta / Attribute.pm
index 230efb5..94bb535 100644 (file)
@@ -4,7 +4,7 @@ use strict;
 use warnings;
 
 use Carp 'confess';
-use Scalar::Util qw/blessed weaken/;
+use Mouse::Util qw/blessed weaken/;
 
 sub new {
     my $class = shift;
@@ -50,113 +50,114 @@ sub _create_args {
     $_[0]->{_create_args}
 }
 
+sub inlined_name {
+    my $self = shift;
+    my $name = $self->name;
+    my $key   = "'" . $name . "'";
+    return $key;
+}
+
 sub generate_accessor {
     my $attribute = shift;
 
-    my $name       = $attribute->name;
-    my $key        = $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 $before  = $trigger->{before};
-    my $after   = $trigger->{after};
-    my $around  = $trigger->{around};
+    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 $accessor = 'sub {
-        my $self = shift;';
+    my $self  = '$_[0]';
+    my $key   = $attribute->inlined_name;
 
+    my $accessor = "sub {\n";
     if ($attribute->_is_metadata eq 'rw') {
-        $accessor .= 'if (@_) {
-            local $_ = $_[0];';
+        $accessor .= 'if (scalar(@_) >= 2) {' . "\n";
 
-        if ($before) {
-            $accessor .= '$before->($self, $_, $attribute);';
-        }
+        my $value = '$_[1]';
 
-        if ($around) {
-            $accessor .= '$around->(sub {
-                my $self = shift;
-                $_ = $_[0];
-            ';
+        if ($constraint) {
+            $accessor .= 'local $_ = '.$value.';
+                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"
         }
 
-            if ($constraint) {
-                $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");
-                }'
-            }
-
-            $accessor .= '$self->{$key} = $_;';
+        # if there's nothing left to do for the attribute we can return during
+        # this setter
+        $accessor .= 'return ' if !$is_weak && !$trigger && !$should_deref;
 
-            if ($attribute->is_weak_ref) {
-                $accessor .= 'Scalar::Util::weaken($self->{$key}) if ref($self->{$key});';
-            }
+        $accessor .= $self.'->{'.$key.'} = '.$value.';' . "\n";
 
-        if ($around) {
-            $accessor .= '}, $self, $_, $attribute);';
+        if ($is_weak) {
+            $accessor .= 'weaken('.$self.'->{'.$key.'}) if ref('.$self.'->{'.$key.'});' . "\n";
         }
 
-        if ($after) {
-            $accessor .= '$after->($self, $_, $attribute);';
+        if ($trigger) {
+            $accessor .= '$trigger->('.$self.', '.$value.', $attribute);' . "\n";
         }
 
-        $accessor .= '}';
+        $accessor .= "}\n";
     }
     else {
-        $accessor .= 'confess "Cannot assign a value to a read-only accessor" if @_;';
+        $accessor .= 'confess "Cannot assign a value to a read-only accessor" if scalar(@_) >= 2;' . "\n";
     }
 
     if ($attribute->is_lazy) {
-        $accessor .= '$self->{$key} = ';
+        $accessor .= $self.'->{'.$key.'} = ';
 
         $accessor .= $attribute->has_builder
-                   ? '$self->$builder'
-                     : ref($default) eq 'CODE'
-                     ? '$default->($self)'
-                     : '$default';
-
-        $accessor .= ' if !exists($self->{$key});';
+                ? $self.'->$builder'
+                    : ref($default) eq 'CODE'
+                    ? '$default->('.$self.')'
+                    : '$default';
+        $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} || [] };
+                return @{ '.$self.'->{'.$key.'} || [] };
             }';
         }
         else {
             $accessor .= 'if (wantarray) {
-                return %{ $self->{$key} || {} };
+                return %{ '.$self.'->{'.$key.'} || {} };
             }';
         }
     }
 
-    $accessor .= 'return $self->{$key};
+    $accessor .= 'return '.$self.'->{'.$key.'};
     }';
 
-    return eval $accessor;
+    my $sub = eval $accessor;
+    confess $@ if $@;
+    return $sub;
 }
 
 sub generate_predicate {
     my $attribute = shift;
-    my $key = $attribute->name;
+    my $key = $attribute->inlined_name;
 
-    my $predicate = 'sub { exists($_[0]->{$key}) }';
+    my $predicate = 'sub { exists($_[0]->{'.$key.'}) }';
 
-    return eval $predicate;
+    my $sub = eval $predicate;
+    confess $@ if $@;
+    return $sub;
 }
 
 sub generate_clearer {
     my $attribute = shift;
-    my $key = $attribute->name;
+    my $key = $attribute->inlined_name;
 
-    my $predicate = 'sub { delete($_[0]->{$key}) }';
+    my $clearer = 'sub { delete($_[0]->{'.$key.'}) }';
 
-    return eval $predicate;
+    my $sub = eval $clearer;
+    confess $@ if $@;
+    return $sub;
 }
 
 sub generate_handles {
@@ -175,6 +176,7 @@ sub generate_handles {
         }';
 
         $method_map{$local_method} = eval $method;
+        confess $@ if $@;
     }
 
     return \%method_map;
@@ -272,17 +274,12 @@ sub validate_args {
         && $args->{isa} ne 'HashRef';
 
     if ($args->{trigger}) {
-        if (ref($args->{trigger}) eq 'CODE') {
-            $args->{trigger} = {
-                after => $args->{trigger},
-            };
-        }
-        elsif (ref($args->{trigger}) eq 'HASH') {
-            Carp::carp "HASH-based form of trigger is deprecated. Please switch back to using the coderef form of trigger.";
+        if (ref($args->{trigger}) eq 'HASH') {
+            Carp::carp "HASH-based form of trigger has been removed. Only the coderef form of triggers are now supported.";
         }
 
         confess "Trigger must be a CODE ref on attribute ($name)"
-            if ref($args->{trigger}) ne 'HASH';
+            if ref($args->{trigger}) ne 'CODE';
     }
 
     return 1;