From: Fuji, Goro Date: Fri, 24 Sep 2010 02:49:47 +0000 (+0900) Subject: Fix apply_all_roles() X-Git-Tag: 0.71~14 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=80b463bbb96854d6e4ddffb338ee103443507e8b;p=gitmo%2FMouse.git Fix apply_all_roles() --- diff --git a/lib/Mouse/Meta/Role.pm b/lib/Mouse/Meta/Role.pm index 13490f6..7fa3260 100644 --- a/lib/Mouse/Meta/Role.pm +++ b/lib/Mouse/Meta/Role.pm @@ -78,8 +78,8 @@ sub combine { my $composite = Mouse::Meta::Role::Composite->create_anon_role(); foreach my $role_spec (@role_specs) { - my($role_name, $args) = @{$role_spec}; - $role_name->meta->apply($composite, %{$args}); + my($role, $args) = @{$role_spec}; + $role->apply($composite, %{$args}); } return $composite; } diff --git a/lib/Mouse/Util.pm b/lib/Mouse/Util.pm index 393bccf..38177d0 100644 --- a/lib/Mouse/Util.pm +++ b/lib/Mouse/Util.pm @@ -274,29 +274,37 @@ sub is_class_loaded; sub apply_all_roles { my $consumer = Scalar::Util::blessed($_[0]) - ? shift # instance - : Mouse::Meta::Class->initialize(shift); # class or role name + ? $_[0] # instance + : Mouse::Meta::Class->initialize($_[0]); # class or role name my @roles; # Basis of Data::OptList my $max = scalar(@_); - for (my $i = 0; $i < $max ; $i++) { - if ($i + 1 < $max && ref($_[$i + 1])) { - push @roles, [ $_[$i] => $_[++$i] ]; - } else { - push @roles, [ $_[$i] => undef ]; + for (my $i = 1; $i < $max ; $i++) { + my $role = $_[$i]; + my $role_name; + if(ref $role) { + $role_name = $role->name; + } + else { + $role_name = $role; + load_class($role_name); + $role = get_metaclass_by_name($role_name); } - my $role_name = $roles[-1][0]; - load_class($role_name); - is_a_metarole( get_metaclass_by_name($role_name) ) + if ($i + 1 < $max && ref($_[$i + 1]) eq 'HASH') { + push @roles, [ $role => $_[++$i] ]; + } else { + push @roles, [ $role => undef ]; + } + is_a_metarole($role) || $consumer->meta->throw_error("You can only consume roles, $role_name is not a Mouse role"); } if ( scalar @roles == 1 ) { - my ( $role_name, $params ) = @{ $roles[0] }; - get_metaclass_by_name($role_name)->apply( $consumer, defined $params ? $params : () ); + my ( $role, $params ) = @{ $roles[0] }; + $role->apply( $consumer, defined $params ? $params : () ); } else { Mouse::Meta::Role->combine(@roles)->apply($consumer);