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