f283ad76da33545b65c1f65750ec6d7eee559419
[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             # 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
111 sub apply_methods {
112     my ($self, $role, $class) = @_;
113     foreach my $method_name ($role->get_method_list) {
114         
115         next if $self->is_method_excluded($method_name);
116         
117         my $orig_method_name = $method_name;
118         
119         if ($self->is_method_aliased($method_name)) {
120             $method_name = $self->get_method_aliases->{$method_name};
121         }
122         
123         # it if it has one already
124         if ($class->has_method($method_name) &&
125             # and if they are not the same thing ...
126             $class->get_method($method_name)->body != $role->get_method($method_name)->body) {
127             next;
128         }
129         else {
130             # add it, although it could be overriden
131             $class->alias_method(
132                 $method_name,
133                 $role->get_method($orig_method_name)
134             );
135         }
136     }
137     # we must reset the cache here since
138     # we are just aliasing methods, otherwise
139     # the modifiers go wonky.
140     $class->reset_package_cache_flag;        
141 }
142
143 sub apply_override_method_modifiers {
144     my ($self, $role, $class) = @_;
145     foreach my $method_name ($role->get_method_modifier_list('override')) {
146         # it if it has one already then ...
147         if ($class->has_method($method_name)) {
148             next;
149         }
150         else {
151             # if this is not a role, then we need to
152             # find the original package of the method
153             # so that we can tell the class were to
154             # find the right super() method
155             my $method = $role->get_override_method_modifier($method_name);
156             my ($package) = Class::MOP::get_code_info($method);
157             # if it is a class, we just add it
158             $class->add_override_method_modifier($method_name, $method, $package);
159         }
160     }
161 }
162
163 sub apply_method_modifiers {
164     my ($self, $modifier_type, $role, $class) = @_;
165     my $add = "add_${modifier_type}_method_modifier";
166     my $get = "get_${modifier_type}_method_modifiers";
167     foreach my $method_name ($role->get_method_modifier_list($modifier_type)) {
168         $class->$add(
169             $method_name,
170             $_
171         ) foreach $role->$get($method_name);
172     }
173 }
174
175 1;
176
177 __END__
178
179 =pod
180
181 =head1 NAME
182
183 Moose::Meta::Role::Application::ToClass
184
185 =head1 DESCRIPTION
186
187 =head2 METHODS
188
189 =over 4
190
191 =item B<new>
192
193 =item B<meta>
194
195 =item B<apply>
196
197 =item B<check_role_exclusions>
198
199 =item B<check_required_methods>
200
201 =item B<check_required_attributes>
202
203 =item B<apply_attributes>
204
205 =item B<apply_methods>
206
207 =item B<apply_method_modifiers>
208
209 =item B<apply_override_method_modifiers>
210
211 =back
212
213 =head1 BUGS
214
215 All complex software has bugs lurking in it, and this module is no
216 exception. If you find a bug please either email me, or add the bug
217 to cpan-RT.
218
219 =head1 AUTHOR
220
221 Stevan Little E<lt>stevan@iinteractive.comE<gt>
222
223 =head1 COPYRIGHT AND LICENSE
224
225 Copyright 2006-2008 by Infinity Interactive, Inc.
226
227 L<http://www.iinteractive.com>
228
229 This library is free software; you can redistribute it and/or modify
230 it under the same terms as Perl itself.
231
232 =cut
233