Use methods on attribute instead of poking directly in the hash
Shawn M Moore [Tue, 10 Jun 2008 01:11:26 +0000 (01:11 +0000)]
lib/Mouse/Attribute.pm
lib/Mouse/Object.pm

index 4bb1856..923bff2 100644 (file)
@@ -18,16 +18,18 @@ sub new {
 
 sub name            { $_[0]->{name}            }
 sub class           { $_[0]->{class}           }
+sub required        { $_[0]->{required}        }
 sub default         { $_[0]->{default}         }
+sub lazy            { $_[0]->{lazy}            }
 sub predicate       { $_[0]->{predicate}       }
 sub clearer         { $_[0]->{clearer}         }
 sub handles         { $_[0]->{handles}         }
 sub weak_ref        { $_[0]->{weak_ref}        }
 sub init_arg        { $_[0]->{init_arg}        }
 sub type_constraint { $_[0]->{type_constraint} }
+sub trigger         { $_[0]->{trigger}         }
+sub builder         { $_[0]->{builder}         }
 
-sub has_name            { exists $_[0]->{name}            }
-sub has_class           { exists $_[0]->{class}           }
 sub has_default         { exists $_[0]->{default}         }
 sub has_predicate       { exists $_[0]->{predicate}       }
 sub has_clearer         { exists $_[0]->{clearer}         }
@@ -35,6 +37,8 @@ sub has_handles         { exists $_[0]->{handles}         }
 sub has_weak_ref        { exists $_[0]->{weak_ref}        }
 sub has_init_arg        { exists $_[0]->{init_arg}        }
 sub has_type_constraint { exists $_[0]->{type_constraint} }
+sub has_trigger         { exists $_[0]->{trigger}         }
+sub has_builder         { exists $_[0]->{builder}         }
 
 sub generate_accessor {
     my $attribute = shift;
index 1dd5481..8564939 100644 (file)
@@ -17,27 +17,29 @@ sub new {
         my $default;
 
         if (!exists($args{$key})) {
-            if (exists($attribute->{default}) || exists($attribute->{builder})) {
-                unless ($attribute->{lazy}) {
-                    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_default || $attribute->has_builder) {
+                my $default = $attribute->default;
+
+                unless ($attribute->lazy) {
+                    my $builder = $attribute->builder;
+                    my $value = $attribute->has_builder
+                              ? $instance->$builder
+                              : ref($default) eq 'CODE'
+                                  ? $default->()
+                                  : $default;
+
+                    $attribute->verify_type_constraint($value)
                         if $attribute->has_type_constraint;
 
-                    $instance->{$key} = $default;
+                    $instance->{$key} = $value;
 
                     Scalar::Util::weaken($instance->{$key})
-                        if $attribute->{weak_ref};
+                        if $attribute->weak_ref;
                 }
             }
             else {
-                if ($attribute->{required}) {
-                    confess "Attribute '$attribute->{name}' is required";
+                if ($attribute->required) {
+                    confess "Attribute '".$attribute->name."' is required";
                 }
             }
         }
@@ -49,10 +51,10 @@ sub new {
             $instance->{$key} = $args{$key};
 
             Scalar::Util::weaken($instance->{$key})
-                if $attribute->{weak_ref};
+                if $attribute->weak_ref;
 
-            if ($attribute->{trigger}) {
-                $attribute->{trigger}->($instance, $args{$key}, $attribute);
+            if ($attribute->has_trigger) {
+                $attribute->trigger->($instance, $args{$key}, $attribute);
             }
         }
     }