X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMouse%2FAttribute.pm;h=1a971bfa4edd0a2006b86b48a3a9b6b3fb563d15;hb=010a2b16f8ed60a3dcca43e80ec75daee135447e;hp=2076297b39606e487838d7a5bf2f2aeec5ebddb7;hpb=af745d5a30ceb45518de556943e95dfdc03f451a;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Attribute.pm b/lib/Mouse/Attribute.pm index 2076297..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'; @@ -147,7 +152,8 @@ sub create { $args{handles} = { $self->_canonicalize_handles($args{handles}) } if $args{handles}; - $args{type_constraint} = delete $args{isa}; + $args{type_constraint} = delete $args{isa} + if exists $args{isa}; my $attribute = $self->new(%args, name => $name, class => $class); my $meta = $class->meta; @@ -301,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 Ces. + =cut