X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMouse%2FAttribute.pm;fp=lib%2FMouse%2FAttribute.pm;h=1a971bfa4edd0a2006b86b48a3a9b6b3fb563d15;hb=9367e029d69af6065f843df198c0631a91834c95;hp=dd4a7aee9075e2cb05b9fe024a593864dab16b0f;hpb=6feb83f15e8242426f088e25e4adb37a6d8698ad;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Attribute.pm b/lib/Mouse/Attribute.pm index dd4a7ae..1a971bf 100644 --- a/lib/Mouse/Attribute.pm +++ b/lib/Mouse/Attribute.pm @@ -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';