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