Commit | Line | Data |
fb1e11d5 |
1 | package Moose::Meta::Role::Application::ToRole; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use metaclass; |
6 | |
fb1e11d5 |
7 | use Scalar::Util 'blessed'; |
8 | |
fb1e11d5 |
9 | our $AUTHORITY = 'cpan:STEVAN'; |
10 | |
11 | use base 'Moose::Meta::Role::Application'; |
12 | |
1c9db35c |
13 | sub apply { |
d03bd989 |
14 | my ($self, $role1, $role2) = @_; |
15 | $self->SUPER::apply($role1, $role2); |
16 | $role2->add_role($role1); |
1c9db35c |
17 | } |
18 | |
19 | sub check_role_exclusions { |
20 | my ($self, $role1, $role2) = @_; |
70ea9161 |
21 | if ( $role2->excludes_role($role1->name) ) { |
22 | require Moose; |
23 | Moose->throw_error("Conflict detected: " . $role2->name . " excludes role '" . $role1->name . "'"); |
24 | } |
1c9db35c |
25 | foreach my $excluded_role_name ($role1->get_excluded_roles_list) { |
70ea9161 |
26 | if ( $role2->does_role($excluded_role_name) ) { |
27 | require Moose; |
28 | Moose->throw_error("The class " . $role2->name . " does the excluded role '$excluded_role_name'"); |
29 | } |
1c9db35c |
30 | $role2->add_excluded_roles($excluded_role_name); |
31 | } |
32 | } |
33 | |
34 | sub check_required_methods { |
35 | my ($self, $role1, $role2) = @_; |
4c93bc92 |
36 | foreach my $required_method ($role1->get_required_method_list) { |
37 | my $required_method_name = $required_method->name; |
d03bd989 |
38 | |
3e19778d |
39 | next if $self->is_aliased_method($required_method_name); |
d03bd989 |
40 | |
4c93bc92 |
41 | $role2->add_required_methods($required_method) |
1c9db35c |
42 | unless $role2->find_method_by_name($required_method_name); |
43 | } |
44 | } |
45 | |
709c321c |
46 | sub check_required_attributes { |
d03bd989 |
47 | |
709c321c |
48 | } |
49 | |
1c9db35c |
50 | sub apply_attributes { |
51 | my ($self, $role1, $role2) = @_; |
52 | foreach my $attribute_name ($role1->get_attribute_list) { |
53 | # it if it has one already |
54 | if ($role2->has_attribute($attribute_name) && |
55 | # make sure we haven't seen this one already too |
56 | $role2->get_attribute($attribute_name) != $role1->get_attribute($attribute_name)) { |
70ea9161 |
57 | |
51d4595e |
58 | my $role2_name = $role2->name; |
59 | |
70ea9161 |
60 | require Moose; |
51d4595e |
61 | Moose->throw_error( "Role '" |
62 | . $role1->name |
63 | . "' has encountered an attribute conflict" |
64 | . " while being composed into '$role2_name'." |
65 | . " This is a fatal error and cannot be disambiguated." |
66 | . " The conflicting attribute is named '$attribute_name'." ); |
1c9db35c |
67 | } |
68 | else { |
69 | $role2->add_attribute( |
f785aad8 |
70 | $role1->get_attribute($attribute_name)->clone |
1c9db35c |
71 | ); |
72 | } |
73 | } |
74 | } |
75 | |
76 | sub apply_methods { |
4a9e8ff6 |
77 | my ( $self, $role1, $role2 ) = @_; |
78 | foreach my $method ( $role1->_get_local_methods ) { |
79 | |
80 | my $method_name = $method->name; |
81 | |
ba7d613d |
82 | next if $method->isa('Class::MOP::Method::Meta'); |
db9476b1 |
83 | |
bd38046e |
84 | unless ( $self->is_method_excluded($method_name) ) { |
4a9e8ff6 |
85 | |
86 | my $role2_method = $role2->get_method($method_name); |
87 | if ( $role2_method |
88 | && $role2_method->body != $method->body ) { |
bd38046e |
89 | |
90 | # method conflicts between roles result in the method becoming |
91 | # a requirement |
92 | $role2->add_conflicting_method( |
93 | name => $method_name, |
94 | roles => [ $role1->name, $role2->name ], |
95 | ); |
96 | } |
97 | else { |
98 | $role2->add_method( |
99 | $method_name, |
4a9e8ff6 |
100 | $method, |
bd38046e |
101 | ); |
102 | } |
103 | } |
104 | |
4a9e8ff6 |
105 | next unless $self->is_method_aliased($method_name); |
094b50c0 |
106 | |
4a9e8ff6 |
107 | my $aliased_method_name = $self->get_method_aliases->{$method_name}; |
70ea9161 |
108 | |
4a9e8ff6 |
109 | my $role2_method = $role2->get_method($aliased_method_name); |
db9476b1 |
110 | |
4a9e8ff6 |
111 | if ( $role2_method |
112 | && $role2_method->body != $method->body ) { |
113 | |
114 | require Moose; |
115 | Moose->throw_error( |
116 | "Cannot create a method alias if a local method of the same name exists" |
db9476b1 |
117 | ); |
4a9e8ff6 |
118 | } |
db9476b1 |
119 | |
4a9e8ff6 |
120 | $role2->add_method( |
121 | $aliased_method_name, |
122 | $role1->get_method($method_name) |
123 | ); |
124 | |
125 | if ( !$role2->has_method($method_name) ) { |
126 | $role2->add_required_methods($method_name) |
127 | unless $self->is_method_excluded($method_name); |
1c9db35c |
128 | } |
129 | } |
130 | } |
131 | |
132 | sub apply_override_method_modifiers { |
133 | my ($self, $role1, $role2) = @_; |
134 | foreach my $method_name ($role1->get_method_modifier_list('override')) { |
135 | # it if it has one already then ... |
136 | if ($role2->has_method($method_name)) { |
137 | # if it is being composed into another role |
138 | # we have a conflict here, because you cannot |
6549b0d1 |
139 | # combine an overridden method with a locally |
1c9db35c |
140 | # defined one |
70ea9161 |
141 | require Moose; |
c245d69b |
142 | Moose->throw_error("Role '" . $role1->name . "' has encountered an 'override' method conflict " . |
1c9db35c |
143 | "during composition (A local method of the same name as been found). This " . |
4c0b3599 |
144 | "is fatal error."); |
1c9db35c |
145 | } |
146 | else { |
147 | # if we are a role, we need to make sure |
148 | # we dont have a conflict with the role |
149 | # we are composing into |
150 | if ($role2->has_override_method_modifier($method_name) && |
151 | $role2->get_override_method_modifier($method_name) != $role2->get_override_method_modifier($method_name)) { |
70ea9161 |
152 | |
153 | require Moose; |
c245d69b |
154 | Moose->throw_error("Role '" . $role1->name . "' has encountered an 'override' method conflict " . |
1c9db35c |
155 | "during composition (Two 'override' methods of the same name encountered). " . |
4c0b3599 |
156 | "This is fatal error."); |
1c9db35c |
157 | } |
158 | else { |
159 | # if there is no conflict, |
160 | # just add it to the role |
161 | $role2->add_override_method_modifier( |
162 | $method_name, |
163 | $role1->get_override_method_modifier($method_name) |
164 | ); |
165 | } |
166 | } |
167 | } |
168 | } |
169 | |
170 | sub apply_method_modifiers { |
171 | my ($self, $modifier_type, $role1, $role2) = @_; |
172 | my $add = "add_${modifier_type}_method_modifier"; |
173 | my $get = "get_${modifier_type}_method_modifiers"; |
174 | foreach my $method_name ($role1->get_method_modifier_list($modifier_type)) { |
175 | $role2->$add( |
176 | $method_name, |
177 | $_ |
178 | ) foreach $role1->$get($method_name); |
179 | } |
180 | } |
181 | |
1c9db35c |
182 | |
fb1e11d5 |
183 | 1; |
184 | |
ad46f524 |
185 | # ABSTRACT: Compose a role into another role |
186 | |
fb1e11d5 |
187 | __END__ |
188 | |
189 | =pod |
190 | |
fb1e11d5 |
191 | =head1 DESCRIPTION |
192 | |
193 | =head2 METHODS |
194 | |
195 | =over 4 |
196 | |
197 | =item B<new> |
198 | |
199 | =item B<meta> |
200 | |
1c9db35c |
201 | =item B<apply> |
202 | |
709c321c |
203 | =item B<check_role_exclusions> |
204 | |
1c9db35c |
205 | =item B<check_required_methods> |
206 | |
709c321c |
207 | =item B<check_required_attributes> |
1c9db35c |
208 | |
209 | =item B<apply_attributes> |
210 | |
211 | =item B<apply_methods> |
212 | |
213 | =item B<apply_method_modifiers> |
214 | |
1c9db35c |
215 | =item B<apply_override_method_modifiers> |
216 | |
fb1e11d5 |
217 | =back |
218 | |
219 | =head1 BUGS |
220 | |
d4048ef3 |
221 | See L<Moose/BUGS> for details on reporting bugs. |
fb1e11d5 |
222 | |
fb1e11d5 |
223 | =cut |
224 | |