my $meta = Mouse::Meta::Class->initialize(caller);
my $role = shift;
+ my $args = shift || {};
- confess "Mouse::Role only supports 'with' on individual roles at a time" if @_;
+ confess "Mouse::Role only supports 'with' on individual roles at a time" if @_ || !ref $args;
Mouse::load_class($role);
- $role->meta->apply($meta);
+ $role->meta->apply($meta, %$args);
}
sub import {
my $selfname = $self->name;
my $class = shift;
my $classname = $class->name;
+ my %args = @_;
- for my $name (@{$self->{required_methods}}) {
- unless ($classname->can($name)) {
- confess "'$selfname' requires the method '$name' to be implemented by '$classname'";
+ if ($class->isa('Mouse::Meta::Class')) {
+ for my $name (@{$self->{required_methods}}) {
+ unless ($classname->can($name)) {
+ confess "'$selfname' requires the method '$name' to be implemented by '$classname'";
+ }
}
}
no strict 'refs';
for my $name ($self->get_method_list) {
next if $name eq 'has' || $name eq 'requires' || $name eq 'meta' || $name eq 'with' || $name eq 'around' || $name eq 'before' || $name eq 'after' || $name eq 'blessed' || $name eq 'extends' || $name eq 'confess' || $name eq 'excludes';
- if ($classname->can($name)) {
+ my $dstname = $args{alias} ? ($args{alias}->{$name}||$name) : $name;
+ if ($classname->can($dstname)) {
# XXX what's Moose's behavior?
next;
}
- *{"${classname}::${name}"} = *{"${selfname}::${name}"};
+ *{"${classname}::${dstname}"} = *{"${selfname}::${name}"};
}
}
sub with {
my $meta = Mouse::Meta::Role->initialize(caller);
my $role = shift;
- confess "Mouse::Role only supports 'with' on individual roles at a time" if @_;
+ my $args = shift || {};
+ confess "Mouse::Role only supports 'with' on individual roles at a time" if @_ || !ref $args;
Mouse::load_class($role);
- $role->meta->apply($meta);
+ $role->meta->apply($meta, %$args);
}
sub requires {
use strict;
use warnings;
-use Test::More tests => 4;
+use Test::More tests => 5;
{
package Animal;
use Mouse::Role;
+ requires 'bark';
sub eat { 'delicious' }
has food => ( is => 'ro' );
}
package Chihuahua;
use Mouse;
with 'Dog';
+ sub bark { 'bow-wow' }
}
ok !Animal->can('food');
my $c = Chihuahua->new(food => 'bone');
is $c->eat(), 'delicious';
is $c->food(), 'bone';
+is $c->bark(), 'bow-wow';
--- /dev/null
+use strict;
+use warnings;
+use Test::More tests => 5;
+
+{
+ package Animal;
+ use Mouse::Role;
+ sub eat { 'delicious' }
+}
+
+{
+ package Cat;
+ use Mouse::Role;
+ with 'Animal', {
+ alias => { eat => 'drink' },
+ };
+ sub eat { 'good!' }
+}
+
+{
+ package Tama;
+ use Mouse;
+ with 'Cat';
+}
+
+{
+ package Dog;
+ use Mouse;
+ with 'Animal', {
+ alias => { eat => 'drink' }
+ };
+}
+
+ok(!Dog->can('eat'));
+ok(Dog->can('drink'));
+
+my $d = Dog->new();
+is($d->drink(), 'delicious');
+
+my $t = Tama->new;
+is $t->drink(), 'delicious';
+is $t->eat(), 'good!';
+