- Fix metaclass incompatibility errors when extending a vanilla perl
class which isa Moose class with a metaclass role applied (t0m)
+ * Moose::Meta::Role
+ - Add a role-combination hook, role_for_combination, for the
+ benefit of MooseX::Role::Parameterized (Sartak)
+
0.77 Sat, May 2, 2009
* Moose::Meta::Role
- Add explicit use of Devel::GlobalDestruction and Sub::Name
}
}
+sub role_for_combination {
+ my ($self, $params) = @_;
+ return $self;
+}
+
sub combine {
my ($class, @role_specs) = @_;
my (@roles, %role_params);
while (@role_specs) {
- my ($role, $params) = @{ splice @role_specs, 0, 1 };
- push @roles => Class::MOP::class_of($role);
+ my ($role_name, $params) = @{ splice @role_specs, 0, 1 };
+ my $requested_role = Class::MOP::class_of($role_name);
+
+ my $actual_role = $requested_role->role_for_combination($params);
+ push @roles => $actual_role;
+
next unless defined $params;
- $role_params{$role} = $params;
+ $role_params{$actual_role->name} = $params;
}
my $c = Moose::Meta::Role::Composite->new(roles => \@roles);
The return value is a new L<Moose::Meta::Role::Composite> that
represents the combined roles.
+=item B<< Moose::Meta::Role->role_for_combination($options) >>
+
+This is a hook for incorporating role-combination parameters. This
+method returns a role metaobject (by default the invocant role) to be
+used for the combination.
+
=item B<< Moose::Meta::Role->create($name, %options) >>
This method is identical to the L<Moose::Meta::Class> C<create>
--- /dev/null
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More tests => 3;
+
+my $OPTS;
+do {
+ package My::Singleton::Role;
+ use Moose::Role;
+
+ sub foo { 'My::Singleton::Role' }
+
+ package My::Role::Metaclass;
+ use Moose;
+ BEGIN { extends 'Moose::Meta::Role' };
+
+ sub role_for_combination {
+ my ($self, $opts) = @_;
+ $OPTS = $opts;
+ return My::Singleton::Role->meta;
+ }
+
+ package My::Special::Role;
+ use Moose::Role -metaclass => 'My::Role::Metaclass';
+
+ sub foo { 'My::Special::Role' }
+
+ package My::Usual::Role;
+ use Moose::Role;
+
+ sub bar { 'My::Usual::Role' }
+
+ package My::Class;
+ use Moose;
+
+ with (
+ 'My::Special::Role' => { number => 1 },
+ 'My::Usual::Role' => { number => 2 },
+ );
+};
+
+is(My::Class->foo, 'My::Singleton::Role', 'role_for_combination applied');
+is(My::Class->bar, 'My::Usual::Role', 'collateral role');
+is_deeply($OPTS, { number => 1 });
+