+ - automatically generate constructors in subclasses when required so that
+ subclasses with a BUILD method but no attributes get it honoured
- add coerce handling
0.009008 - 2011-06-03
}
local $self->{captures} = {};
my $body = ' my $class = shift;'."\n";
+ $body .= $self->_handle_subconstructor($into, $name);
$body .= $self->_generate_args;
$body .= $self->_check_required($spec);
$body .= ' my $new = '.$self->construction_string.";\n";
;
}
+sub _handle_subconstructor {
+ my ($self, $into, $name) = @_;
+ if (my $gen = $self->{subconstructor_generator}) {
+ ' if ($class ne '.perlstring($into).') {'."\n".
+ ' '.$gen.";\n".
+ ' return $class->'.$name.'(@_)'.";\n".
+ ' }'."\n";
+ } else {
+ ''
+ }
+}
+
sub _cap_call {
my ($self, $code, $captures) = @_;
@{$self->{captures}}{keys %$captures} = values %$captures if $captures;
use strictures 1;
use Moo::_Utils;
+use B 'perlstring';
our $VERSION = '0.009008'; # 0.9.8
$VERSION = eval $VERSION;
$moo_constructor
? ($con ? $con->construction_string : undef)
: ('$class->'.$target.'::SUPER::new(@_)')
- )
+ ),
+ subconstructor_generator => (
+ $class.'->_constructor_maker_for($class,'.perlstring($target).')'
+ ),
)
->install_delayed
->register_attribute_specs(%{$con?$con->all_attribute_specs:{}})
sub BUILD { push @ran, 'Odd3' }
}
+{
+ package Sub1;
+ use Moo;
+ has 'foo' => (is => 'ro');
+ package Sub2;
+ use Moo;
+ extends 'Sub1';
+ sub BUILD { push @ran, "sub2" }
+}
+
my $o = Quux->new;
is(ref($o), 'Quux', 'object returned');
is(ref($o), 'Odd3', 'Odd3 object constructed');
is_deeply(\@ran, [ qw(Odd1 Odd3) ], 'BUILDs ran in order');
+@ran = ();
+
+$o = Sub2->new;
+
+is(ref($o), 'Sub2', 'Sub2 object constructed');
+is_deeply(\@ran, [ qw(sub2) ], 'BUILD ran');
+
done_testing;