From: Dave Rolsky Date: Tue, 26 Oct 2010 20:31:02 +0000 (-0500) Subject: Move lazy_build processing to its own sub and make it work for inherited attrs X-Git-Tag: 1.18~52 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4a8c7092b1338621f36644b62f825703cc9ebf8e;p=gitmo%2FMoose.git Move lazy_build processing to its own sub and make it work for inherited attrs --- diff --git a/lib/Moose/Meta/Attribute.pm b/lib/Moose/Meta/Attribute.pm index 5c3562e..1a9cbf5 100644 --- a/lib/Moose/Meta/Attribute.pm +++ b/lib/Moose/Meta/Attribute.pm @@ -212,6 +212,8 @@ sub clone_and_inherit_options { $options{traits} = \@all_traits if @all_traits; } + $self->_modify_attr_options_for_lazy_build( $self->name, \%options ); + $self->clone(%options); } @@ -331,20 +333,7 @@ sub _process_options { || $class->throw_error("You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute ($name)", data => $options); } - if (exists $options->{lazy_build} && $options->{lazy_build} == 1) { - $class->throw_error("You can not use lazy_build and default for the same attribute ($name)", data => $options) - if exists $options->{default}; - $options->{lazy} = 1; - $options->{builder} ||= "_build_${name}"; - if ($name =~ /^_/) { - $options->{clearer} ||= "_clear${name}"; - $options->{predicate} ||= "_has${name}"; - } - else { - $options->{clearer} ||= "clear_${name}"; - $options->{predicate} ||= "has_${name}"; - } - } + $class->_modify_attr_options_for_lazy_build( $name, $options ); if (exists $options->{lazy} && $options->{lazy}) { (exists $options->{default} || defined $options->{builder} ) @@ -357,6 +346,28 @@ sub _process_options { } +sub _modify_attr_options_for_lazy_build { + my ( $class, $name, $options ) = @_; + + return unless $options->{lazy_build}; + + $class->throw_error( + "You can not use lazy_build and default for the same attribute ($name)", + data => $options ) + if exists $options->{default}; + + $options->{lazy} = 1; + $options->{builder} ||= "_build_${name}"; + if ( $name =~ /^_/ ) { + $options->{clearer} ||= "_clear${name}"; + $options->{predicate} ||= "_has${name}"; + } + else { + $options->{clearer} ||= "clear_${name}"; + $options->{predicate} ||= "has_${name}"; + } +} + sub initialize_instance_slot { my ($self, $meta_instance, $instance, $params) = @_; my $init_arg = $self->init_arg();