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