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