anon-roles are now more efficient about package usage
[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
7use Carp 'confess';
8use Scalar::Util 'blessed';
9
10use Data::Dumper;
11
12our $VERSION = '0.01';
13our $AUTHORITY = 'cpan:STEVAN';
14
15use base 'Moose::Meta::Role::Application';
16
1c9db35c 17sub apply {
c4538447 18 my ($self, $role, $class) = @_;
1c9db35c 19 $self->SUPER::apply($role, $class);
c4538447 20 $class->add_role($role);
1c9db35c 21}
22
23sub check_role_exclusions {
24 my ($self, $role, $class) = @_;
25 if ($class->excludes_role($role->name)) {
26 confess "Conflict detected: " . $class->name . " excludes role '" . $role->name . "'";
27 }
28 foreach my $excluded_role_name ($role->get_excluded_roles_list) {
29 if ($class->does_role($excluded_role_name)) {
30 confess "The class " . $class->name . " does the excluded role '$excluded_role_name'";
31 }
32 }
33}
34
35sub check_required_methods {
36 my ($self, $role, $class) = @_;
37 # NOTE:
38 # we might need to move this down below the
39 # the attributes so that we can require any
40 # attribute accessors. However I am thinking
41 # that maybe those are somehow exempt from
42 # the require methods stuff.
43 foreach my $required_method_name ($role->get_required_method_list) {
44
3e19778d 45 if (!$class->find_method_by_name($required_method_name)) {
46
47 next if $self->is_aliased_method($required_method_name);
48
1c9db35c 49 confess "'" . $role->name . "' requires the method '$required_method_name' " .
50 "to be implemented by '" . $class->name . "'";
51 }
52 else {
53 # NOTE:
54 # we need to make sure that the method is
55 # not a method modifier, because those do
56 # not satisfy the requirements ...
57 my $method = $class->find_method_by_name($required_method_name);
58
59 # check if it is a generated accessor ...
60 (!$method->isa('Class::MOP::Method::Accessor'))
61 || confess "'" . $role->name . "' requires the method '$required_method_name' " .
62 "to be implemented by '" . $class->name . "', the method is only an attribute accessor";
63
64 # NOTE:
65 # All other tests here have been removed, they were tests
66 # for overriden methods and before/after/around modifiers.
67 # But we realized that for classes any overriden or modified
68 # methods would be backed by a real method of that name
69 # (and therefore meet the requirement). And for roles, the
70 # overriden and modified methods are "in statis" and so would
71 # not show up in this test anyway (and as a side-effect they
72 # would not fufill the requirement, which is exactly what we
73 # want them to do anyway).
74 # - SL
75 }
76 }
77}
78
709c321c 79sub check_required_attributes {
80
81}
82
1c9db35c 83sub apply_attributes {
84 my ($self, $role, $class) = @_;
85 foreach my $attribute_name ($role->get_attribute_list) {
86 # it if it has one already
87 if ($class->has_attribute($attribute_name) &&
88 # make sure we haven't seen this one already too
89 $class->get_attribute($attribute_name) != $role->get_attribute($attribute_name)) {
90 next;
91 }
92 else {
93 # NOTE:
94 # this is kinda ugly ...
95 if ($class->isa('Moose::Meta::Class')) {
96 $class->_process_attribute(
97 $attribute_name,
98 %{$role->get_attribute($attribute_name)}
99 );
100 }
101 else {
102 $class->add_attribute(
103 $attribute_name,
104 $role->get_attribute($attribute_name)
105 );
106 }
107 }
108 }
109}
110
111sub apply_methods {
112 my ($self, $role, $class) = @_;
113 foreach my $method_name ($role->get_method_list) {
c4538447 114
115 next if $self->is_method_excluded($method_name);
116
1c9db35c 117 # it if it has one already
118 if ($class->has_method($method_name) &&
119 # and if they are not the same thing ...
120 $class->get_method($method_name)->body != $role->get_method($method_name)->body) {
121 next;
122 }
43a41ede 123 else {
124
1c9db35c 125 # add it, although it could be overriden
126 $class->alias_method(
127 $method_name,
43a41ede 128 $role->get_method($method_name)
129 );
1c9db35c 130 }
43a41ede 131
132 if ($self->is_method_aliased($method_name)) {
133 my $aliased_method_name = $self->get_method_aliases->{$method_name};
134 # it if it has one already
135 if ($class->has_method($aliased_method_name) &&
136 # and if they are not the same thing ...
137 $class->get_method($aliased_method_name)->body != $role->get_method($method_name)->body) {
138 confess "Cannot create a method alias if a local method of the same name exists";
139 }
140 $class->alias_method(
141 $aliased_method_name,
142 $role->get_method($method_name)
143 );
144 }
1c9db35c 145 }
146 # we must reset the cache here since
147 # we are just aliasing methods, otherwise
148 # the modifiers go wonky.
149 $class->reset_package_cache_flag;
150}
151
152sub apply_override_method_modifiers {
153 my ($self, $role, $class) = @_;
154 foreach my $method_name ($role->get_method_modifier_list('override')) {
155 # it if it has one already then ...
156 if ($class->has_method($method_name)) {
157 next;
158 }
159 else {
160 # if this is not a role, then we need to
161 # find the original package of the method
162 # so that we can tell the class were to
163 # find the right super() method
164 my $method = $role->get_override_method_modifier($method_name);
165 my ($package) = Class::MOP::get_code_info($method);
166 # if it is a class, we just add it
167 $class->add_override_method_modifier($method_name, $method, $package);
168 }
169 }
170}
171
172sub apply_method_modifiers {
173 my ($self, $modifier_type, $role, $class) = @_;
174 my $add = "add_${modifier_type}_method_modifier";
175 my $get = "get_${modifier_type}_method_modifiers";
176 foreach my $method_name ($role->get_method_modifier_list($modifier_type)) {
177 $class->$add(
178 $method_name,
179 $_
180 ) foreach $role->$get($method_name);
181 }
182}
183
fb1e11d5 1841;
185
186__END__
187
188=pod
189
190=head1 NAME
191
192Moose::Meta::Role::Application::ToClass
193
194=head1 DESCRIPTION
195
196=head2 METHODS
197
198=over 4
199
200=item B<new>
201
202=item B<meta>
203
1c9db35c 204=item B<apply>
205
709c321c 206=item B<check_role_exclusions>
207
1c9db35c 208=item B<check_required_methods>
209
709c321c 210=item B<check_required_attributes>
1c9db35c 211
212=item B<apply_attributes>
213
214=item B<apply_methods>
215
216=item B<apply_method_modifiers>
217
1c9db35c 218=item B<apply_override_method_modifiers>
219
fb1e11d5 220=back
221
222=head1 BUGS
223
224All complex software has bugs lurking in it, and this module is no
225exception. If you find a bug please either email me, or add the bug
226to cpan-RT.
227
228=head1 AUTHOR
229
230Stevan Little E<lt>stevan@iinteractive.comE<gt>
231
232=head1 COPYRIGHT AND LICENSE
233
778db3ac 234Copyright 2006-2008 by Infinity Interactive, Inc.
fb1e11d5 235
236L<http://www.iinteractive.com>
237
238This library is free software; you can redistribute it and/or modify
239it under the same terms as Perl itself.
240
241=cut
242