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';
- my $dstname = $args{alias} ? ($args{alias}->{$name}||$name) : $name;
- if ($classname->can($dstname)) {
+
+ if ($classname->can($name)) {
# XXX what's Moose's behavior?
- next;
+ #next;
+ } else {
+ *{"${classname}::${name}"} = *{"${selfname}::${name}"};
+ }
+ if ($args{alias} && $args{alias}->{$name}) {
+ my $dstname = $args{alias}->{$name};
+ unless ($classname->can($dstname)) {
+ *{"${classname}::${dstname}"} = *{"${selfname}::${name}"};
+ }
}
- *{"${classname}::${dstname}"} = *{"${selfname}::${name}"};
}
}
push @{ $class->roles }, $self, @{ $self->roles };
}
+sub combine_apply {
+ my(undef, $class, @roles) = @_;
+ my $classname = $class->name;
+
+ if ($class->isa('Mouse::Meta::Class')) {
+ for my $role_spec (@roles) {
+ my $self = $role_spec->[0]->meta;
+ for my $name (@{$self->{required_methods}}) {
+ unless ($classname->can($name)) {
+ my $method_required = 0;
+ for my $role (@roles) {
+ $method_required = 1 if $self->name ne $role->[0] && $role->[0]->can($name);
+ }
+ confess "'".$self->name."' requires the method '$name' to be implemented by '$classname'"
+ unless $method_required;
+ }
+ }
+ }
+ }
+
+ {
+ no strict 'refs';
+ for my $role_spec (@roles) {
+ my $self = $role_spec->[0]->meta;
+ my $selfname = $self->name;
+ my %args = %{ $role_spec->[1] };
+ 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)) {
+ # XXX what's Moose's behavior?
+ #next;
+ } else {
+ *{"${classname}::${name}"} = *{"${selfname}::${name}"};
+ }
+ if ($args{alias} && $args{alias}->{$name}) {
+ my $dstname = $args{alias}->{$name};
+ unless ($classname->can($dstname)) {
+ *{"${classname}::${dstname}"} = *{"${selfname}::${name}"};
+ }
+ }
+ }
+ }
+ }
+
+
+ if ($class->isa('Mouse::Meta::Class')) {
+ # apply role to class
+ for my $role_spec (@roles) {
+ my $self = $role_spec->[0]->meta;
+ for my $name ($self->get_attribute_list) {
+ next if $class->has_attribute($name);
+ my $spec = $self->get_attribute($name);
+ Mouse::Meta::Attribute->create($class, $name, %$spec);
+ }
+ }
+ } else {
+ # apply role to role
+ # XXX Room for speed improvement
+ for my $role_spec (@roles) {
+ my $self = $role_spec->[0]->meta;
+ for my $name ($self->get_attribute_list) {
+ next if $class->has_attribute($name);
+ my $spec = $self->get_attribute($name);
+ $class->add_attribute($name, $spec);
+ }
+ }
+ }
+
+ # XXX Room for speed improvement in role to role
+ for my $modifier_type (qw/before after around/) {
+ my $add_method = "add_${modifier_type}_method_modifier";
+ for my $role_spec (@roles) {
+ my $self = $role_spec->[0]->meta;
+ my $modified = $self->{"${modifier_type}_method_modifiers"};
+
+ for my $method_name (keys %$modified) {
+ for my $code (@{ $modified->{$method_name} }) {
+ $class->$add_method($method_name => $code);
+ }
+ }
+ }
+ }
+
+ # append roles
+ my %role_apply_cache;
+ my @apply_roles;
+ for my $role_spec (@roles) {
+ my $self = $role_spec->[0]->meta;
+ push @apply_roles, $self unless $role_apply_cache{$self}++;
+ for my $role ($self->roles) {
+ push @apply_roles, $role unless $role_apply_cache{$role}++;
+ }
+ }
+}
+
for my $modifier_type (qw/before after around/) {
no strict 'refs';
*{ __PACKAGE__ . '::' . "add_${modifier_type}_method_modifier" } = sub {
sub apply_all_roles {
my $meta = Mouse::Meta::Class->initialize(shift);
- my $role = shift;
- confess "Mouse::Util only supports 'apply_all_roles' on individual roles at a time" if @_;
- Mouse::load_class($role);
- $role->meta->apply($meta);
+ my @roles;
+ 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] => {} ];
+ }
+ }
+
+ foreach my $role_spec (@roles) {
+ Mouse::load_class( $role_spec->[0] );
+ }
+
+ ( $_->[0]->can('meta') && $_->[0]->meta->isa('Mouse::Meta::Role') )
+ || croak("You can only consume roles, "
+ . $_->[0]
+ . " is not a Moose role")
+ foreach @roles;
+
+ if ( scalar @roles == 1 ) {
+ my ( $role, $params ) = @{ $roles[0] };
+ $role->meta->apply( $meta, ( defined $params ? %$params : () ) );
+ }
+ else {
+ Mouse::Meta::Role->combine_apply($meta, @roles);
+ }
+
}
1;