From: Matt S Trout Date: Fri, 25 Feb 2011 16:19:02 +0000 (+0000) Subject: factor out eager default calculation X-Git-Tag: release_0.009006~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4ced3a946d97b0620f316923039897fd98256919;p=gitmo%2FRole-Tiny.git factor out eager default calculation Both constructor and accessor now need to test if an attribute has an eager default for their parts in generating the population code, so factor the logic out onto accessor to leave it pluggable for later. --- diff --git a/lib/Method/Generate/Accessor.pm b/lib/Method/Generate/Accessor.pm index d4112bf..76e2a54 100644 --- a/lib/Method/Generate/Accessor.pm +++ b/lib/Method/Generate/Accessor.pm @@ -143,6 +143,11 @@ sub is_simple_set { !grep $spec->{$_}, qw(isa trigger weak_ref); } +sub has_eager_default { + my ($self, $name, $spec) = @_; + (!$spec->{lazy} and ($spec->{default} or $spec->{builder})); +} + sub _generate_get { my ($self, $name, $spec) = @_; my $simple = $self->_generate_simple_get('$_[0]', $name); @@ -272,8 +277,7 @@ sub generate_populate_set { sub _generate_populate_set { my ($self, $me, $name, $spec, $source, $test) = @_; - if (!$spec->{lazy} and - ($spec->{default} or $spec->{builder})) { + if ($self->has_eager_default($name, $spec)) { my $get_indent = ' ' x ($spec->{isa} ? 6 : 4); my $get_default = $self->_generate_get_default( '$new', $_, $spec diff --git a/lib/Method/Generate/Constructor.pm b/lib/Method/Generate/Constructor.pm index 5d459b6..fdaa636 100644 --- a/lib/Method/Generate/Constructor.pm +++ b/lib/Method/Generate/Constructor.pm @@ -79,8 +79,7 @@ sub _assign_new { my $attr_spec = $spec->{$name}; unless ($ag->is_simple_attribute($name, $attr_spec)) { next NAME unless defined($attr_spec->{init_arg}) - or (($attr_spec->{default} or $attr_spec->{builder}) - and not $attr_spec->{lazy}); + or $ag->has_eager_default($name, $attr_spec); $test{$name} = $attr_spec->{init_arg}; next NAME; }