inlined_name which quotes
[gitmo/Mouse.git] / lib / Mouse / Meta / Attribute.pm
index 230efb5..051ebe3 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,6 +50,13 @@ 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;
 
@@ -59,49 +66,31 @@ sub generate_accessor {
     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 $trigger    = $attribute->trigger;
 
     my $accessor = 'sub {
         my $self = shift;';
 
     if ($attribute->_is_metadata eq 'rw') {
         $accessor .= 'if (@_) {
-            local $_ = $_[0];';
-
-        if ($before) {
-            $accessor .= '$before->($self, $_, $attribute);';
+        ';
+
+        if ($constraint) {
+            $accessor .= 'local $_ = $_[0];
+                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");
+            }'
         }
 
-        if ($around) {
-            $accessor .= '$around->(sub {
-                my $self = shift;
-                $_ = $_[0];
-            ';
-        }
-
-            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} = $_;';
+        $accessor .= '$self->{$key} = $_[0];';
 
-            if ($attribute->is_weak_ref) {
-                $accessor .= 'Scalar::Util::weaken($self->{$key}) if ref($self->{$key});';
-            }
-
-        if ($around) {
-            $accessor .= '}, $self, $_, $attribute);';
+        if ($attribute->is_weak_ref) {
+            $accessor .= 'weaken($self->{$key}) if ref($self->{$key});';
         }
 
-        if ($after) {
-            $accessor .= '$after->($self, $_, $attribute);';
+        if ($trigger) {
+            $accessor .= '$trigger->($self, $_[0], $attribute);';
         }
 
         $accessor .= '}';
@@ -138,7 +127,9 @@ sub generate_accessor {
     $accessor .= 'return $self->{$key};
     }';
 
-    return eval $accessor;
+    my $sub = eval $accessor;
+    confess $@ if $@;
+    return $sub;
 }
 
 sub generate_predicate {
@@ -147,16 +138,20 @@ sub generate_predicate {
 
     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 $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 +170,7 @@ sub generate_handles {
         }';
 
         $method_map{$local_method} = eval $method;
+        confess $@ if $@;
     }
 
     return \%method_map;
@@ -272,17 +268,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;