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
CommitLineData
fb1e11d5 1package Moose::Meta::Role::Application::ToClass;
2
3use strict;
4use warnings;
5use metaclass;
6
d939e016 7use Moose::Util 'english_list';
154a4aae 8use Scalar::Util 'weaken', 'blessed';
fb1e11d5 9
6d0815b5 10our $VERSION = '0.93';
e606ae5f 11$VERSION = eval $VERSION;
fb1e11d5 12our $AUTHORITY = 'cpan:STEVAN';
13
14use base 'Moose::Meta::Role::Application';
15
ff9c4953 16__PACKAGE__->meta->add_attribute('role' => (
17 reader => 'role',
ff9c4953 18));
19
20__PACKAGE__->meta->add_attribute('class' => (
21 reader => 'class',
ff9c4953 22));
23
1c9db35c 24sub apply {
d03bd989 25 my ($self, $role, $class) = @_;
ff9c4953 26
154a4aae 27 # We need weak_ref in CMOP :(
28 weaken($self->{role} = $role);
29 weaken($self->{class} = $class);
ff9c4953 30
1c9db35c 31 $self->SUPER::apply($role, $class);
a9b63d79 32
d03bd989 33 $class->add_role($role);
a9b63d79 34 $class->add_role_application($self);
1c9db35c 35}
36
37sub check_role_exclusions {
38 my ($self, $role, $class) = @_;
721b5f29 39 if (ref $class eq 'Class::MOP::Class' ){
40 Carp::cluck('wtf');
41 }
1c9db35c 42 if ($class->excludes_role($role->name)) {
4c0b3599 43 $class->throw_error("Conflict detected: " . $class->name . " excludes role '" . $role->name . "'");
1c9db35c 44 }
45 foreach my $excluded_role_name ($role->get_excluded_roles_list) {
46 if ($class->does_role($excluded_role_name)) {
4c0b3599 47 $class->throw_error("The class " . $class->name . " does the excluded role '$excluded_role_name'");
1c9db35c 48 }
49 }
50}
51
52sub check_required_methods {
53 my ($self, $role, $class) = @_;
d939e016 54
55 my @missing;
56 my @is_attr;
57
1c9db35c 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.
92b7f727 64 foreach my $required_method ($role->get_required_method_list) {
65 my $required_method_name = $required_method->name;
1c9db35c 66
3e19778d 67 if (!$class->find_method_by_name($required_method_name)) {
d03bd989 68
3e19778d 69 next if $self->is_aliased_method($required_method_name);
d939e016 70
92b7f727 71 push @missing, $required_method;
1c9db35c 72 }
1c9db35c 73 }
d939e016 74
f3e76c8f 75 return unless @missing;
d939e016 76
77 my $error = '';
78
bb365545 79 @missing = sort { $a->name cmp $b->name } @missing;
16d7dc8d 80 my @conflicts = grep { $_->isa('Moose::Meta::Role::Method::Conflicting') } @missing;
884837f3 81
82 if (@conflicts) {
83 my $conflict = $conflicts[0];
e893345e 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 }
884837f3 111 }
112 elsif (@missing) {
d939e016 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
d939e016 126 $class->throw_error($error);
1c9db35c 127}
128
709c321c 129sub check_required_attributes {
d03bd989 130
709c321c 131}
132
1c9db35c 133sub 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 {
d7d8a8c7 143 $class->add_attribute(
721b5f29 144 $role->get_attribute($attribute_name)->clone
d7d8a8c7 145 );
1c9db35c 146 }
147 }
148}
149
150sub apply_methods {
151 my ($self, $role, $class) = @_;
152 foreach my $method_name ($role->get_method_list) {
5826183b 153 next if $method_name eq 'meta';
d03bd989 154
f5b6d42e 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 {
6549b0d1 163 # add it, although it could be overridden
f5b6d42e 164 $class->add_method(
165 $method_name,
166 $role->get_method($method_name)
d03bd989 167 );
f5b6d42e 168 }
1c9db35c 169 }
d03bd989 170
43a41ede 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) {
4c0b3599 177 $class->throw_error("Cannot create a method alias if a local method of the same name exists");
d03bd989 178 }
87e63626 179 $class->add_method(
43a41ede 180 $aliased_method_name,
181 $role->get_method($method_name)
d03bd989 182 );
183 }
1c9db35c 184 }
185 # we must reset the cache here since
186 # we are just aliasing methods, otherwise
187 # the modifiers go wonky.
d03bd989 188 $class->reset_package_cache_flag;
1c9db35c 189}
190
191sub 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
211sub 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
fb1e11d5 2231;
224
225__END__
226
227=pod
228
229=head1 NAME
230
ab76842e 231Moose::Meta::Role::Application::ToClass - Compose a role into a class
fb1e11d5 232
233=head1 DESCRIPTION
234
235=head2 METHODS
236
237=over 4
238
239=item B<new>
240
241=item B<meta>
242
1c9db35c 243=item B<apply>
244
709c321c 245=item B<check_role_exclusions>
246
1c9db35c 247=item B<check_required_methods>
248
709c321c 249=item B<check_required_attributes>
1c9db35c 250
251=item B<apply_attributes>
252
253=item B<apply_methods>
254
255=item B<apply_method_modifiers>
256
1c9db35c 257=item B<apply_override_method_modifiers>
258
fb1e11d5 259=back
260
261=head1 BUGS
262
263All complex software has bugs lurking in it, and this module is no
264exception. If you find a bug please either email me, or add the bug
265to cpan-RT.
266
267=head1 AUTHOR
268
269Stevan Little E<lt>stevan@iinteractive.comE<gt>
270
271=head1 COPYRIGHT AND LICENSE
272
2840a3b2 273Copyright 2006-2009 by Infinity Interactive, Inc.
fb1e11d5 274
275L<http://www.iinteractive.com>
276
277This library is free software; you can redistribute it and/or modify
278it under the same terms as Perl itself.
279
280=cut
281