adding method exclusion
[gitmo/Moose.git] / lib / Moose / Meta / Role / Application / ToRole.pm
CommitLineData
fb1e11d5 1package Moose::Meta::Role::Application::ToRole;
2
3use strict;
4use warnings;
5use metaclass;
6
7use Carp 'confess';
8use Scalar::Util 'blessed';
9
10use Data::Dumper;
11
12our $VERSION = '0.01';
13our $AUTHORITY = 'cpan:STEVAN';
14
15use base 'Moose::Meta::Role::Application';
16
1c9db35c 17sub apply {
c4538447 18 my ($self, $role1, $role2) = @_;
19 $self->SUPER::apply($role1, $role2);
20 $role2->add_role($role1);
1c9db35c 21}
22
23sub check_role_exclusions {
24 my ($self, $role1, $role2) = @_;
25 confess "Conflict detected: " . $role2->name . " excludes role '" . $role1->name . "'"
26 if $role2->excludes_role($role1->name);
27 foreach my $excluded_role_name ($role1->get_excluded_roles_list) {
28 confess "The class " . $role2->name . " does the excluded role '$excluded_role_name'"
29 if $role2->does_role($excluded_role_name);
30 $role2->add_excluded_roles($excluded_role_name);
31 }
32}
33
34sub check_required_methods {
35 my ($self, $role1, $role2) = @_;
36 foreach my $required_method_name ($role1->get_required_method_list) {
37 $role2->add_required_methods($required_method_name)
38 unless $role2->find_method_by_name($required_method_name);
39 }
40}
41
709c321c 42sub check_required_attributes {
43
44}
45
1c9db35c 46sub apply_attributes {
47 my ($self, $role1, $role2) = @_;
48 foreach my $attribute_name ($role1->get_attribute_list) {
49 # it if it has one already
50 if ($role2->has_attribute($attribute_name) &&
51 # make sure we haven't seen this one already too
52 $role2->get_attribute($attribute_name) != $role1->get_attribute($attribute_name)) {
53 confess "Role '" . $role1->name . "' has encountered an attribute conflict " .
54 "during composition. This is fatal error and cannot be disambiguated.";
55 }
56 else {
57 $role2->add_attribute(
58 $attribute_name,
59 $role1->get_attribute($attribute_name)
60 );
61 }
62 }
63}
64
65sub apply_methods {
66 my ($self, $role1, $role2) = @_;
67 foreach my $method_name ($role1->get_method_list) {
c4538447 68
69 next if $self->is_method_excluded($method_name);
70
1c9db35c 71 # it if it has one already
72 if ($role2->has_method($method_name) &&
73 # and if they are not the same thing ...
74 $role2->get_method($method_name)->body != $role1->get_method($method_name)->body) {
75 # method conflicts between roles result
76 # in the method becoming a requirement
77 $role2->add_required_methods($method_name);
78 }
79 else {
80 # add it, although it could be overriden
81 $role2->alias_method(
82 $method_name,
83 $role1->get_method($method_name)
84 );
85 }
86 }
87}
88
89sub apply_override_method_modifiers {
90 my ($self, $role1, $role2) = @_;
91 foreach my $method_name ($role1->get_method_modifier_list('override')) {
92 # it if it has one already then ...
93 if ($role2->has_method($method_name)) {
94 # if it is being composed into another role
95 # we have a conflict here, because you cannot
96 # combine an overriden method with a locally
97 # defined one
98 confess "Role '" . $role1->name . "' has encountered an 'override' method conflict " .
99 "during composition (A local method of the same name as been found). This " .
100 "is fatal error.";
101 }
102 else {
103 # if we are a role, we need to make sure
104 # we dont have a conflict with the role
105 # we are composing into
106 if ($role2->has_override_method_modifier($method_name) &&
107 $role2->get_override_method_modifier($method_name) != $role2->get_override_method_modifier($method_name)) {
108 confess "Role '" . $role1->name . "' has encountered an 'override' method conflict " .
109 "during composition (Two 'override' methods of the same name encountered). " .
110 "This is fatal error.";
111 }
112 else {
113 # if there is no conflict,
114 # just add it to the role
115 $role2->add_override_method_modifier(
116 $method_name,
117 $role1->get_override_method_modifier($method_name)
118 );
119 }
120 }
121 }
122}
123
124sub apply_method_modifiers {
125 my ($self, $modifier_type, $role1, $role2) = @_;
126 my $add = "add_${modifier_type}_method_modifier";
127 my $get = "get_${modifier_type}_method_modifiers";
128 foreach my $method_name ($role1->get_method_modifier_list($modifier_type)) {
129 $role2->$add(
130 $method_name,
131 $_
132 ) foreach $role1->$get($method_name);
133 }
134}
135
1c9db35c 136
fb1e11d5 1371;
138
139__END__
140
141=pod
142
143=head1 NAME
144
145Moose::Meta::Role::Application::ToRole
146
147=head1 DESCRIPTION
148
149=head2 METHODS
150
151=over 4
152
153=item B<new>
154
155=item B<meta>
156
1c9db35c 157=item B<apply>
158
709c321c 159=item B<check_role_exclusions>
160
1c9db35c 161=item B<check_required_methods>
162
709c321c 163=item B<check_required_attributes>
1c9db35c 164
165=item B<apply_attributes>
166
167=item B<apply_methods>
168
169=item B<apply_method_modifiers>
170
1c9db35c 171=item B<apply_override_method_modifiers>
172
fb1e11d5 173=back
174
175=head1 BUGS
176
177All complex software has bugs lurking in it, and this module is no
178exception. If you find a bug please either email me, or add the bug
179to cpan-RT.
180
181=head1 AUTHOR
182
183Stevan Little E<lt>stevan@iinteractive.comE<gt>
184
185=head1 COPYRIGHT AND LICENSE
186
778db3ac 187Copyright 2006-2008 by Infinity Interactive, Inc.
fb1e11d5 188
189L<http://www.iinteractive.com>
190
191This library is free software; you can redistribute it and/or modify
192it under the same terms as Perl itself.
193
194=cut
195