Beginning of dzilization
[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
fb1e11d5 10our $AUTHORITY = 'cpan:STEVAN';
11
12use base 'Moose::Meta::Role::Application';
13
ff9c4953 14__PACKAGE__->meta->add_attribute('role' => (
15 reader => 'role',
ff9c4953 16));
17
18__PACKAGE__->meta->add_attribute('class' => (
19 reader => 'class',
ff9c4953 20));
21
1c9db35c 22sub apply {
d03bd989 23 my ($self, $role, $class) = @_;
ff9c4953 24
154a4aae 25 # We need weak_ref in CMOP :(
26 weaken($self->{role} = $role);
27 weaken($self->{class} = $class);
ff9c4953 28
1c9db35c 29 $self->SUPER::apply($role, $class);
a9b63d79 30
d03bd989 31 $class->add_role($role);
a9b63d79 32 $class->add_role_application($self);
1c9db35c 33}
34
35sub check_role_exclusions {
36 my ($self, $role, $class) = @_;
37 if ($class->excludes_role($role->name)) {
4c0b3599 38 $class->throw_error("Conflict detected: " . $class->name . " excludes role '" . $role->name . "'");
1c9db35c 39 }
40 foreach my $excluded_role_name ($role->get_excluded_roles_list) {
41 if ($class->does_role($excluded_role_name)) {
4c0b3599 42 $class->throw_error("The class " . $class->name . " does the excluded role '$excluded_role_name'");
1c9db35c 43 }
44 }
45}
46
47sub check_required_methods {
48 my ($self, $role, $class) = @_;
d939e016 49
50 my @missing;
51 my @is_attr;
52
1c9db35c 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.
92b7f727 59 foreach my $required_method ($role->get_required_method_list) {
60 my $required_method_name = $required_method->name;
1c9db35c 61
3e19778d 62 if (!$class->find_method_by_name($required_method_name)) {
d03bd989 63
3e19778d 64 next if $self->is_aliased_method($required_method_name);
d939e016 65
92b7f727 66 push @missing, $required_method;
1c9db35c 67 }
1c9db35c 68 }
d939e016 69
f3e76c8f 70 return unless @missing;
d939e016 71
72 my $error = '';
73
bb365545 74 @missing = sort { $a->name cmp $b->name } @missing;
16d7dc8d 75 my @conflicts = grep { $_->isa('Moose::Meta::Role::Method::Conflicting') } @missing;
884837f3 76
77 if (@conflicts) {
78 my $conflict = $conflicts[0];
e893345e 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 }
884837f3 106 }
107 elsif (@missing) {
d939e016 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
d939e016 121 $class->throw_error($error);
1c9db35c 122}
123
709c321c 124sub check_required_attributes {
d03bd989 125
709c321c 126}
127
1c9db35c 128sub apply_attributes {
129 my ($self, $role, $class) = @_;
d037bb62 130 my $attr_metaclass = $role->applied_attribute_metaclass;
f785aad8 131
1c9db35c 132 foreach my $attribute_name ($role->get_attribute_list) {
133 # it if it has one already
134 if ($class->has_attribute($attribute_name) &&
135 # make sure we haven't seen this one already too
136 $class->get_attribute($attribute_name) != $role->get_attribute($attribute_name)) {
137 next;
138 }
139 else {
d7d8a8c7 140 $class->add_attribute(
f785aad8 141 $role->get_attribute($attribute_name)->attribute_for_class($attr_metaclass)
d7d8a8c7 142 );
1c9db35c 143 }
144 }
145}
146
147sub apply_methods {
4a9e8ff6 148 my ( $self, $role, $class ) = @_;
149
150 foreach my $method ( $role->_get_local_methods ) {
151 my $method_name = $method->name;
152
ba7d613d 153 next if $method->isa('Class::MOP::Method::Meta');
d03bd989 154
4a9e8ff6 155 unless ( $self->is_method_excluded($method_name) ) {
156
157 my $class_method = $class->get_method($method_name);
158
159 next if $class_method && $class_method->body != $method->body;
d03bd989 160
87e63626 161 $class->add_method(
4a9e8ff6 162 $method_name,
163 $method,
164 );
165 }
166
167 next unless $self->is_method_aliased($method_name);
168
169 my $aliased_method_name = $self->get_method_aliases->{$method_name};
170
171 my $class_method = $class->get_method($aliased_method_name);
172
173 if ( $class_method && $class_method->body != $method->body ) {
174 $class->throw_error(
175 "Cannot create a method alias if a local method of the same name exists"
d03bd989 176 );
177 }
4a9e8ff6 178
179 $class->add_method(
180 $aliased_method_name,
181 $method,
182 );
1c9db35c 183 }
4a9e8ff6 184
1c9db35c 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
ad46f524 225# ABSTRACT: Compose a role into a class
226
fb1e11d5 227__END__
228
229=pod
230
fb1e11d5 231=head1 DESCRIPTION
232
233=head2 METHODS
234
235=over 4
236
237=item B<new>
238
239=item B<meta>
240
1c9db35c 241=item B<apply>
242
709c321c 243=item B<check_role_exclusions>
244
1c9db35c 245=item B<check_required_methods>
246
709c321c 247=item B<check_required_attributes>
1c9db35c 248
249=item B<apply_attributes>
250
251=item B<apply_methods>
252
253=item B<apply_method_modifiers>
254
1c9db35c 255=item B<apply_override_method_modifiers>
256
fb1e11d5 257=back
258
259=head1 BUGS
260
d4048ef3 261See L<Moose/BUGS> for details on reporting bugs.
fb1e11d5 262
fb1e11d5 263=cut
264