Add support for an "around" trigger in the setter
[gitmo/Mouse.git] / lib / Mouse / Meta / Attribute.pm
index 023ab4d..4bdb9f8 100644 (file)
@@ -56,11 +56,15 @@ sub generate_accessor {
     my $name       = $attribute->name;
     my $key        = $name;
     my $default    = $attribute->default;
-    my $trigger    = $attribute->trigger;
     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 $accessor = 'sub {
         my $self = shift;';
 
@@ -68,21 +72,36 @@ sub generate_accessor {
         $accessor .= 'if (@_) {
             local $_ = $_[0];';
 
-        if ($constraint) {
-            $accessor .= 'do {
-                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") unless $constraint->();
-            };'
+        if ($before) {
+            $accessor .= '$before->($self, $_, $attribute);';
+        }
+
+        if ($around) {
+            $accessor .= '$around->(sub {
+                my $self = shift;
+                $_ = $_[0];
+            ';
         }
 
-        $accessor .= '$self->{$key} = $_;';
+            if ($constraint) {
+                $accessor .= 'do {
+                    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") unless $constraint->();
+                };'
+            }
+
+            $accessor .= '$self->{$key} = $_;';
+
+            if ($attribute->is_weak_ref) {
+                $accessor .= 'Scalar::Util::weaken($self->{$key}) if ref($self->{$key});';
+            }
 
-        if ($attribute->is_weak_ref) {
-            $accessor .= 'Scalar::Util::weaken($self->{$key}) if ref($self->{$key});';
+        if ($around) {
+            $accessor .= '}, $self, $_, $attribute);';
         }
 
-        if ($trigger) {
-            $accessor .= '$trigger->($self, $_, $attribute);';
+        if ($after) {
+            $accessor .= '$after->($self, $_, $attribute);';
         }
 
         $accessor .= '}';