builder now called in the constructor
[gitmo/Mouse.git] / lib / Mouse / Object.pm
index 82cbec0..1dd5481 100644 (file)
@@ -17,18 +17,22 @@ sub new {
         my $default;
 
         if (!exists($args{$key})) {
-            if (exists($attribute->{default})) {
+            if (exists($attribute->{default}) || exists($attribute->{builder})) {
                 unless ($attribute->{lazy}) {
-                    if (ref($attribute->{default}) eq 'CODE') {
-                        $instance->{$key} = $attribute->{default}->();
-                        Scalar::Util::weaken($instance->{$key})
-                            if $attribute->{weak_ref};
-                    }
-                    else {
-                        $instance->{$key} = $attribute->{default};
-                        Scalar::Util::weaken($instance->{$key})
-                            if $attribute->{weak_ref};
-                    }
+                    my $builder = $attribute->{builder};
+                    my $default = exists($attribute->{builder})
+                                ? $instance->$builder
+                                : ref($attribute->{default}) eq 'CODE'
+                                    ? $attribute->{default}->()
+                                    : $attribute->{default};
+
+                    $attribute->verify_type_constraint($default)
+                        if $attribute->has_type_constraint;
+
+                    $instance->{$key} = $default;
+
+                    Scalar::Util::weaken($instance->{$key})
+                        if $attribute->{weak_ref};
                 }
             }
             else {
@@ -39,7 +43,11 @@ sub new {
         }
 
         if (exists($args{$key})) {
+            $attribute->verify_type_constraint($args{$key})
+                if $attribute->has_type_constraint;
+
             $instance->{$key} = $args{$key};
+
             Scalar::Util::weaken($instance->{$key})
                 if $attribute->{weak_ref};