set instance to attributes default code
[gitmo/Mouse.git] / lib / Mouse / Meta / Method / Constructor.pm
index 77cced9..2a9bf6c 100644 (file)
@@ -41,14 +41,14 @@ sub _generate_processattrs {
             my @code;
 
             if ($attr->should_coerce) {
-                push @code, "my \$value = \$attr->coerce_constraint( \$args->{'$from'});";
+                push @code, "my \$value = \$attrs[$index]->coerce_constraint( \$args->{'$from'});";
             }
             else {
                 push @code, "my \$value = \$args->{'$from'};";
             }
 
             if ($attr->has_type_constraint) {
-                push @code, "\$attr->verify_type_constraint( \$value );";
+                push @code, "\$attrs[$index]->verify_type_constraint( \$value );";
             }
 
             push @code, "\$instance->{'$key'} = \$value;";
@@ -58,7 +58,7 @@ sub _generate_processattrs {
             }
 
             if ( $attr->has_trigger ) {
-                push @code, "\$attr->trigger->( \$instance, \$value, \$attr );";
+                push @code, "\$attrs[$index]->trigger->( \$instance, \$value, \$attrs[$index] );";
             }
 
             join "\n", @code;
@@ -75,17 +75,22 @@ sub _generate_processattrs {
                     push @code, "my \$value = ";
 
                     if ($attr->should_coerce) {
-                        push @code, "\$attr->coerce_constraint(";
+                        push @code, "\$attrs[$index]->coerce_constraint(";
                     }
-
                         if ($attr->has_builder) {
                             push @code, "\$instance->$builder";
                         }
                         elsif (ref($default) eq 'CODE') {
-                            push @code, "\$attr->default()->()";
+                            push @code, "\$attrs[$index]->default()->(\$instance)";
+                        }
+                        elsif (!defined($default)) {
+                            push @code, 'undef';
+                        }
+                        elsif ($default =~ /^\-?[0-9]+(?:\.[0-9]+)$/) {
+                            push @code, $default;
                         }
                         else {
-                            push @code, "\$attr->default()";
+                            push @code, "'$default'";
                         }
 
                     if ($attr->should_coerce) {
@@ -96,7 +101,7 @@ sub _generate_processattrs {
                     }
 
                     if ($attr->has_type_constraint) {
-                        push @code, "\$attr->verify_type_constraint(\$value);";
+                        push @code, "\$attrs[$index]->verify_type_constraint(\$value);";
                     }
 
                     push @code, "\$instance->{'$key'} = \$value;";
@@ -117,11 +122,16 @@ sub _generate_processattrs {
         };
         my $code = <<"...";
             {
-                my \$attr = \$attrs[$index];
                 if (exists(\$args->{'$from'})) {
                     $set_value;
+...
+        if ($make_default_value) {
+            $code .= <<"...";
                 } else {
                     $make_default_value;
+...
+        }
+        $code .= <<"...";
                 }
             }
 ...