improve alias/excludes warning
[gitmo/Moose.git] / lib / Moose / Meta / Role / Application.pm
CommitLineData
fb1e11d5 1package Moose::Meta::Role::Application;
2
3use strict;
4use warnings;
5use metaclass;
6
f4b86ac0 7our $VERSION = '1.16';
75b95414 8$VERSION = eval $VERSION;
fb1e11d5 9our $AUTHORITY = 'cpan:STEVAN';
10
c4538447 11__PACKAGE__->meta->add_attribute('method_exclusions' => (
c8b8d92f 12 init_arg => '-excludes',
c4538447 13 reader => 'get_method_exclusions',
14 default => sub { [] }
15));
16
3e19778d 17__PACKAGE__->meta->add_attribute('method_aliases' => (
c8b8d92f 18 init_arg => '-alias',
3e19778d 19 reader => 'get_method_aliases',
20 default => sub { {} }
21));
22
a8eb9fe0 23__PACKAGE__->meta->add_attribute('fire_alias_excludes_warning' => (
24 init_arg => 'fire_alias_excludes_warning',
25 reader => 'fire_alias_excludes_warning',
26 default => 0
27));
28
d03bd989 29sub new {
c4538447 30 my ($class, %params) = @_;
d03bd989 31
82f8ace6 32 if ( exists $params{excludes} || exists $params{alias} ) {
a8eb9fe0 33 $params{fire_alias_excludes_warning} = 1;
82f8ace6 34 }
35
3a4fc1c0 36 if ( exists $params{excludes} && !exists $params{'-excludes'} ) {
c8b8d92f 37 $params{'-excludes'} = delete $params{excludes};
38 }
3a4fc1c0 39 if ( exists $params{alias} && !exists $params{'-alias'} ) {
c8b8d92f 40 $params{'-alias'} = delete $params{alias};
41 }
42
3a4fc1c0 43 if ( exists $params{'-excludes'} ) {
44
c4538447 45 # I wish we had coercion here :)
3a4fc1c0 46 $params{'-excludes'} = (
47 ref $params{'-excludes'} eq 'ARRAY'
48 ? $params{'-excludes'}
49 : [ $params{'-excludes'} ]
50 );
c4538447 51 }
d03bd989 52
1b8d1399 53 $class->_new(\%params);
c4538447 54}
55
56sub is_method_excluded {
57 my ($self, $method_name) = @_;
58 foreach (@{$self->get_method_exclusions}) {
59 return 1 if $_ eq $method_name;
60 }
61 return 0;
62}
fb1e11d5 63
3e19778d 64sub is_method_aliased {
65 my ($self, $method_name) = @_;
66 exists $self->get_method_aliases->{$method_name} ? 1 : 0
67}
68
69sub is_aliased_method {
70 my ($self, $method_name) = @_;
71 my %aliased_names = reverse %{$self->get_method_aliases};
72 exists $aliased_names{$method_name} ? 1 : 0;
73}
74
fb1e11d5 75sub apply {
1c9db35c 76 my $self = shift;
fb1e11d5 77
a8eb9fe0 78 if ($self->fire_alias_excludes_warning) {
79 Moose::Deprecated::deprecated(
80 feature => 'alias or excludes',
81 message =>
82 "The alias and excludes options for role application have been renamed -alias and -excludes (applying ${\$_[0]->name} to ${\$_[1]->name} - do you need to upgrade ${\$_[1]->name}?)"
83 );
84 }
85
1c9db35c 86 $self->check_role_exclusions(@_);
87 $self->check_required_methods(@_);
709c321c 88 $self->check_required_attributes(@_);
d03bd989 89
1c9db35c 90 $self->apply_attributes(@_);
d03bd989 91 $self->apply_methods(@_);
92
1c9db35c 93 $self->apply_override_method_modifiers(@_);
d03bd989 94
1c9db35c 95 $self->apply_before_method_modifiers(@_);
96 $self->apply_around_method_modifiers(@_);
97 $self->apply_after_method_modifiers(@_);
fb1e11d5 98}
99
547dda77 100sub check_role_exclusions { Carp::croak "Abstract Method" }
101sub check_required_methods { Carp::croak "Abstract Method" }
102sub check_required_attributes { Carp::croak "Abstract Method" }
103
104sub apply_attributes { Carp::croak "Abstract Method" }
105sub apply_methods { Carp::croak "Abstract Method" }
106sub apply_override_method_modifiers { Carp::croak "Abstract Method" }
107sub apply_method_modifiers { Carp::croak "Abstract Method" }
bca01282 108
109sub apply_before_method_modifiers { (shift)->apply_method_modifiers('before' => @_) }
110sub apply_around_method_modifiers { (shift)->apply_method_modifiers('around' => @_) }
111sub apply_after_method_modifiers { (shift)->apply_method_modifiers('after' => @_) }
fb1e11d5 112
1131;
114
115__END__
116
117=pod
118
119=head1 NAME
120
ab76842e 121Moose::Meta::Role::Application - A base class for role application
fb1e11d5 122
123=head1 DESCRIPTION
124
125This is the abstract base class for role applications.
126
a5583b3a 127The API for this class and its subclasses still needs some
128consideration, and is intentionally not yet documented.
129
fb1e11d5 130=head2 METHODS
131
132=over 4
133
134=item B<new>
135
136=item B<meta>
137
c4538447 138=item B<get_method_exclusions>
139
140=item B<is_method_excluded>
141
3e19778d 142=item B<get_method_aliases>
143
144=item B<is_aliased_method>
145
146=item B<is_method_aliased>
147
fb1e11d5 148=item B<apply>
149
709c321c 150=item B<check_role_exclusions>
151
fb1e11d5 152=item B<check_required_methods>
153
709c321c 154=item B<check_required_attributes>
fb1e11d5 155
156=item B<apply_attributes>
157
158=item B<apply_methods>
159
160=item B<apply_method_modifiers>
161
162=item B<apply_before_method_modifiers>
163
164=item B<apply_after_method_modifiers>
165
166=item B<apply_around_method_modifiers>
167
168=item B<apply_override_method_modifiers>
169
170=back
171
172=head1 BUGS
173
d4048ef3 174See L<Moose/BUGS> for details on reporting bugs.
fb1e11d5 175
176=head1 AUTHOR
177
178Stevan Little E<lt>stevan@iinteractive.comE<gt>
179
180=head1 COPYRIGHT AND LICENSE
181
7e0492d3 182Copyright 2006-2010 by Infinity Interactive, Inc.
fb1e11d5 183
184L<http://www.iinteractive.com>
185
186This library is free software; you can redistribute it and/or modify
187it under the same terms as Perl itself.
188
189=cut
190