- However, we will probably deprecate $obj->new, so please don't start
using it for new code!
+ * Moose::Meta::Role::Application
+ * Moose::Meta::Role::Application::RoleSummation
+ - Rename alias and excludes to -alias and -excludes (but keep the old
+ names for now, for backcompat) (doy)
+
0.88 Fri Jul 24, 2009
* Moose::Manual::Contributing
- Re-write the Moose::Manual::Contributing document to reflect
use Moose::Role;
with 'Restartable' => {
- alias => {
+ -alias => {
stop => '_stop',
start => '_start'
}
package Restartable::ButBroken;
use Moose::Role;
- with 'Restartable' => { excludes => [ 'stop', 'start' ] };
+ with 'Restartable' => { -excludes => [ 'stop', 'start' ] };
sub stop {
my $self = shift;
originals (1).
with 'Restartable' => {
- alias => {
+ -alias => {
stop => '_stop',
start => '_start'
}
new behavior for C<stop> and C<start>. We exclude them entirely when
composing the C<Restartable> role into C<Restartable::ButBroken>.
-It's worth noting that the C<excludes> parameter also accepts a single
+It's worth noting that the C<-excludes> parameter also accepts a single
string as an argument if you just want to exclude one method.
- with 'Restartable' => { excludes => [ 'stop', 'start' ] };
+ with 'Restartable' => { -excludes => [ 'stop', 'start' ] };
=head1 CONCLUSION
MyApp::Role::Job::Manager->meta->apply(
$lisa,
- alias => { assign_work => 'get_off_your_lazy_behind' },
+ -alias => { assign_work => 'get_off_your_lazy_behind' },
);
We saw examples of how method exclusion and alias working in L<roles
way of doing this.
+The C<alias> and C<excludes> role parameters have been renamed to C<-alias>
+and C<-excludes>. The old names still work, but new code should use the new
+names, and eventually the old ones will be deprecated and removed.
+
=head1 Version 0.84
When an attribute generates I<no> accessors, we now warn. This is to help
use Moose;
- with 'Breakable' => { alias => { break => 'break_bone' } },
- 'Breakdancer' => { alias => { break => 'break_dance' } };
+ with 'Breakable' => { -alias => { break => 'break_bone' } },
+ 'Breakdancer' => { -alias => { break => 'break_dance' } };
However, aliasing a method simply makes a I<copy> of the method with
the new name. We also need to exclude the original name:
with 'Breakable' => {
- alias => { break => 'break_bone' },
- excludes => 'break',
+ -alias => { break => 'break_bone' },
+ -excludes => 'break',
},
'Breakdancer' => {
- alias => { break => 'break_dance' },
- excludes => 'break',
+ -alias => { break => 'break_dance' },
+ -excludes => 'break',
};
The excludes parameter prevents the C<break> method from being composed
This method accepts a list of array references. Each array reference
should contain a role name as its first element. The second element is
-an optional hash reference. The hash reference can contain C<excludes>
-and C<alias> keys to control how methods are composed from the role.
+an optional hash reference. The hash reference can contain C<-excludes>
+and C<-alias> keys to control how methods are composed from the role.
The return value is a new L<Moose::Meta::Role::Composite> that
represents the combined roles.
our $AUTHORITY = 'cpan:STEVAN';
__PACKAGE__->meta->add_attribute('method_exclusions' => (
- init_arg => 'excludes',
+ init_arg => '-excludes',
reader => 'get_method_exclusions',
default => sub { [] }
));
__PACKAGE__->meta->add_attribute('method_aliases' => (
- init_arg => 'alias',
+ init_arg => '-alias',
reader => 'get_method_aliases',
default => sub { {} }
));
sub new {
my ($class, %params) = @_;
- if (exists $params{excludes}) {
+ if (exists $params{excludes} && !exists $params{'-excludes'}) {
+ $params{'-excludes'} = delete $params{excludes};
+ }
+ if (exists $params{alias} && !exists $params{'-alias'}) {
+ $params{'-alias'} = delete $params{alias};
+ }
+
+ if (exists $params{'-excludes'}) {
# I wish we had coercion here :)
- $params{excludes} = (ref $params{excludes} eq 'ARRAY'
- ? $params{excludes}
- : [ $params{excludes} ]);
+ $params{'-excludes'} = (ref $params{'-excludes'} eq 'ARRAY'
+ ? $params{'-excludes'}
+ : [ $params{'-excludes'} ]);
}
$class->_new(\%params);
sub get_exclusions_for_role {
my ($self, $role) = @_;
$role = $role->name if blessed $role;
- if ($self->role_params->{$role} && defined $self->role_params->{$role}->{excludes}) {
- if (ref $self->role_params->{$role}->{excludes} eq 'ARRAY') {
- return $self->role_params->{$role}->{excludes};
+ my $excludes_key = exists $self->role_params->{$role}->{'-excludes'} ?
+ '-excludes' : 'excludes';
+ if ($self->role_params->{$role} && defined $self->role_params->{$role}->{$excludes_key}) {
+ if (ref $self->role_params->{$role}->{$excludes_key} eq 'ARRAY') {
+ return $self->role_params->{$role}->{$excludes_key};
}
- return [ $self->role_params->{$role}->{excludes} ];
+ return [ $self->role_params->{$role}->{$excludes_key} ];
}
return [];
}
sub get_method_aliases_for_role {
my ($self, $role) = @_;
$role = $role->name if blessed $role;
- if ($self->role_params->{$role} && defined $self->role_params->{$role}->{alias}) {
- return $self->role_params->{$role}->{alias};
+ my $alias_key = exists $self->role_params->{$role}->{'-alias'} ?
+ '-alias' : 'alias';
+ if ($self->role_params->{$role} && defined $self->role_params->{$role}->{$alias_key}) {
+ return $self->role_params->{$role}->{$alias_key};
}
return {};
}
The C<$applicant> must already have a metaclass object.
The list of C<@roles> should be a list of names, each of which can be
-followed by an optional hash reference of options (C<excludes> and
-C<alias>).
+followed by an optional hash reference of options (C<-excludes> and
+C<-alias>).
=item B<ensure_all_roles($applicant, @roles)>
has foo => (
traits => [
'My::Attribute::Trait' => {
- alias => {
+ -alias => {
reversed_name => 'eman',
},
},
has foo => (
traits => [
'My::Attribute::Trait' => {
- alias => {
+ -alias => {
reversed_name => 'reversed',
},
- excludes => 'reversed_name',
+ -excludes => 'reversed_name',
},
],
is => 'bare',
package My::Class;
use Moose;
- with 'My::Role' => { excludes => 'bar' };
+ with 'My::Role' => { -excludes => 'bar' };
}
ok(My::Class->meta->has_method($_), "we have a $_ method") for qw(foo baz);
package My::OtherRole;
use Moose::Role;
- with 'My::Role' => { excludes => 'foo' };
+ with 'My::Role' => { -excludes => 'foo' };
sub foo { 'My::OtherRole::foo' }
sub bar { 'My::OtherRole::bar' }
use Moose;
::lives_ok {
- with 'Foo::Role' => { excludes => 'foo' },
- 'Bar::Role' => { excludes => 'foo' },
+ with 'Foo::Role' => { -excludes => 'foo' },
+ 'Bar::Role' => { -excludes => 'foo' },
'Baz::Role';
} '... composed our roles correctly';
::throws_ok {
with 'Foo::Role',
- 'Bar::Role' => { excludes => 'foo' },
+ 'Bar::Role' => { -excludes => 'foo' },
'Baz::Role';
} qr/Due to a method name conflict in roles 'Baz::Role' and 'Foo::Role', the method 'foo' must be implemented or excluded by 'My::Foo::Class::Broken'/,
'... composed our roles correctly';
use Moose::Role;
::lives_ok {
- with 'Foo::Role' => { excludes => 'foo' },
- 'Bar::Role' => { excludes => 'foo' },
+ with 'Foo::Role' => { -excludes => 'foo' },
+ 'Bar::Role' => { -excludes => 'foo' },
'Baz::Role';
} '... composed our roles correctly';
}
::lives_ok {
with 'Foo::Role',
- 'Bar::Role' => { excludes => 'foo' },
+ 'Bar::Role' => { -excludes => 'foo' },
'Baz::Role';
} '... composed our roles correctly';
}
use Moose;
::lives_ok {
- with 'My::Role' => { alias => { bar => 'role_bar' } };
+ with 'My::Role' => { -alias => { bar => 'role_bar' } };
} '... this succeeds';
package My::Class::Failure;
use Moose;
::throws_ok {
- with 'My::Role' => { alias => { bar => 'role_bar' } };
+ with 'My::Role' => { -alias => { bar => 'role_bar' } };
} qr/Cannot create a method alias if a local method of the same name exists/, '... this succeeds';
sub role_bar { 'FAIL' }
use Moose::Role;
::lives_ok {
- with 'My::Role' => { alias => { bar => 'role_bar' } };
+ with 'My::Role' => { -alias => { bar => 'role_bar' } };
} '... this succeeds';
sub bar { 'My::OtherRole::bar' }
use Moose::Role;
::throws_ok {
- with 'My::Role' => { alias => { bar => 'role_bar' } };
+ with 'My::Role' => { -alias => { bar => 'role_bar' } };
} qr/Cannot create a method alias if a local method of the same name exists/, '... this succeeds';
sub role_bar { 'FAIL' }
use Moose::Role;
::lives_ok {
- with 'My::Role' => { alias => { bar => 'role_bar' } };
+ with 'My::Role' => { -alias => { bar => 'role_bar' } };
} '... this succeeds';
}
use Moose;
::lives_ok {
- with 'Foo::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' },
- 'Bar::Role' => { alias => { 'foo' => 'bar_foo' }, excludes => 'foo' },
+ with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
+ 'Bar::Role' => { -alias => { 'foo' => 'bar_foo' }, -excludes => 'foo' },
'Baz::Role';
} '... composed our roles correctly';
use Moose;
::throws_ok {
- with 'Foo::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' },
- 'Bar::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' },
+ with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
+ 'Bar::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
'Baz::Role';
} qr/Due to a method name conflict in roles 'Bar::Role' and 'Foo::Role', the method 'foo_foo' must be implemented or excluded by 'My::Foo::Class::Broken'/,
'... composed our roles correctly';
use Moose::Role;
::lives_ok {
- with 'Foo::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' },
- 'Bar::Role' => { alias => { 'foo' => 'bar_foo' }, excludes => 'foo' },
+ with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
+ 'Bar::Role' => { -alias => { 'foo' => 'bar_foo' }, -excludes => 'foo' },
'Baz::Role';
} '... composed our roles correctly';
}
use Moose::Role;
::lives_ok {
- with 'Foo::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' },
- 'Bar::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' },
+ with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
+ 'Bar::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
'Baz::Role';
} '... composed our roles correctly';
}
use Moose;
::lives_ok {
- with 'Foo' => { excludes => [qw/bar baz gorch/], alias => { gorch => 'foo_gorch' } },
- 'Bar' => { excludes => [qw/foo baz gorch/] },
- 'Baz' => { excludes => [qw/foo bar gorch/], alias => { foo => 'baz_foo', bar => 'baz_bar' } },
- 'Gorch' => { excludes => [qw/foo bar baz/] };
+ with 'Foo' => { -excludes => [qw/bar baz gorch/], -alias => { gorch => 'foo_gorch' } },
+ 'Bar' => { -excludes => [qw/foo baz gorch/] },
+ 'Baz' => { -excludes => [qw/foo bar gorch/], -alias => { foo => 'baz_foo', bar => 'baz_bar' } },
+ 'Gorch' => { -excludes => [qw/foo bar baz/] };
} '... everything works out all right';
}
use Moose;
with 'My::Role' => {
- alias => { foo => 'baz', bar => 'gorch' },
- excludes => ['foo', 'bar'],
+ -alias => { foo => 'baz', bar => 'gorch' },
+ -excludes => ['foo', 'bar'],
};
}
use Moose::Role;
with 'My::Role' => {
- alias => { foo => 'baz', bar => 'gorch' },
- excludes => ['foo', 'bar'],
+ -alias => { foo => 'baz', bar => 'gorch' },
+ -excludes => ['foo', 'bar'],
};
package My::Class::Again;
package Consumer::Excludes;
use Moose;
- with 'Role::Foo' => { excludes => 'foo' };
+ with 'Role::Foo' => { -excludes => 'foo' };
package Consumer::Aliases;
use Moose;
- with 'Role::Foo' => { alias => { 'foo' => 'role_foo' } };
+ with 'Role::Foo' => { -alias => { 'foo' => 'role_foo' } };
package Consumer::Overrides;
use Moose;
package My::Class;
use Moose -traits => [
'My::Trait' => {
- alias => {
+ -alias => {
reversed_name => 'enam',
},
},
package My::Other::Class;
use Moose -traits => [
'My::Trait' => {
- alias => {
+ -alias => {
reversed_name => 'reversed',
},
- excludes => 'reversed_name',
+ -excludes => 'reversed_name',
},
];
}