I started documenting this stuff but then my head blew up. I'm
[gitmo/Moose.git] / lib / Moose / Meta / Role / Application.pm
CommitLineData
fb1e11d5 1package Moose::Meta::Role::Application;
2
3use strict;
4use warnings;
5use metaclass;
6
4b2189ce 7our $VERSION = '0.72';
75b95414 8$VERSION = eval $VERSION;
fb1e11d5 9our $AUTHORITY = 'cpan:STEVAN';
10
c4538447 11__PACKAGE__->meta->add_attribute('method_exclusions' => (
12 init_arg => 'excludes',
13 reader => 'get_method_exclusions',
14 default => sub { [] }
15));
16
3e19778d 17__PACKAGE__->meta->add_attribute('method_aliases' => (
18 init_arg => 'alias',
19 reader => 'get_method_aliases',
20 default => sub { {} }
21));
22
c4538447 23sub new {
24 my ($class, %params) = @_;
25
26 if (exists $params{excludes}) {
27 # I wish we had coercion here :)
28 $params{excludes} = (ref $params{excludes} eq 'ARRAY'
29 ? $params{excludes}
30 : [ $params{excludes} ]);
31 }
32
1b8d1399 33 $class->_new(\%params);
c4538447 34}
35
36sub is_method_excluded {
37 my ($self, $method_name) = @_;
38 foreach (@{$self->get_method_exclusions}) {
39 return 1 if $_ eq $method_name;
40 }
41 return 0;
42}
fb1e11d5 43
3e19778d 44sub is_method_aliased {
45 my ($self, $method_name) = @_;
46 exists $self->get_method_aliases->{$method_name} ? 1 : 0
47}
48
49sub is_aliased_method {
50 my ($self, $method_name) = @_;
51 my %aliased_names = reverse %{$self->get_method_aliases};
52 exists $aliased_names{$method_name} ? 1 : 0;
53}
54
fb1e11d5 55sub apply {
1c9db35c 56 my $self = shift;
fb1e11d5 57
1c9db35c 58 $self->check_role_exclusions(@_);
59 $self->check_required_methods(@_);
709c321c 60 $self->check_required_attributes(@_);
fb1e11d5 61
1c9db35c 62 $self->apply_attributes(@_);
63 $self->apply_methods(@_);
fb1e11d5 64
1c9db35c 65 $self->apply_override_method_modifiers(@_);
fb1e11d5 66
1c9db35c 67 $self->apply_before_method_modifiers(@_);
68 $self->apply_around_method_modifiers(@_);
69 $self->apply_after_method_modifiers(@_);
fb1e11d5 70}
71
547dda77 72sub check_role_exclusions { Carp::croak "Abstract Method" }
73sub check_required_methods { Carp::croak "Abstract Method" }
74sub check_required_attributes { Carp::croak "Abstract Method" }
75
76sub apply_attributes { Carp::croak "Abstract Method" }
77sub apply_methods { Carp::croak "Abstract Method" }
78sub apply_override_method_modifiers { Carp::croak "Abstract Method" }
79sub apply_method_modifiers { Carp::croak "Abstract Method" }
bca01282 80
81sub apply_before_method_modifiers { (shift)->apply_method_modifiers('before' => @_) }
82sub apply_around_method_modifiers { (shift)->apply_method_modifiers('around' => @_) }
83sub apply_after_method_modifiers { (shift)->apply_method_modifiers('after' => @_) }
fb1e11d5 84
851;
86
87__END__
88
89=pod
90
91=head1 NAME
92
ab76842e 93Moose::Meta::Role::Application - A base class for role application
fb1e11d5 94
95=head1 DESCRIPTION
96
7579c703 97This is the abstract base class for role applications. Role
98application is the logic of composing a role into something. That
99something could be a class, another role, or an object instance.
fb1e11d5 100
101=head2 METHODS
102
103=over 4
104
7579c703 105=item B<< Moose::Meta::Role::Application->new(%options) >>
fb1e11d5 106
7579c703 107This method returns a new role application. It accepts several
108options:
109
110=over 8
111
112=item * excludes
113
114This is an optional array reference of methods to be excluded when
115applying the role.
116
117=item * alias
118
119This is an optional hash reference of methods to be renamed when
120applying the role. The keys are the original method names, and the
121values are the new method names.
122
123=back
124
125Note that the constructor does not actually take any roles as
126arguments.
127
128=item B<< $application->get_method_exclusions >>
fb1e11d5 129
7579c703 130Returns an array reference containing the names of the excluded
131methods.
c4538447 132
7579c703 133=item B<< $application->is_method_excluded($method_name) >>
c4538447 134
7579c703 135Given a method name, returns true if the method is excluded.
3e19778d 136
7579c703 137=item B<< $application->get_method_aliases >>
3e19778d 138
7579c703 139Returns the hash reference of method aliases passed to the
140constructor.
3e19778d 141
7579c703 142=item B<< $application->is_aliased_method($method_name) >>
fb1e11d5 143
7579c703 144This takes the name of the original method, and returns true if it is
145aliased.
146
147=item B<< $application->is_method_aliased($method_name) >>
148
149Returns true if the method name given is being used as the I<new> name
150for any method.
151
152=item B<< $application->apply($role, $thing) >>
153
154This method implements the logic of role application by calling the
155various check and apply methods below. Any arguments passed to this
156method are simply passed on to the other methods, without any
157processing.
158
159The first argument is always a L<Moose::Meta::Role> object, and the
160second is the thing to which the role is being applied.
161
162In some cases, the second
163
164=item B<< $application->check_role_exclusions(...) >>
165
166A virtual method. Subclasses are expected to throw an error if
709c321c 167
fb1e11d5 168=item B<check_required_methods>
169
709c321c 170=item B<check_required_attributes>
fb1e11d5 171
172=item B<apply_attributes>
173
174=item B<apply_methods>
175
176=item B<apply_method_modifiers>
177
178=item B<apply_before_method_modifiers>
179
180=item B<apply_after_method_modifiers>
181
182=item B<apply_around_method_modifiers>
183
184=item B<apply_override_method_modifiers>
185
7579c703 186=item B<meta>
187
fb1e11d5 188=back
189
190=head1 BUGS
191
192All complex software has bugs lurking in it, and this module is no
193exception. If you find a bug please either email me, or add the bug
194to cpan-RT.
195
196=head1 AUTHOR
197
198Stevan Little E<lt>stevan@iinteractive.comE<gt>
199
200=head1 COPYRIGHT AND LICENSE
201
2840a3b2 202Copyright 2006-2009 by Infinity Interactive, Inc.
fb1e11d5 203
204L<http://www.iinteractive.com>
205
206This library is free software; you can redistribute it and/or modify
207it under the same terms as Perl itself.
208
209=cut
210