moving stuff around and some cleanup
[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         unless ($class->find_method_by_name($required_method_name)) {
46             confess "'" . $role->name . "' requires the method '$required_method_name' " .
47                     "to be implemented by '" . $class->name . "'";
48         }
49         else {
50             # NOTE:
51             # we need to make sure that the method is
52             # not a method modifier, because those do
53             # not satisfy the requirements ...
54             my $method = $class->find_method_by_name($required_method_name);
55
56             # check if it is a generated accessor ...
57             (!$method->isa('Class::MOP::Method::Accessor'))
58                 || confess "'" . $role->name . "' requires the method '$required_method_name' " .
59                            "to be implemented by '" . $class->name . "', the method is only an attribute accessor";
60
61             # NOTE:
62             # All other tests here have been removed, they were tests
63             # for overriden methods and before/after/around modifiers.
64             # But we realized that for classes any overriden or modified
65             # methods would be backed by a real method of that name
66             # (and therefore meet the requirement). And for roles, the
67             # overriden and modified methods are "in statis" and so would
68             # not show up in this test anyway (and as a side-effect they
69             # would not fufill the requirement, which is exactly what we
70             # want them to do anyway).
71             # - SL
72         }
73     }
74 }
75
76 sub apply_attributes {
77     my ($self, $role, $class) = @_;
78     foreach my $attribute_name ($role->get_attribute_list) {
79         # it if it has one already
80         if ($class->has_attribute($attribute_name) &&
81             # make sure we haven't seen this one already too
82             $class->get_attribute($attribute_name) != $role->get_attribute($attribute_name)) {
83             next;
84         }
85         else {
86             # NOTE:
87             # this is kinda ugly ...
88             if ($class->isa('Moose::Meta::Class')) {
89                 $class->_process_attribute(
90                     $attribute_name,
91                     %{$role->get_attribute($attribute_name)}
92                 );
93             }
94             else {
95                 $class->add_attribute(
96                     $attribute_name,
97                     $role->get_attribute($attribute_name)
98                 );
99             }
100         }
101     }
102 }
103
104 sub apply_methods {
105     my ($self, $role, $class) = @_;
106     foreach my $method_name ($role->get_method_list) {
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             # add it, although it could be overriden
115             $class->alias_method(
116                 $method_name,
117                 $role->get_method($method_name)
118             );
119         }
120     }
121     # we must reset the cache here since
122     # we are just aliasing methods, otherwise
123     # the modifiers go wonky.
124     $class->reset_package_cache_flag;        
125 }
126
127 sub apply_override_method_modifiers {
128     my ($self, $role, $class) = @_;
129     foreach my $method_name ($role->get_method_modifier_list('override')) {
130         # it if it has one already then ...
131         if ($class->has_method($method_name)) {
132             next;
133         }
134         else {
135             # if this is not a role, then we need to
136             # find the original package of the method
137             # so that we can tell the class were to
138             # find the right super() method
139             my $method = $role->get_override_method_modifier($method_name);
140             my ($package) = Class::MOP::get_code_info($method);
141             # if it is a class, we just add it
142             $class->add_override_method_modifier($method_name, $method, $package);
143         }
144     }
145 }
146
147 sub apply_method_modifiers {
148     my ($self, $modifier_type, $role, $class) = @_;
149     my $add = "add_${modifier_type}_method_modifier";
150     my $get = "get_${modifier_type}_method_modifiers";
151     foreach my $method_name ($role->get_method_modifier_list($modifier_type)) {
152         $class->$add(
153             $method_name,
154             $_
155         ) foreach $role->$get($method_name);
156     }
157 }
158
159 1;
160
161 __END__
162
163 =pod
164
165 =head1 NAME
166
167 Moose::Meta::Role::Application::ToClass
168
169 =head1 DESCRIPTION
170
171 =head2 METHODS
172
173 =over 4
174
175 =item B<new>
176
177 =item B<meta>
178
179 =item B<apply>
180
181 =item B<check_required_methods>
182
183 =item B<check_role_exclusions>
184
185 =item B<apply_attributes>
186
187 =item B<apply_methods>
188
189 =item B<apply_method_modifiers>
190
191 =item B<apply_override_method_modifiers>
192
193 =back
194
195 =head1 BUGS
196
197 All complex software has bugs lurking in it, and this module is no
198 exception. If you find a bug please either email me, or add the bug
199 to cpan-RT.
200
201 =head1 AUTHOR
202
203 Stevan Little E<lt>stevan@iinteractive.comE<gt>
204
205 =head1 COPYRIGHT AND LICENSE
206
207 Copyright 2006, 2007 by Infinity Interactive, Inc.
208
209 L<http://www.iinteractive.com>
210
211 This library is free software; you can redistribute it and/or modify
212 it under the same terms as Perl itself.
213
214 =cut
215