From: Alex J. G. BurzyƄski Date: Fri, 29 Jul 2011 16:20:36 +0000 (+0100) Subject: inline BUILDARGS X-Git-Tag: v0.009011~13 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0123201bb23a0510ae9ad5817a5138fc2eb0cb3e;p=gitmo%2FMoo.git inline BUILDARGS --- diff --git a/lib/Method/Generate/Constructor.pm b/lib/Method/Generate/Constructor.pm index 43be542..9190ea5 100644 --- a/lib/Method/Generate/Constructor.pm +++ b/lib/Method/Generate/Constructor.pm @@ -44,7 +44,8 @@ sub generate_method { local $self->{captures} = {}; my $body = ' my $class = shift;'."\n"; $body .= $self->_handle_subconstructor($into, $name); - if ($into->can('BUILDARGS') ) { + my $into_buildargs = $into->can('BUILDARGS'); + if ( $into_buildargs && $into_buildargs != \&Moo::Object::BUILDARGS ) { $body .= $self->_generate_args_via_buildargs; } else { $body .= $self->_generate_args; @@ -88,9 +89,27 @@ sub _generate_args_via_buildargs { q{ my $args = $class->BUILDARGS(@_);}."\n"; } +# inlined from Moo::Object - update that first. sub _generate_args { my ($self) = @_; - q{ my $args = ref($_[0]) eq 'HASH' ? $_[0] : { @_ };}."\n"; + return <<'_EOA'; + my $args; + if ( scalar @_ == 1 ) { + unless ( defined $_[0] && ref $_[0] eq 'HASH' ) { + die "Single parameters to new() must be a HASH ref" + ." data => ". $_[0] ."\n"; + } + $args = { %{ $_[0] } }; + } + elsif ( @_ % 2 ) { + die "The new() method for $class expects a hash reference or a key/value list." + . " You passed an odd number of arguments\n"; + } + else { + $args = {@_}; + } +_EOA + } sub _assign_new { diff --git a/lib/Moo/Object.pm b/lib/Moo/Object.pm index 09a4541..cc7fc06 100644 --- a/lib/Moo/Object.pm +++ b/lib/Moo/Object.pm @@ -18,6 +18,7 @@ sub new { }; } +# Inlined into Method::Generate::Constructor::_generate_args() - keep in sync sub BUILDARGS { my $class = shift; if ( scalar @_ == 1 ) { diff --git a/t/buildargs.t b/t/buildargs.t index 90cd9f8..f1e4c27 100644 --- a/t/buildargs.t +++ b/t/buildargs.t @@ -13,6 +13,31 @@ use Test::More; extends qw(Qux); } + +{ + package t::non_moo; + + sub new { + my ($class, $arg) = @_; + bless { attr => $arg }, $class; + } + + sub attr { shift->{attr} } + + package t::ext_non_moo::with_attr; + use Moo; + extends qw( t::non_moo ); + + has 'attr2' => ( is => 'ro' ); + + sub BUILDARGS { + my ( $class, @args ) = @_; + shift @args if @args % 2 == 1; + return { @args }; + } +} + + { package Foo; use Moo; @@ -97,5 +122,16 @@ foreach my $class (qw(Qux Quux)) { ); } +my $non_moo = t::non_moo->new( 'bar' ); +my $ext_non_moo = t::ext_non_moo::with_attr->new( 'bar', attr2 => 'baz' ); + +is $non_moo->attr, 'bar', + "non-moo accepts params"; +is $ext_non_moo->attr, 'bar', + "extended non-moo passes params"; +is $ext_non_moo->attr2, 'baz', + "extended non-moo has own attributes"; + + done_testing;