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