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