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