Use dzil Authority plugin - remove $AUTHORITY from code
[gitmo/Moose.git] / lib / Moose / Meta / Role / Application / ToClass.pm
1 package Moose::Meta::Role::Application::ToClass;
2
3 use strict;
4 use warnings;
5 use metaclass;
6
7 use Moose::Util  'english_list';
8 use Scalar::Util 'weaken', 'blessed';
9
10 use base 'Moose::Meta::Role::Application';
11
12 __PACKAGE__->meta->add_attribute('role' => (
13     reader => 'role',
14 ));
15
16 __PACKAGE__->meta->add_attribute('class' => (
17     reader => 'class',
18 ));
19
20 sub apply {
21     my ($self, $role, $class) = @_;
22
23     # We need weak_ref in CMOP :(
24     weaken($self->{role}  = $role);
25     weaken($self->{class} = $class);
26
27     $self->SUPER::apply($role, $class);
28
29     $class->add_role($role);
30     $class->add_role_application($self);
31 }
32
33 sub check_role_exclusions {
34     my ($self, $role, $class) = @_;
35     if ($class->excludes_role($role->name)) {
36         $class->throw_error("Conflict detected: " . $class->name . " excludes role '" . $role->name . "'");
37     }
38     foreach my $excluded_role_name ($role->get_excluded_roles_list) {
39         if ($class->does_role($excluded_role_name)) {
40             $class->throw_error("The class " . $class->name . " does the excluded role '$excluded_role_name'");
41         }
42     }
43 }
44
45 sub check_required_methods {
46     my ($self, $role, $class) = @_;
47
48     my @missing;
49     my @is_attr;
50
51     # NOTE:
52     # we might need to move this down below the
53     # the attributes so that we can require any
54     # attribute accessors. However I am thinking
55     # that maybe those are somehow exempt from
56     # the require methods stuff.
57     foreach my $required_method ($role->get_required_method_list) {
58         my $required_method_name = $required_method->name;
59
60         if (!$class->find_method_by_name($required_method_name)) {
61
62             next if $self->is_aliased_method($required_method_name);
63
64             push @missing, $required_method;
65         }
66     }
67
68     return unless @missing;
69
70     my $error = '';
71
72     @missing = sort { $a->name cmp $b->name } @missing;
73     my @conflicts = grep { $_->isa('Moose::Meta::Role::Method::Conflicting') } @missing;
74
75     if (@conflicts) {
76         my $conflict = $conflicts[0];
77         my $roles = $conflict->roles_as_english_list;
78
79         my @same_role_conflicts = grep { $_->roles_as_english_list eq $roles } @conflicts;
80
81         if (@same_role_conflicts == 1) {
82             $error
83                 .= "Due to a method name conflict in roles "
84                 .  $roles
85                 . ", the method '"
86                 . $conflict->name
87                 . "' must be implemented or excluded by '"
88                 . $class->name
89                 . q{'};
90         }
91         else {
92             my $methods
93                 = Moose::Util::english_list( map { q{'} . $_->name . q{'} } @same_role_conflicts );
94
95             $error
96                 .= "Due to method name conflicts in roles "
97                 .  $roles
98                 . ", the methods "
99                 . $methods
100                 . " must be implemented or excluded by '"
101                 . $class->name
102                 . q{'};
103         }
104     }
105     elsif (@missing) {
106         my $noun = @missing == 1 ? 'method' : 'methods';
107
108         my $list
109             = Moose::Util::english_list( map { q{'} . $_ . q{'} } @missing );
110
111         $error
112             .= q{'}
113             . $role->name
114             . "' requires the $noun $list "
115             . "to be implemented by '"
116             . $class->name . q{'};
117     }
118
119     $class->throw_error($error);
120 }
121
122 sub check_required_attributes {
123
124 }
125
126 sub apply_attributes {
127     my ($self, $role, $class) = @_;
128     my $attr_metaclass = $role->applied_attribute_metaclass;
129
130     foreach my $attribute_name ($role->get_attribute_list) {
131         # it if it has one already
132         if ($class->has_attribute($attribute_name) &&
133             # make sure we haven't seen this one already too
134             $class->get_attribute($attribute_name) != $role->get_attribute($attribute_name)) {
135             next;
136         }
137         else {
138             $class->add_attribute(
139                 $role->get_attribute($attribute_name)->attribute_for_class($attr_metaclass)
140             );
141         }
142     }
143 }
144
145 sub apply_methods {
146     my ( $self, $role, $class ) = @_;
147
148     foreach my $method ( $role->_get_local_methods ) {
149         my $method_name = $method->name;
150
151         next if $method->isa('Class::MOP::Method::Meta');
152
153         unless ( $self->is_method_excluded($method_name) ) {
154
155             my $class_method = $class->get_method($method_name);
156
157             next if $class_method && $class_method->body != $method->body;
158
159             $class->add_method(
160                 $method_name,
161                 $method,
162             );
163         }
164
165         next unless $self->is_method_aliased($method_name);
166
167         my $aliased_method_name = $self->get_method_aliases->{$method_name};
168
169         my $class_method = $class->get_method($aliased_method_name);
170
171         if ( $class_method && $class_method->body != $method->body ) {
172             $class->throw_error(
173                 "Cannot create a method alias if a local method of the same name exists"
174             );
175         }
176
177         $class->add_method(
178             $aliased_method_name,
179             $method,
180         );
181     }
182
183     # we must reset the cache here since
184     # we are just aliasing methods, otherwise
185     # the modifiers go wonky.
186     $class->reset_package_cache_flag;
187 }
188
189 sub apply_override_method_modifiers {
190     my ($self, $role, $class) = @_;
191     foreach my $method_name ($role->get_method_modifier_list('override')) {
192         # it if it has one already then ...
193         if ($class->has_method($method_name)) {
194             next;
195         }
196         else {
197             # if this is not a role, then we need to
198             # find the original package of the method
199             # so that we can tell the class were to
200             # find the right super() method
201             my $method = $role->get_override_method_modifier($method_name);
202             my ($package) = Class::MOP::get_code_info($method);
203             # if it is a class, we just add it
204             $class->add_override_method_modifier($method_name, $method, $package);
205         }
206     }
207 }
208
209 sub apply_method_modifiers {
210     my ($self, $modifier_type, $role, $class) = @_;
211     my $add = "add_${modifier_type}_method_modifier";
212     my $get = "get_${modifier_type}_method_modifiers";
213     foreach my $method_name ($role->get_method_modifier_list($modifier_type)) {
214         $class->$add(
215             $method_name,
216             $_
217         ) foreach $role->$get($method_name);
218     }
219 }
220
221 1;
222
223 # ABSTRACT: Compose a role into a class
224
225 __END__
226
227 =pod
228
229 =head1 DESCRIPTION
230
231 =head2 METHODS
232
233 =over 4
234
235 =item B<new>
236
237 =item B<meta>
238
239 =item B<apply>
240
241 =item B<check_role_exclusions>
242
243 =item B<check_required_methods>
244
245 =item B<check_required_attributes>
246
247 =item B<apply_attributes>
248
249 =item B<apply_methods>
250
251 =item B<apply_method_modifiers>
252
253 =item B<apply_override_method_modifiers>
254
255 =back
256
257 =head1 BUGS
258
259 See L<Moose/BUGS> for details on reporting bugs.
260
261 =cut
262