Real attribute objects in roles is now working, with a few hacks and changes to the...
[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.93';
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 (ref $class eq 'Class::MOP::Class' ){
40         Carp::cluck('wtf');
41     }
42     if ($class->excludes_role($role->name)) {
43         $class->throw_error("Conflict detected: " . $class->name . " excludes role '" . $role->name . "'");
44     }
45     foreach my $excluded_role_name ($role->get_excluded_roles_list) {
46         if ($class->does_role($excluded_role_name)) {
47             $class->throw_error("The class " . $class->name . " does the excluded role '$excluded_role_name'");
48         }
49     }
50 }
51
52 sub check_required_methods {
53     my ($self, $role, $class) = @_;
54
55     my @missing;
56     my @is_attr;
57
58     # NOTE:
59     # we might need to move this down below the
60     # the attributes so that we can require any
61     # attribute accessors. However I am thinking
62     # that maybe those are somehow exempt from
63     # the require methods stuff.
64     foreach my $required_method ($role->get_required_method_list) {
65         my $required_method_name = $required_method->name;
66
67         if (!$class->find_method_by_name($required_method_name)) {
68
69             next if $self->is_aliased_method($required_method_name);
70
71             push @missing, $required_method;
72         }
73     }
74
75     return unless @missing;
76
77     my $error = '';
78
79     @missing = sort { $a->name cmp $b->name } @missing;
80     my @conflicts = grep { $_->isa('Moose::Meta::Role::Method::Conflicting') } @missing;
81
82     if (@conflicts) {
83         my $conflict = $conflicts[0];
84         my $roles = $conflict->roles_as_english_list;
85
86         my @same_role_conflicts = grep { $_->roles_as_english_list eq $roles } @conflicts;
87
88         if (@same_role_conflicts == 1) {
89             $error
90                 .= "Due to a method name conflict in roles "
91                 .  $roles
92                 . ", the method '"
93                 . $conflict->name
94                 . "' must be implemented or excluded by '"
95                 . $class->name
96                 . q{'};
97         }
98         else {
99             my $methods
100                 = Moose::Util::english_list( map { q{'} . $_->name . q{'} } @same_role_conflicts );
101
102             $error
103                 .= "Due to method name conflicts in roles "
104                 .  $roles
105                 . ", the methods "
106                 . $methods
107                 . " must be implemented or excluded by '"
108                 . $class->name
109                 . q{'};
110         }
111     }
112     elsif (@missing) {
113         my $noun = @missing == 1 ? 'method' : 'methods';
114
115         my $list
116             = Moose::Util::english_list( map { q{'} . $_ . q{'} } @missing );
117
118         $error
119             .= q{'}
120             . $role->name
121             . "' requires the $noun $list "
122             . "to be implemented by '"
123             . $class->name . q{'};
124     }
125
126     $class->throw_error($error);
127 }
128
129 sub check_required_attributes {
130
131 }
132
133 sub apply_attributes {
134     my ($self, $role, $class) = @_;
135     foreach my $attribute_name ($role->get_attribute_list) {
136         # it if it has one already
137         if ($class->has_attribute($attribute_name) &&
138             # make sure we haven't seen this one already too
139             $class->get_attribute($attribute_name) != $role->get_attribute($attribute_name)) {
140             next;
141         }
142         else {
143             $class->add_attribute(
144                 $role->get_attribute($attribute_name)->clone
145             );
146         }
147     }
148 }
149
150 sub apply_methods {
151     my ($self, $role, $class) = @_;
152     foreach my $method_name ($role->get_method_list) {
153         next if $method_name eq 'meta';
154
155         unless ($self->is_method_excluded($method_name)) {
156             # it if it has one already
157             if ($class->has_method($method_name) &&
158                 # and if they are not the same thing ...
159                 $class->get_method($method_name)->body != $role->get_method($method_name)->body) {
160                 next;
161             }
162             else {
163                 # add it, although it could be overridden
164                 $class->add_method(
165                     $method_name,
166                     $role->get_method($method_name)
167                 );
168             }
169         }
170
171         if ($self->is_method_aliased($method_name)) {
172             my $aliased_method_name = $self->get_method_aliases->{$method_name};
173             # it if it has one already
174             if ($class->has_method($aliased_method_name) &&
175                 # and if they are not the same thing ...
176                 $class->get_method($aliased_method_name)->body != $role->get_method($method_name)->body) {
177                 $class->throw_error("Cannot create a method alias if a local method of the same name exists");
178             }
179             $class->add_method(
180                 $aliased_method_name,
181                 $role->get_method($method_name)
182             );
183         }
184     }
185     # we must reset the cache here since
186     # we are just aliasing methods, otherwise
187     # the modifiers go wonky.
188     $class->reset_package_cache_flag;
189 }
190
191 sub apply_override_method_modifiers {
192     my ($self, $role, $class) = @_;
193     foreach my $method_name ($role->get_method_modifier_list('override')) {
194         # it if it has one already then ...
195         if ($class->has_method($method_name)) {
196             next;
197         }
198         else {
199             # if this is not a role, then we need to
200             # find the original package of the method
201             # so that we can tell the class were to
202             # find the right super() method
203             my $method = $role->get_override_method_modifier($method_name);
204             my ($package) = Class::MOP::get_code_info($method);
205             # if it is a class, we just add it
206             $class->add_override_method_modifier($method_name, $method, $package);
207         }
208     }
209 }
210
211 sub apply_method_modifiers {
212     my ($self, $modifier_type, $role, $class) = @_;
213     my $add = "add_${modifier_type}_method_modifier";
214     my $get = "get_${modifier_type}_method_modifiers";
215     foreach my $method_name ($role->get_method_modifier_list($modifier_type)) {
216         $class->$add(
217             $method_name,
218             $_
219         ) foreach $role->$get($method_name);
220     }
221 }
222
223 1;
224
225 __END__
226
227 =pod
228
229 =head1 NAME
230
231 Moose::Meta::Role::Application::ToClass - Compose a role into a class
232
233 =head1 DESCRIPTION
234
235 =head2 METHODS
236
237 =over 4
238
239 =item B<new>
240
241 =item B<meta>
242
243 =item B<apply>
244
245 =item B<check_role_exclusions>
246
247 =item B<check_required_methods>
248
249 =item B<check_required_attributes>
250
251 =item B<apply_attributes>
252
253 =item B<apply_methods>
254
255 =item B<apply_method_modifiers>
256
257 =item B<apply_override_method_modifiers>
258
259 =back
260
261 =head1 BUGS
262
263 All complex software has bugs lurking in it, and this module is no
264 exception. If you find a bug please either email me, or add the bug
265 to cpan-RT.
266
267 =head1 AUTHOR
268
269 Stevan Little E<lt>stevan@iinteractive.comE<gt>
270
271 =head1 COPYRIGHT AND LICENSE
272
273 Copyright 2006-2009 by Infinity Interactive, Inc.
274
275 L<http://www.iinteractive.com>
276
277 This library is free software; you can redistribute it and/or modify
278 it under the same terms as Perl itself.
279
280 =cut
281