bump version to 0.89_01
[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 our $VERSION   = '0.89_01';
11 $VERSION = eval $VERSION;
12 our $AUTHORITY = 'cpan:STEVAN';
13
14 use base 'Moose::Meta::Role::Application';
15
16 __PACKAGE__->meta->add_attribute('role' => (
17     reader => 'role',
18 ));
19
20 __PACKAGE__->meta->add_attribute('class' => (
21     reader => 'class',
22 ));
23
24 sub apply {
25     my ($self, $role, $class) = @_;
26
27     # We need weak_ref in CMOP :(
28     weaken($self->{role}  = $role);
29     weaken($self->{class} = $class);
30
31     $self->SUPER::apply($role, $class);
32
33     $class->add_role($role);
34     $class->add_role_application($self);
35 }
36
37 sub check_role_exclusions {
38     my ($self, $role, $class) = @_;
39     if ($class->excludes_role($role->name)) {
40         $class->throw_error("Conflict detected: " . $class->name . " excludes role '" . $role->name . "'");
41     }
42     foreach my $excluded_role_name ($role->get_excluded_roles_list) {
43         if ($class->does_role($excluded_role_name)) {
44             $class->throw_error("The class " . $class->name . " does the excluded role '$excluded_role_name'");
45         }
46     }
47 }
48
49 sub check_required_methods {
50     my ($self, $role, $class) = @_;
51
52     my @missing;
53     my @is_attr;
54
55     # NOTE:
56     # we might need to move this down below the
57     # the attributes so that we can require any
58     # attribute accessors. However I am thinking
59     # that maybe those are somehow exempt from
60     # the require methods stuff.
61     foreach my $required_method ($role->get_required_method_list) {
62         my $required_method_name = $required_method->name;
63
64         if (!$class->find_method_by_name($required_method_name)) {
65
66             next if $self->is_aliased_method($required_method_name);
67
68             push @missing, $required_method;
69         }
70     }
71
72     return unless @missing;
73
74     my $error = '';
75
76     my @conflicts = grep { $_->isa('Moose::Meta::Role::Method::Conflicting') } @missing;
77
78     if (@conflicts) {
79         my $conflict = $conflicts[0];
80         my $roles = $conflict->roles_as_english_list;
81
82         my @same_role_conflicts = grep { $_->roles_as_english_list eq $roles } @conflicts;
83
84         if (@same_role_conflicts == 1) {
85             $error
86                 .= "Due to a method name conflict in roles "
87                 .  $roles
88                 . ", the method '"
89                 . $conflict->name
90                 . "' must be implemented or excluded by '"
91                 . $class->name
92                 . q{'};
93         }
94         else {
95             my $methods
96                 = Moose::Util::english_list( map { q{'} . $_->name . q{'} } @same_role_conflicts );
97
98             $error
99                 .= "Due to method name conflicts in roles "
100                 .  $roles
101                 . ", the methods "
102                 . $methods
103                 . " must be implemented or excluded by '"
104                 . $class->name
105                 . q{'};
106         }
107     }
108     elsif (@missing) {
109         my $noun = @missing == 1 ? 'method' : 'methods';
110
111         my $list
112             = Moose::Util::english_list( map { q{'} . $_ . q{'} } @missing );
113
114         $error
115             .= q{'}
116             . $role->name
117             . "' requires the $noun $list "
118             . "to be implemented by '"
119             . $class->name . q{'};
120     }
121
122     $class->throw_error($error);
123 }
124
125 sub check_required_attributes {
126
127 }
128
129 sub apply_attributes {
130     my ($self, $role, $class) = @_;
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                 $attribute_name,
141                 $role->get_attribute($attribute_name)
142             );
143         }
144     }
145 }
146
147 sub apply_methods {
148     my ($self, $role, $class) = @_;
149     foreach my $method_name ($role->get_method_list) {
150
151         unless ($self->is_method_excluded($method_name)) {
152             # it if it has one already
153             if ($class->has_method($method_name) &&
154                 # and if they are not the same thing ...
155                 $class->get_method($method_name)->body != $role->get_method($method_name)->body) {
156                 next;
157             }
158             else {
159                 # add it, although it could be overridden
160                 $class->add_method(
161                     $method_name,
162                     $role->get_method($method_name)
163                 );
164             }
165         }
166
167         if ($self->is_method_aliased($method_name)) {
168             my $aliased_method_name = $self->get_method_aliases->{$method_name};
169             # it if it has one already
170             if ($class->has_method($aliased_method_name) &&
171                 # and if they are not the same thing ...
172                 $class->get_method($aliased_method_name)->body != $role->get_method($method_name)->body) {
173                 $class->throw_error("Cannot create a method alias if a local method of the same name exists");
174             }
175             $class->add_method(
176                 $aliased_method_name,
177                 $role->get_method($method_name)
178             );
179         }
180     }
181     # we must reset the cache here since
182     # we are just aliasing methods, otherwise
183     # the modifiers go wonky.
184     $class->reset_package_cache_flag;
185 }
186
187 sub apply_override_method_modifiers {
188     my ($self, $role, $class) = @_;
189     foreach my $method_name ($role->get_method_modifier_list('override')) {
190         # it if it has one already then ...
191         if ($class->has_method($method_name)) {
192             next;
193         }
194         else {
195             # if this is not a role, then we need to
196             # find the original package of the method
197             # so that we can tell the class were to
198             # find the right super() method
199             my $method = $role->get_override_method_modifier($method_name);
200             my ($package) = Class::MOP::get_code_info($method);
201             # if it is a class, we just add it
202             $class->add_override_method_modifier($method_name, $method, $package);
203         }
204     }
205 }
206
207 sub apply_method_modifiers {
208     my ($self, $modifier_type, $role, $class) = @_;
209     my $add = "add_${modifier_type}_method_modifier";
210     my $get = "get_${modifier_type}_method_modifiers";
211     foreach my $method_name ($role->get_method_modifier_list($modifier_type)) {
212         $class->$add(
213             $method_name,
214             $_
215         ) foreach $role->$get($method_name);
216     }
217 }
218
219 1;
220
221 __END__
222
223 =pod
224
225 =head1 NAME
226
227 Moose::Meta::Role::Application::ToClass - Compose a role into a class
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 All complex software has bugs lurking in it, and this module is no
260 exception. If you find a bug please either email me, or add the bug
261 to cpan-RT.
262
263 =head1 AUTHOR
264
265 Stevan Little E<lt>stevan@iinteractive.comE<gt>
266
267 =head1 COPYRIGHT AND LICENSE
268
269 Copyright 2006-2009 by Infinity Interactive, Inc.
270
271 L<http://www.iinteractive.com>
272
273 This library is free software; you can redistribute it and/or modify
274 it under the same terms as Perl itself.
275
276 =cut
277