Implement the "eval $VERSION" trick from perlmodstyle so CPAN doesn't
[gitmo/Moose.git] / lib / Moose / Meta / Role / Application / ToClass.pm
CommitLineData
fb1e11d5 1package Moose::Meta::Role::Application::ToClass;
2
3use strict;
4use warnings;
5use metaclass;
6
7use Carp 'confess';
8use Scalar::Util 'blessed';
9
10use Data::Dumper;
11
75b95414 12our $VERSION = '0.55_01';
13$VERSION = eval $VERSION;
fb1e11d5 14our $AUTHORITY = 'cpan:STEVAN';
15
16use base 'Moose::Meta::Role::Application';
17
1c9db35c 18sub apply {
c4538447 19 my ($self, $role, $class) = @_;
1c9db35c 20 $self->SUPER::apply($role, $class);
c4538447 21 $class->add_role($role);
1c9db35c 22}
23
24sub check_role_exclusions {
25 my ($self, $role, $class) = @_;
26 if ($class->excludes_role($role->name)) {
27 confess "Conflict detected: " . $class->name . " excludes role '" . $role->name . "'";
28 }
29 foreach my $excluded_role_name ($role->get_excluded_roles_list) {
30 if ($class->does_role($excluded_role_name)) {
31 confess "The class " . $class->name . " does the excluded role '$excluded_role_name'";
32 }
33 }
34}
35
36sub check_required_methods {
37 my ($self, $role, $class) = @_;
38 # NOTE:
39 # we might need to move this down below the
40 # the attributes so that we can require any
41 # attribute accessors. However I am thinking
42 # that maybe those are somehow exempt from
43 # the require methods stuff.
44 foreach my $required_method_name ($role->get_required_method_list) {
45
3e19778d 46 if (!$class->find_method_by_name($required_method_name)) {
47
48 next if $self->is_aliased_method($required_method_name);
49
1c9db35c 50 confess "'" . $role->name . "' requires the method '$required_method_name' " .
51 "to be implemented by '" . $class->name . "'";
52 }
53 else {
54 # NOTE:
55 # we need to make sure that the method is
56 # not a method modifier, because those do
57 # not satisfy the requirements ...
58 my $method = $class->find_method_by_name($required_method_name);
59
60 # check if it is a generated accessor ...
61 (!$method->isa('Class::MOP::Method::Accessor'))
62 || confess "'" . $role->name . "' requires the method '$required_method_name' " .
63 "to be implemented by '" . $class->name . "', the method is only an attribute accessor";
64
65 # NOTE:
66 # All other tests here have been removed, they were tests
67 # for overriden methods and before/after/around modifiers.
68 # But we realized that for classes any overriden or modified
69 # methods would be backed by a real method of that name
70 # (and therefore meet the requirement). And for roles, the
71 # overriden and modified methods are "in statis" and so would
72 # not show up in this test anyway (and as a side-effect they
73 # would not fufill the requirement, which is exactly what we
74 # want them to do anyway).
75 # - SL
76 }
77 }
78}
79
709c321c 80sub check_required_attributes {
81
82}
83
1c9db35c 84sub apply_attributes {
85 my ($self, $role, $class) = @_;
86 foreach my $attribute_name ($role->get_attribute_list) {
87 # it if it has one already
88 if ($class->has_attribute($attribute_name) &&
89 # make sure we haven't seen this one already too
90 $class->get_attribute($attribute_name) != $role->get_attribute($attribute_name)) {
91 next;
92 }
93 else {
d7d8a8c7 94 $class->add_attribute(
95 $attribute_name,
96 $role->get_attribute($attribute_name)
97 );
1c9db35c 98 }
99 }
100}
101
102sub apply_methods {
103 my ($self, $role, $class) = @_;
104 foreach my $method_name ($role->get_method_list) {
c4538447 105
106 next if $self->is_method_excluded($method_name);
107
1c9db35c 108 # it if it has one already
109 if ($class->has_method($method_name) &&
110 # and if they are not the same thing ...
111 $class->get_method($method_name)->body != $role->get_method($method_name)->body) {
112 next;
113 }
620db045 114 else {
1c9db35c 115 # add it, although it could be overriden
116 $class->alias_method(
117 $method_name,
43a41ede 118 $role->get_method($method_name)
119 );
1c9db35c 120 }
43a41ede 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 }
1c9db35c 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
142sub 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
162sub 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
fb1e11d5 1741;
175
176__END__
177
178=pod
179
180=head1 NAME
181
ab76842e 182Moose::Meta::Role::Application::ToClass - Compose a role into a class
fb1e11d5 183
184=head1 DESCRIPTION
185
186=head2 METHODS
187
188=over 4
189
190=item B<new>
191
192=item B<meta>
193
1c9db35c 194=item B<apply>
195
709c321c 196=item B<check_role_exclusions>
197
1c9db35c 198=item B<check_required_methods>
199
709c321c 200=item B<check_required_attributes>
1c9db35c 201
202=item B<apply_attributes>
203
204=item B<apply_methods>
205
206=item B<apply_method_modifiers>
207
1c9db35c 208=item B<apply_override_method_modifiers>
209
fb1e11d5 210=back
211
212=head1 BUGS
213
214All complex software has bugs lurking in it, and this module is no
215exception. If you find a bug please either email me, or add the bug
216to cpan-RT.
217
218=head1 AUTHOR
219
220Stevan Little E<lt>stevan@iinteractive.comE<gt>
221
222=head1 COPYRIGHT AND LICENSE
223
778db3ac 224Copyright 2006-2008 by Infinity Interactive, Inc.
fb1e11d5 225
226L<http://www.iinteractive.com>
227
228This library is free software; you can redistribute it and/or modify
229it under the same terms as Perl itself.
230
231=cut
232