Make sure that eager builder does not run after clearer
[gitmo/Mouse.git] / lib / Mouse / Attribute.pm
index 0bc6a20..1a971bf 100644 (file)
@@ -49,6 +49,7 @@ sub generate_accessor {
     my $trigger    = $attribute->trigger;
     my $type       = $attribute->type_constraint;
     my $constraint = $attribute->find_type_constraint;
+    my $builder    = $attribute->builder;
 
     my $accessor = 'sub {
         my $self = shift;';
@@ -81,9 +82,13 @@ sub generate_accessor {
 
     if ($attribute->is_lazy) {
         $accessor .= '$self->{$key} = ';
-        $accessor .= ref($default) eq 'CODE'
-                   ? '$default->($self)'
-                   : '$default';
+
+        $accessor .= $attribute->has_builder
+                   ? '$self->$builder'
+                     : ref($default) eq 'CODE'
+                     ? '$default->($self)'
+                     : '$default';
+
         $accessor .= ' if !exists($self->{$key});';
     }
 
@@ -135,7 +140,7 @@ sub create {
     my ($self, $class, $name, %args) = @_;
 
     confess "You must specify a default for lazy attribute '$name'"
-        if $args{lazy} && !exists($args{default});
+        if $args{lazy} && !exists($args{default}) && !exists($args{builder});
 
     confess "Trigger is not allowed on read-only attribute '$name'"
         if $args{trigger} && $args{is} ne 'rw';
@@ -302,5 +307,15 @@ Creates a new code reference for the attribute's clearer.
 
 Creates a new code reference for each of the attribute's handles methods.
 
+=head2 find_type_constraint -> CODE
+
+Returns a code reference which can be used to check that a given value passes
+this attribute's type constraint;
+
+=head2 verify_type_constraint Item -> 1 | ERROR
+
+Checks that the given value passes this attribute's type constraint. Returns 1
+on success, otherwise C<confess>es.
+
 =cut