X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMouse%2FAttribute.pm;h=c086b407faebd3f1e9bde4e814ab6dafbb0541ca;hb=8473108059ddb46644a679a32f6e9bae8d9fd73c;hp=dd4a7aee9075e2cb05b9fe024a593864dab16b0f;hpb=fb706f5c1e60ddafe873717be7209066d47b8998;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Attribute.pm b/lib/Mouse/Attribute.pm index dd4a7ae..c086b40 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;'; @@ -77,13 +78,18 @@ sub generate_accessor { $accessor .= '}'; } else { + $accessor .= 'confess "Cannot assign a value to a read-only accessor" if @_;'; } 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});'; } @@ -134,11 +140,8 @@ sub generate_handles { sub create { my ($self, $class, $name, %args) = @_; - confess "You must specify a default for lazy attribute '$name'" - if $args{lazy} && !exists($args{default}); - - confess "Trigger is not allowed on read-only attribute '$name'" - if $args{trigger} && $args{is} ne 'rw'; + confess "You cannot have lazy attribute ($name) without specifying a default value for it" + if $args{lazy} && !exists($args{default}) && !exists($args{builder}); confess "References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])" if ref($args{default})