From: Graham Knop Date: Thu, 27 Jun 2013 10:52:31 +0000 (-0400) Subject: fix extending a constructor generator X-Git-Tag: v1.003000~25 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=6b3fe0921837da22cfb5398565a8840cd70b5d0a;p=gitmo%2FMoo.git fix extending a constructor generator --- diff --git a/lib/Method/Generate/Constructor.pm b/lib/Method/Generate/Constructor.pm index 165df9c..c116eff 100644 --- a/lib/Method/Generate/Constructor.pm +++ b/lib/Method/Generate/Constructor.pm @@ -5,6 +5,7 @@ use Sub::Quote; use base qw(Moo::Object); use Sub::Defer; use B 'perlstring'; +use Moo::_Utils qw(_getstash); sub register_attribute_specs { my ($self, @new_specs) = @_; @@ -44,9 +45,13 @@ sub accessor_generator { sub construction_string { my ($self) = @_; $self->{construction_string} - or 'bless(' - .$self->accessor_generator->default_construction_string - .', $class);' + ||= $self->_build_construction_string; +} + +sub _build_construction_string { + 'bless(' + .$_[0]->accessor_generator->default_construction_string + .', $class);' } sub install_delayed { @@ -178,4 +183,16 @@ sub _check_required { ." }\n"; } +use Moo; +Moo->_constructor_maker_for(__PACKAGE__)->register_attribute_specs( + attribute_specs => { + is => 'ro', + reader => 'all_attribute_specs', + }, + accessor_generator => { is => 'ro' }, + construction_string => { is => 'lazy' }, + subconstructor_handler => { is => 'ro' }, + package => { is => 'ro' }, +); + 1; diff --git a/t/extend-constructor.t b/t/extend-constructor.t new file mode 100644 index 0000000..bae3a15 --- /dev/null +++ b/t/extend-constructor.t @@ -0,0 +1,29 @@ +use strictures 1; +use Test::More; +use Test::Fatal; + +BEGIN { + package Role::For::Constructor; + use Moo::Role; + has extra_param => (is => 'ro'); +} +BEGIN { + package Some::Class; + use Moo; + BEGIN { + my $con = Moo->_constructor_maker_for(__PACKAGE__); + Moo::Role->apply_roles_to_object($con, 'Role::For::Constructor'); + } +} + +{ + package Some::SubClass; + use Moo; + extends 'Some::Class'; + + ::is(::exception { + has bar => (is => 'ro'); + }, undef, 'extending constructor generator works'); +} + +done_testing;