From: Matt S Trout Date: Thu, 3 May 2012 18:40:58 +0000 (+0000) Subject: regenerate accessors during role application if the accessor generator is non-standard X-Git-Tag: v0.091002~15 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a41e15c32a4553d493d9a44b3865280212c2f553;p=gitmo%2FMoo.git regenerate accessors during role application if the accessor generator is non-standard --- diff --git a/lib/Moo.pm b/lib/Moo.pm index 6d402ae..f4023cb 100644 --- a/lib/Moo.pm +++ b/lib/Moo.pm @@ -3,6 +3,7 @@ package Moo; use strictures 1; use Moo::_Utils; use B 'perlstring'; +use Sub::Defer (); our $VERSION = '0.091001'; # 0.91.1 $VERSION = eval $VERSION; diff --git a/lib/Moo/Role.pm b/lib/Moo/Role.pm index efc17e9..a8884de 100644 --- a/lib/Moo/Role.pm +++ b/lib/Moo/Role.pm @@ -58,27 +58,41 @@ sub _inhale_if_moose { } } +sub _maybe_make_accessors { + my ($self, $role, $target) = @_; + my $m; + if ($INFO{$role}{inhaled_from_moose} + or $m = Moo->_accessor_maker_for($target) + and ref($m) ne 'Method::Generate::Accessor') { + $self->_make_accessors($role, $target); + } +} + sub _make_accessors_if_moose { my ($self, $role, $target) = @_; if ($INFO{$role}{inhaled_from_moose}) { - if (my @attrs = @{$INFO{$role}{attributes}||[]}) { - my $acc_gen = ($Moo::MAKERS{$target}{accessor} ||= do { - require Method::Generate::Accessor; - Method::Generate::Accessor->new - }); - while (my ($name, $spec) = splice @attrs, 0, 2) { - $acc_gen->generate_method($target, $name, $spec); - } - } + $self->_make_accessors($role, $target); + } +} + +sub _make_accessors { + my ($self, $role, $target) = @_; + my $acc_gen = ($Moo::MAKERS{$target}{accessor} ||= do { + require Method::Generate::Accessor; + Method::Generate::Accessor->new + }); + my @attrs = @{$INFO{$role}{attributes}||[]}; + while (my ($name, $spec) = splice @attrs, 0, 2) { + $acc_gen->generate_method($target, $name, $spec); } } sub apply_single_role_to_package { my ($me, $to, $role) = @_; $me->_inhale_if_moose($role); - $me->_make_accessors_if_moose($role, $to); - $me->SUPER::apply_single_role_to_package($to, $role); $me->_handle_constructor($to, $INFO{$role}{attributes}); + $me->_maybe_make_accessors($role, $to); + $me->SUPER::apply_single_role_to_package($to, $role); } sub create_class_with_roles { @@ -131,6 +145,7 @@ sub _handle_constructor { # only fiddle with the constructor if the target is a Moo class if ($INC{"Moo.pm"} and my $con = Moo->_constructor_maker_for($to, $superclass)) { + # shallow copy of the specs since the constructor will assign an index $con->register_attribute_specs(map ref() ? { %$_ } : $_, @$attr_info); } }