From: Matt S Trout Date: Wed, 20 Jan 2010 14:56:54 +0000 (+0000) Subject: skeleton BUILDALL support X-Git-Tag: v0.003~8 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FWeb-Simple.git;a=commitdiff_plain;h=14ebaf8a1b35305acf1f02e466e19489ab10de1e skeleton BUILDALL support --- diff --git a/lib/Web/Simple/Application.pm b/lib/Web/Simple/Application.pm index 99a59a3..c115e6e 100644 --- a/lib/Web/Simple/Application.pm +++ b/lib/Web/Simple/Application.pm @@ -61,7 +61,25 @@ use warnings FATAL => 'all'; sub new { my ($class, $data) = @_; my $config = { $class->_default_config, %{($data||{})->{config}||{}} }; - bless({ config => $config }, $class); + my $new = bless({ config => $config }, $class); + $new->BUILDALL($data); + $new; +} + +sub BUILDALL { + my ($self, $data) = @_; + my $targ = ref($self); + my @targ; + while ($targ->isa(__PACKAGE__) and $targ ne __PACKAGE__) { + push(@targ, "${targ}::BUILD") + if do { no strict 'refs'; defined *{"${targ}::BUILD"}{CODE} }; + my @targ_isa = do { no strict 'refs'; @{"${targ}::ISA"} }; + die "${targ} uses Multiple Inheritance: ISA is: ".join ', ', @targ_isa + if @targ_isa > 1; + $targ = $targ_isa[0]; + } + $self->$_($data) for reverse @targ; + return; } sub _setup_default_config { diff --git a/t/buildall.t b/t/buildall.t new file mode 100644 index 0000000..8d676ab --- /dev/null +++ b/t/buildall.t @@ -0,0 +1,23 @@ +use Test::More 'no_plan'; + +use Web::Simple 'Fork'; + +my @run; + +sub Fork::BUILD { push @run, [ FORK => $_[1] ] } + +@Knife::ISA = 'Fork'; + +@Spoon::ISA = 'Knife'; + +sub Spoon::BUILD { push @run, [ SPOON => $_[1] ] } + +bless({}, 'Fork')->BUILDALL('data'); + +is_deeply(\@run, [ [ FORK => 'data' ] ], 'Single class ok'); + +@run = (); + +bless({}, 'Spoon')->BUILDALL('data'); + +is_deeply(\@run, [ [ FORK => 'data' ], [ SPOON => 'data' ] ], 'Subclass ok');