[Catalyst-commits] r10359 -
Catalyst-Runtime/5.80/branches/component-tweaks/lib/Catalyst
caelum at dev.catalyst.perl.org
caelum at dev.catalyst.perl.org
Fri May 29 11:33:20 GMT 2009
Author: caelum
Date: 2009-05-29 11:33:19 +0000 (Fri, 29 May 2009)
New Revision: 10359
Modified:
Catalyst-Runtime/5.80/branches/component-tweaks/lib/Catalyst/Component.pm
Log:
make component BUILDARGS accept flat hashes
Modified: Catalyst-Runtime/5.80/branches/component-tweaks/lib/Catalyst/Component.pm
===================================================================
--- Catalyst-Runtime/5.80/branches/component-tweaks/lib/Catalyst/Component.pm 2009-05-29 10:39:18 UTC (rev 10358)
+++ Catalyst-Runtime/5.80/branches/component-tweaks/lib/Catalyst/Component.pm 2009-05-29 11:33:19 UTC (rev 10359)
@@ -8,6 +8,7 @@
use MRO::Compat;
use mro 'c3';
use Storable 'dclone';
+use Carp;
use namespace::clean -except => 'meta';
with 'MooseX::Emulate::Class::Accessor::Fast';
@@ -60,7 +61,21 @@
__PACKAGE__->mk_classdata('_config');
sub BUILDARGS {
- return ref($_[-1]) eq 'HASH' ? $_[-1] : {};
+ my $class = shift;
+
+ if (@_ == 1 && ref($_[0]) eq 'HASH') {
+ return $_[0];
+ } elsif (@_ == 2) { # is it ($app, $args) or foo => 'bar' ?
+ if (Class::MOP::is_class_loaded($_[0]) && $_[0]->isa('Catalyst')) {
+ return $_[1];
+ } else {
+ return +{ @_ };
+ }
+ } elsif (@_ % 2 == 0) {
+ return +{ @_ };
+ }
+
+ croak "invalid parameters";
}
sub COMPONENT {
@@ -96,8 +111,7 @@
my $class = blessed($self) || $self;
my $meta = Class::MOP::get_metaclass_by_name($class);
unless ($meta->has_package_symbol('$_config')) {
- $config = dclone $config;
- $self->_config( $config );
+ $self->_config( dclone $config );
}
}
return $self->_config
@@ -152,22 +166,13 @@
=head2 BUILDARGS
-By default gets C<$app, $args> as parameters.
+Processes args passed to C<< ->new >>, see L<Moose::Object>.
-Processes args passed to C<< ->new >>. By default returns C<$args>, see
-L<Moose::Object>.
+By default gets either C<$app, $args> or C<$args> as parameters.
If you override L</COMPONENT> to pass different types of parameters to
C<< ->new >>, override L</BUILDARGS> as well.
-By default looks like this:
-
- sub BUILDARGS {
- return ref($_[-1]) eq 'HASH' ? $_[-1] : {};
- }
-
-Because C<< ->new >> is not always passed C<$app>.
-
=head2 $c->config
=head2 $c->config($hashref)
More information about the Catalyst-commits
mailing list