a bug fix and some tweaks
[gitmo/Moose.git] / lib / Moose / Meta / Role / Application / ToClass.pm
1 package Moose::Meta::Role::Application::ToClass;
2
3 use strict;
4 use warnings;
5 use metaclass;
6
7 use Carp            'confess';
8 use Scalar::Util    'blessed';
9
10 use Data::Dumper;
11
12 our $VERSION   = '0.01';
13 our $AUTHORITY = 'cpan:STEVAN';
14
15 use base 'Moose::Meta::Role::Application';
16
17 sub apply {
18     my ($self, $role, $class) = @_;    
19     $self->SUPER::apply($role, $class);
20     $class->add_role($role);        
21 }
22
23 sub 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
35 sub 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
45         if (!$class->find_method_by_name($required_method_name)) {
46             
47             next if $self->is_aliased_method($required_method_name);
48             
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
79 sub check_required_attributes {
80     
81 }
82
83 sub 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             $class->add_attribute(
94                 $attribute_name,
95                 $role->get_attribute($attribute_name)
96             );
97         }
98     }
99 }
100
101 sub apply_methods {
102     my ($self, $role, $class) = @_;
103     foreach my $method_name ($role->get_method_list) {
104         
105         next if $self->is_method_excluded($method_name);
106         
107         # it if it has one already
108         if ($class->has_method($method_name) &&
109             # and if they are not the same thing ...
110             $class->get_method($method_name)->body != $role->get_method($method_name)->body) {
111             next;
112         }
113         else {           
114             
115             # add it, although it could be overriden
116             $class->alias_method(
117                 $method_name,
118                 $role->get_method($method_name)
119             );         
120         }
121         
122         if ($self->is_method_aliased($method_name)) {
123             my $aliased_method_name = $self->get_method_aliases->{$method_name};
124             # it if it has one already
125             if ($class->has_method($aliased_method_name) &&
126                 # and if they are not the same thing ...
127                 $class->get_method($aliased_method_name)->body != $role->get_method($method_name)->body) {
128                 confess "Cannot create a method alias if a local method of the same name exists";
129             }            
130             $class->alias_method(
131                 $aliased_method_name,
132                 $role->get_method($method_name)
133             );                
134         }        
135     }
136     # we must reset the cache here since
137     # we are just aliasing methods, otherwise
138     # the modifiers go wonky.
139     $class->reset_package_cache_flag;        
140 }
141
142 sub apply_override_method_modifiers {
143     my ($self, $role, $class) = @_;
144     foreach my $method_name ($role->get_method_modifier_list('override')) {
145         # it if it has one already then ...
146         if ($class->has_method($method_name)) {
147             next;
148         }
149         else {
150             # if this is not a role, then we need to
151             # find the original package of the method
152             # so that we can tell the class were to
153             # find the right super() method
154             my $method = $role->get_override_method_modifier($method_name);
155             my ($package) = Class::MOP::get_code_info($method);
156             # if it is a class, we just add it
157             $class->add_override_method_modifier($method_name, $method, $package);
158         }
159     }
160 }
161
162 sub apply_method_modifiers {
163     my ($self, $modifier_type, $role, $class) = @_;
164     my $add = "add_${modifier_type}_method_modifier";
165     my $get = "get_${modifier_type}_method_modifiers";
166     foreach my $method_name ($role->get_method_modifier_list($modifier_type)) {
167         $class->$add(
168             $method_name,
169             $_
170         ) foreach $role->$get($method_name);
171     }
172 }
173
174 1;
175
176 __END__
177
178 =pod
179
180 =head1 NAME
181
182 Moose::Meta::Role::Application::ToClass - Compose a role into a class
183
184 =head1 DESCRIPTION
185
186 =head2 METHODS
187
188 =over 4
189
190 =item B<new>
191
192 =item B<meta>
193
194 =item B<apply>
195
196 =item B<check_role_exclusions>
197
198 =item B<check_required_methods>
199
200 =item B<check_required_attributes>
201
202 =item B<apply_attributes>
203
204 =item B<apply_methods>
205
206 =item B<apply_method_modifiers>
207
208 =item B<apply_override_method_modifiers>
209
210 =back
211
212 =head1 BUGS
213
214 All complex software has bugs lurking in it, and this module is no
215 exception. If you find a bug please either email me, or add the bug
216 to cpan-RT.
217
218 =head1 AUTHOR
219
220 Stevan Little E<lt>stevan@iinteractive.comE<gt>
221
222 =head1 COPYRIGHT AND LICENSE
223
224 Copyright 2006-2008 by Infinity Interactive, Inc.
225
226 L<http://www.iinteractive.com>
227
228 This library is free software; you can redistribute it and/or modify
229 it under the same terms as Perl itself.
230
231 =cut
232