Fix the timing triggers are invoked
gfx [Tue, 22 Sep 2009 08:22:24 +0000 (17:22 +0900)]
Changes
Makefile.PL
lib/Mouse/Meta/Class.pm
lib/Mouse/Meta/Method/Constructor.pm
t/200_examples/001_example.t

diff --git a/Changes b/Changes
index 7f31a1c..930ce5c 100644 (file)
--- a/Changes
+++ b/Changes
@@ -5,6 +5,8 @@ Revision history for Mouse
 
     * Fix Mouse::Util::find_meta() and Mouse::Util::does_role() (gfx)
 
+    * Fix the timing triggers are invoked (gfx)
+
     * Implement confliction checks in roles
 
     * work around create() and create_anon() in Mouse::Meta::Role
index 4fb0040..20396d5 100755 (executable)
@@ -91,8 +91,8 @@ sub create_moose_compatibility_test {
                         close $rfh;
                         $s;
                     };
-                    $src =~ s/Mouse::is_class_loaded/Class::MOP::is_class_loaded/g;
-                    $src =~ s/Mouse::load_class/Class::MOP::load_class/g;
+                    $src =~ s/Mouse::(?:Util::)?is_class_loaded/Class::MOP::is_class_loaded/g;
+                    $src =~ s/Mouse::(?:Util::)?load_class/Class::MOP::load_class/g;
                     $src =~ s/Mouse/Moose/g;
                     $src;
                 };
index 7084439..9ede6f8 100644 (file)
@@ -119,6 +119,8 @@ sub new_object {
 
     my $instance = bless {}, $self->name;
 
+    my @triggers_queue;
+
     foreach my $attribute ($self->get_all_attributes) {
         my $from = $attribute->init_arg;
         my $key  = $attribute->name;
@@ -134,7 +136,7 @@ sub new_object {
                 if ref($instance->{$key}) && $attribute->is_weak_ref;
 
             if ($attribute->has_trigger) {
-                $attribute->trigger->($instance, $args{$from});
+                push @triggers_queue, [ $attribute->trigger, $args{$from} ];
             }
         }
         else {
@@ -165,6 +167,12 @@ sub new_object {
             }
         }
     }
+
+    foreach my $trigger_and_value(@triggers_queue){
+        my($trigger, $value) = @{$trigger_and_value};
+        $trigger->($instance, $value);
+    }
+
     return $instance;
 }
 
index d205492..cff5fc3 100644 (file)
@@ -26,7 +26,7 @@ sub generate_constructor_method_inline {
 ...
 
     local $@;
-    # warn $code;
+    #warn $code;
     my $res = eval $code;
     die $@ if $@;
     $res;
@@ -61,20 +61,20 @@ sub _generate_processattrs {
                 }
                 $code .= "
                         \$attrs[$index]->verify_type_constraint_error(
-                            '$key', \$value, \$attrs[$index]->type_constraint
+                            q{$key}, \$value, \$attrs[$index]->type_constraint
                         )
                     }
                 ";
             }
 
-            $code .= "\$instance->{'$key'} = \$value;\n";
+            $code .= "\$instance->{q{$key}} = \$value;\n";
 
             if ($attr->is_weak_ref) {
-                $code .= "Scalar::Util::weaken( \$instance->{'$key'} ) if ref( \$value );\n";
+                $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref( \$value );\n";
             }
 
             if ($attr->has_trigger) {
-                $code .= "\$attrs[$index]->{trigger}->( \$instance, \$value );\n";
+                $code .= "push \@triggers, [\$attrs[$index]->{trigger}, \$value];\n";
             }
 
             $code .= "\n} else {\n";
@@ -117,15 +117,15 @@ sub _generate_processattrs {
                 if ($attr->has_type_constraint) {
                     $code .= "{
                         unless (\$attrs[$index]->{type_constraint}->check(\$value)) {
-                            \$attrs[$index]->verify_type_constraint_error('$key', \$value, \$attrs[$index]->type_constraint)
+                            \$attrs[$index]->verify_type_constraint_error(q{$key}, \$value, \$attrs[$index]->type_constraint)
                         }
                     }";
                 }
 
-                $code .= "\$instance->{'$key'} = \$value;\n";
+                $code .= "\$instance->{q{$key}} = \$value;\n";
 
                 if ($attr->is_weak_ref) {
-                    $code .= "Scalar::Util::weaken( \$instance->{'$key'} ) if ref( \$value );\n";
+                    $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref( \$value );\n";
                 }
             }
         }
@@ -138,7 +138,7 @@ sub _generate_processattrs {
         push @res, $code;
     }
 
-    return join "\n", @res;
+    return join "\n", q{my @triggers;}, @res, q{$_->[0]->($instance, $_->[1]) for @triggers;};
 }
 
 sub _generate_BUILDARGS {
index b4606c4..58264b0 100644 (file)
@@ -3,11 +3,17 @@
 use strict;
 use warnings;
 
-use Test::More tests => 20;
+use Test::More;
+BEGIN{
+    if(eval{ require Class::Method::Modifiers::Fast } || eval{ require Class::Method::Modifiers }){
+        plan tests => 20;
+    }
+    else{
+        plan skip_all => 'This test requires Class::Method::Modifiers(::Fast)?';
+    }
+}
 use Test::Exception;
 
-
-
 ## Roles
 
 {